I have an multi threaded Application. One of the property of application is reporting.
User can take one year data day by day
I fetch the database, get the results to the List. Then use Excel (as com object).
Excel is opened and cell values are started to be added from the list.
While these processes are going suddenly I get this message:
Here is my code for excel reporting:
private void RNReportDensityStatistics(List<object[]> _PlateBasedDensityStaticticsList)
{
try
{
RNTakeReportButton.Enabled = false;
RNExcelApp = new RNExcel.Application();
RNExcelApp.Visible = false;
RNWorkBook = RNExcelApp.Workbooks.Add();
RNWorkSheet = (RNExcel.Worksheet)RNExcelApp.ActiveSheet;
RNExcelApp.DisplayAlerts = false;
RNProgressBar.Visible = true;
RNProgressBar.Minimum = 0;
RNProgressBar.Maximum = _PlateBasedDensityStaticticsList.Count;
RNProgressBar.Value = 0;
RNProgressBar.Step = 1;
RNExcelApp.Range["A2"].Value = GetGUIItemString(GUIItemIndex.RNReportDensityStatisticsAutoParkPlate);
RNExcelApp.Range["B2"].Value = GetGUIItemString(GUIItemIndex.RNReportDensityStatisticsAutoParkEntryTime);
RNExcelApp.Range["C2"].Value = GetGUIItemString(GUIItemIndex.RNReportDensityStatisticsAutoParkExitTime);
RNExcelApp.Range["D2"].Value = GetGUIItemString(GUIItemIndex.RNReportDensityStatisticsAutoParkCameraIP);
RNExcelApp.Range["E2"].Value = GetGUIItemString(GUIItemIndex.RNReportDensityStatisticsAutoParkSpaceNo);
RNWorkSheet.Columns[1].AutoFit();
RNWorkSheet.Columns[2].AutoFit();
RNWorkSheet.Columns[3].AutoFit();
RNWorkSheet.Columns[4].AutoFit();
RNWorkSheet.Columns[5].AutoFit();
var row = 2;
foreach (var DensityStatistics in _PlateBasedDensityStaticticsList)
{
row++;
RNWorkSheet.Cells[row, "A"] = DensityStatistics[4];
RNWorkSheet.Cells[row, "B"] = ConvertEpochToDateTime(Convert.ToUInt64(DensityStatistics[2]));
RNWorkSheet.Cells[row, "C"] = ConvertEpochToDateTime(Convert.ToUInt64(DensityStatistics[3]));
RNWorkSheet.Cells[row, "D"] = DensityStatistics[0];
RNWorkSheet.Cells[row, "E"] = DensityStatistics[1];
RNProgressBar.PerformStep();
}
RNWorkSheet.Columns[1].AutoFit();
RNWorkSheet.Columns[2].AutoFit();
RNWorkSheet.Columns[3].AutoFit();
RNWorkSheet.Columns[4].AutoFit();
RNWorkSheet.Columns[5].AutoFit();
string RNExcelReportPath = RNExcelReportPathStartingAdress
+ GetGUIItemString(GUIItemIndex.RNReportPathNameAsPlate)
+ RNPlateSearchTextBox.Text + " "
+ GetGUIItemString(GUIItemIndex.RNReportPathNameAsDensity)
+ ".xlsx";
RNWorkBook.SaveAs(RNExcelReportPath,
misValue,
RNExcelPassword,
misValue,
misValue,
misValue,
RNExcel.XlSaveAsAccessMode.xlExclusive,
misValue,
misValue,
misValue,
misValue,
misValue);
RNWorkBook.Close(true, misValue, misValue);
RNExcelApp.Application.Quit();
RNExcelApp.Quit();
RNReleaseObject(RNWorkSheet);
RNReleaseObject(RNWorkBook);
RNReleaseObject(RNExcelApp);
RNTakeReportButton.Enabled = true;
AddLog(LogIndex.RNReportDensityStatisticsSuc, RNExcelReportPath, string.Empty, string.Empty);
}
catch (Exception ex)
{
RNReleaseObject(RNWorkSheet);
RNReleaseObject(RNWorkBook);
RNReleaseObject(RNExcelApp);
AddException(ex.ToString());
RNTakeReportButton.Enabled = true;
AddLog(LogIndex.RNReportDensityStatisticsFail, string.Empty, string.Empty, string.Empty);
}
finally
{
RNReleaseObject(RNWorkSheet);
RNReleaseObject(RNWorkBook);
RNReleaseObject(RNExcelApp);
RNTakeReportButton.Enabled = true;
RNProgressBar.Value = _PlateBasedDensityStaticticsList.Count;
RNProgressBar.Update();
}
}
private void RNReleaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
if (System.Runtime.InteropServices.Marshal.AreComObjectsAvailableForCleanup())
{
System.Runtime.InteropServices.Marshal.CleanupUnusedObjectsInCurrentContext();
}
obj = null;
}
catch (Exception ex)
{
obj = null;
AddTrace("Unable to release the Object ");
AddException(ex.ToString());
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}