【问题标题】:COM Exception from HRESULT: 0x800A03EC on Excel Save FileHRESULT 的 COM 异常:Excel 保存文件上的 0x800A03EC
【发布时间】:2018-06-28 10:01:17
【问题描述】:

这困扰了我一段时间。我正在尝试将报告导出到程序中的 excel 函数。每次我导出报告并提示我命名并保存文件时,它都会抛出此错误:

COM Exception from HRESULT: 0x800A03EC

我知道在许多论坛中都提出过这个问题,但这些解决方案对我根本不起作用。我尝试重新启动 VS,以管理员身份运行 VS,更改列行号等。一点运气都没有。问题是,我在程序中有 10 个不同的函数,其中 4-5 个允许我成功保存并导出到 excel,但其他函数会抛出此错误。

这是引发此错误的代码的一部分:

private void ExportExcel(SqlDataReader dr) {
  try {
    DataTable dt = new DataTable();
    dt.Load(dr);
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "Microsoft Office Excel Workbook (*.xls)|*.xls|All Files (*.*)|*.*";
    saveFileDialog1.FilterIndex = 1;
    saveFileDialog1.RestoreDirectory = true;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
      // Create an Excel object and add workbook...
      Excel.ApplicationClass excel = new Excel.ApplicationClass();
      Excel.Workbook workbook = excel.Application.Workbooks.Add(true); // true for object template???

      // Add column headings...
      int iCol = 0;
      int iVisibleColumnCount = 0;
      foreach (DataColumn c in dt.Columns) {
        iCol++;
        // counting visible columns
        if (c.ColumnMapping != MappingType.Hidden)
          iVisibleColumnCount++;
        else    // hide the columns in excel is the column is hide in datatable
        {
          ((Excel.Range) excel.Cells[1, iCol]).EntireColumn.Hidden = true;
          continue;
        }
          // Set column header text to bold
          ((Excel.Range) excel.Cells[1, iCol]).Font.Bold = true;
        excel.Cells[1, iCol] = c.ColumnName;

        if (c.DataType == typeof(System.String))
          ((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = "@";
        else if (c.DataType == typeof(System.Int16)
            || c.DataType == typeof(System.Int32)
            || c.DataType == typeof(System.Int64))
          ((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = "#,##0";
        else if (c.DataType == typeof(System.TimeSpan))
          ((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = @"[$-409]hh:mm:ss AM/PM;@";
        else if (c.DataType == typeof(System.DateTime))
          ((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = "dd-MMM-yyyy";
        else if (c.DataType == typeof(System.Decimal))
          ((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = @"#,##0.00_);[Red](#,##0.00)";
        else
          ((Excel.Range) excel.Cells[1, iCol]).EntireColumn.NumberFormat = "General";
      }
      // for each row of data...
      int iRow = 0;
      foreach (DataRow r in dt.Rows) {
        iRow++;

        // add each row's cell data...
        iCol = 0;
        foreach (DataColumn c in dt.Columns) {
          iCol++;
          if (c.ColumnMapping != MappingType.Hidden) {
            Exception thrown at this line: excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
          }
        }
      }

      //                  // Global missing reference for objects we are not defining...
      //                  object missing = System.Reflection.Missing.Value;

      // If wanting to Save the workbook...
      workbook.SaveAs(saveFileDialog1.FileName, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
      //                  workbook.SaveAs(saveFileDialog1.FileName,
      //                      Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
      //                      false, false, Excel.XlSaveAsAccessMode.xlNoChange,
      //                      missing, missing, missing, missing, missing);

      // If wanting to make Excel visible and activate the worksheet...
      //excel.Visible = !bCloseAfterExport;
      Excel.Worksheet xlWorkSheet = (Excel.Worksheet) excel.ActiveSheet;
      ((Excel._Worksheet) xlWorkSheet).Activate();

      workbook.Save();
      //save the workbook

      //if (bCloseAfterExport)
      workbook.Close(false, String.Empty, false);
      //close the workbook

      // End

      // If wanting excel to shutdown...
      //if (bCloseAfterExport)
      ((Excel._Application) excel).Quit();
    }
  }
  catch (Exception ex) {
    MessageBox.Show(ex.ToString());
  }
}

错误信息:

任何帮助将不胜感激。

【问题讨论】:

  • 到底发生在什么时候? 2268行在哪里?
  • 在这一行抛出异常:excel.Cells[iRow + 1, iCol] = r[c.ColumnName];

标签: c# excel excel-interop


【解决方案1】:

我相信我的问题是我的一些数据(单元格值)以“=”开头,导致分配失败。当您一次执行 000 行时很难看到。将传输放在一个循环中,一次传输 10 行(也在一个 try catch 块中,以便它可以继续)。

【讨论】:

    【解决方案2】:

    excel.Cells[iRow + 1, iCol] 是一个范围。它有一个 Range 类型。你不能像这样设置值。要为 Range 设置值,您需要设置其值或公式:

    excel.Cells[iRow + 1, iCol].Value = r[c.ColumnName];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 2011-03-02
      相关资源
      最近更新 更多