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;
xlWorkBook = xlApp.Workbooks.Add(misValue);
Logger.Log("Started excel Template ", LogFilename.ServiceLog, 3, MethodInfo.GetCurrentMethod().Name);
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);
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];
}
}
}
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;