我有一个多线程应用程序。应用程序的属性之一是报告。
用户可以每天获取一年的数据
我获取数据库,将结果放到列表中。然后使用 Excel(作为 com 对象)。
打开 Excel 并开始从列表中添加单元格值。
当这些过程突然发生时,我收到以下消息:
这是我的excel报告代码:
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();
}
}