断开的上下文Excel Com对象

时间:2013-02-26 13:32:32

标签: excel comobject disconnected

我有一个多线程的应用程序。申请的一个属性是报告。

用户可以每天获取一年的数据

我获取数据库,将结果输入List。然后使用Excel(作为com对象)。

打开Excel并开始从列表中添加单元格值。

虽然这些过程突然发生,但我得到了这样的信息:

The Error

以下是我的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();
            }
        }

1 个答案:

答案 0 :(得分:0)

无需使用COM对象进行报告。我通过不使用com对象来使用excel进行报告来解决了我的问题。

有不同的报告和使用excel的工具

  1. Open XML SDK:http://www.microsoft.com/en-us/download/details.aspx?id=5124
  2. 文档http://msdn.microsoft.com/en-us/library/bb491088(v=office.14).aspx

    1. 如果Open XML SDK语法很难,那么您可以使用:http://spreadsheetlight.com/
    2. 文档非常好,Vincent(http://spreadsheetlight.com/about/)对您的问题非常有帮助。

      1. 如果您的应用程序不需要某种图表,那么您可以使用:http://closedxml.codeplex.com/
      2. spreadsheetlight和Closed XML都基于Open XML SDK,根据我的经验,我没有任何类型的断开连接的contex问题。

        如果您需要保护您的Excel文件,可以使用:http://dotnetzip.codeplex.com/ 您有一个受密码保护的压缩xlsx文件。

        因此,您不需要在计算机中使用COM对象,互操作服务和Excel.Exe。

        我希望这些建议对您有所帮助,并感谢所有开发SpreadSheetLight和Closed XML以及Open XML的人