Excel Unload
Excel.Application oXLApp = null;
Excel.Worksheet oXLWs = null;
Excel.Workbook oWB = null;
int ColNumber = 7;
string templateFilePath = System.Configuration.ConfigurationSettings.AppSett ings["Report_Template"].ToString();
filePath = System.Configuration.ConfigurationSettings.AppSett ings["Report_FilePath"].ToString();
fileName = DateTime.Now.Ticks.ToString()+".xls";
//Create the Report Files Folder, if the Directory doesn't exist
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
//Throw exception if template file is not found
if(!File.Exists(templateFilePath))
{
throw new NostroException(ExceptionType.SpecificException,Ex ceptionField.ExcelTemplateNotFound);
}
File.Copy(templateFilePath,filePath+@"\"+fileName, true);
GC.Collect();//Clear Any Excel objects created prev.
oXLApp = new Excel.ApplicationClass();
oXLWs = new Excel.WorksheetClass();
//Don't show the Excel App to the user, when it gets loaded
oXLApp.Visible = false;
oXLApp.UserControl = false;
oWB = oXLApp.Workbooks.Open(filePath+@"\"+fileName, 0,false, 5,"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,0, true);
oXLWs = (Excel.Worksheet)(oWB.ActiveSheet);
//oXLApp.Workbooks.Open(filePath+@"\"+fileName,Type. Missing,Type.Missing,Type.Missing,Type.Missing,Typ e.Missing,Type.Missing,Type.Missing,Type.Missing,T ype.Missing,Type.Missing,Type.Missing,Type.Missing );
//oXLWs = (Excel.Worksheet)oXLApp.Sheets.get_Item(1);
if (_reportType == ReportTypeEnum.AgeingSummary)
{
//Creating headers
oXLWs.Cells[ColNumber,1] = "Outstanding duration (in days)";
oXLWs.Cells[ColNumber,2] = "No of entries for Nostro";
oXLWs.Cells[ColNumber,3] = "Amount for Nostro entry";
oXLWs.Cells[ColNumber,4] = "No of entries for Mirror";
oXLWs.Cells[ColNumber,5] = "Amount for Mirror entry";
//Populate data from ReportModel Array
for (int i=0; i<_reportModelArray.Length; i++)//Rows
{
oXLWs.Cells[i+ColNumber+1,1] = _reportModelArray[i].OutstandingDateRange;
oXLWs.Cells[i+ColNumber+1,2] = _reportModelArray[i].NostroNoOfEntries.ToString();
oXLWs.Cells[i+ColNumber+1,3] = _reportModelArray[i].NostroAmount.ToString();
oXLWs.Cells[i+ColNumber+1,4] = _reportModelArray[i].MirrorNoOfEntries.ToString();
oXLWs.Cells[i+ColNumber+1,5] = _reportModelArray[i].MirrorAmount.ToString();
i++;
}
}
else if (_reportType == ReportTypeEnum.Recon)
{
}
//Set the header text to Bold
oXLWs.get_Range("A"+ColNumber.ToString(),"Z"+ColNu mber.ToString()).Font.Bold = true;
oXLWs.get_Range("A"+ColNumber.ToString(),"Z"+ColNu mber.ToString()).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
//Set the filepath to Absolute File path (with the file name that is generated)
filePath += @"\"+DateTime.Now.Ticks.ToString()+".xls";
//Save the file
oXLWs.SaveAs(filePath,Excel.XlFileFormat.xlWorkboo kNormal,Type.Missing,Type.Missing,Type.Missing,Typ e.Missing,Type.Missing,Type.Missing,Type.Missing);
This code does not unload the Excel from the memory, it can be seen in the taskManager. Kindly help me out.
The finally code has ::
oWB.Close(null,null,null);
// oXLApp.Workbooks.Close();
// oXLApp.Quit();
// System.Runtime.InteropServices.Marshal.ReleaseComO bject(oXLApp);
// System.Runtime.InteropServices.Marshal.ReleaseComO bject(oXLWs);
// System.Runtime.InteropServices.Marshal.ReleaseComO bject(oWB);
// oXLWs = null;
// oWB = null;
// oXLApp = null;
// GC.Collect();// force final cleanup!
|