Click here to Skip to main content
15,884,668 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I am getting the below exception while writing to a excel file in c# while deploying in a server.Locally no issue as such.

Exception:

Microsoft Excel cannot open or save any more documents because there is not enough available memory or disk space.

To make more memory available, close workbooks or programs you no longer need.

• To free disk space, delete files you no longer need from the disk you are saving to

What I have tried:

Core Code is as below:
_Application xlApp = new Microsoft.Office.Interop.Excel.Application();
object misValue = System.Reflection.Missing.Value;
_Workbook xlWorkBook = null;
_Worksheet xlWorkSheet = null;
string strFileName = string.Empty;
try
{

    string ExcelTemplateFullPath = string.Empty;
    string ExcelExtension = string.Empty;
    string DestinationFullPathExcel = string.Empty;

    if (File.Exists(templatePath + "CustomerTemplate" + ".xls"))
    {
        Logger.Log("Started generating .xls File ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
        ExcelExtension = ".xls";
        ExcelTemplateFullPath = templatePath + "CustomerTemplate" + ExcelExtension;
        strFileName = filename + ExcelExtension;
        DestinationFullPathExcel = DestinationPDFPath + strFileName;

    }
    else if (File.Exists(templatePath + "CustomerTemplate" + ".xlsx"))
    {
        Logger.Log("Started generating .xlsx File ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
        ExcelExtension = ".xlsx";
        ExcelTemplateFullPath = templatePath + "CustomerTemplate" + ExcelExtension;
        strFileName = filename + ExcelExtension;
        DestinationFullPathExcel = DestinationPDFPath + strFileName;

    }
    else
    {
        Logger.Log("Template doesn't exists in the Folder: " + templatePath, LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
        return strFileName;
    }



    xlApp.DisplayAlerts = false;
    //object misValue = System.Reflection.Missing.Value;
    // _Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    Logger.Log("Started excel Template ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
    //xlWorkBook = xlApp.Workbooks.Open(ExcelTemplateFullPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
    try
    {
        xlWorkBook = xlApp.Workbooks.Add(ExcelTemplateFullPath);
    }
    catch(Exception ex)
    {
        Logger.Log("Failed to open ExcelTemplate ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
        Logger.Log(ex, LogFilename.ServiceLog, 1, MethodInfo.GetCurrentMethod().Name, false);

    }
    Logger.Log("Opened  excel Template ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
   // _Worksheet xlWorkSheet = (_Worksheet)xlWorkBook.Worksheets.get_Item(1);
    try
    {
        xlWorkSheet = (_Worksheet)xlWorkBook.Worksheets.get_Item(1);
        for (int i = 0; i < Values.Length; i++)
        {
            string[] val = Values[i].Split(delimiter1);

            for (int j = 0; j < val.Length; j++)
            {
                int k = 0;
                k = i + 1;
                //xlWorkSheet.Cells[k + 1, j + 1] = "'" + val[j];
                xlWorkSheet.Cells[k + 1, j + 1] = val[j];

            }

        }
    }
    catch (Exception ex)
    {
        Logger.Log("Failed to extract ExcelTemplate ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
        Logger.Log(ex, LogFilename.ServiceLog, 1, MethodInfo.GetCurrentMethod().Name, false);
    }
    Logger.Log("Extracted  excel Template ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
    try
    {
        xlWorkBook.SaveAs(DestinationFullPathExcel, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    }
    catch (Exception ex)
    {
        Logger.Log("Failed to save generated ExcelTemplate ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
        Logger.Log(ex, LogFilename.ServiceLog, 1, MethodInfo.GetCurrentMethod().Name, false);
    }


    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);


}

catch (Exception ex)
{
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);
    throw (ex);
}
return strFileName;
Posted
Updated 27-Jul-18 3:11am
Comments
dan!sh 27-Jul-18 9:06am    
Is this happening all the time or intermittently? You should perhaps check serve health as to how many processes are running on it and how many resources are available.

1 solution

You've tagged this question as ASP.NET; Office Interop is not supported in ASP.NET, or any other non-interactive process:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


There are various ways to create Excel spreadsheets on the server without using Office interop. For example:
 
Share this answer
 
Comments
Alek Massey 27-Jul-18 11:06am    
Agreed, an Excel application is huge resource wise.
Use the openxml SDK. It works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900