来自HRESULT的Excel异常:0x800A03EC

时间:2015-07-17 21:04:10

标签: c# excel

我正在尝试更新一个适用于Office 2010的程序,但在2013年会出错。

我正在运行的代码是:

private static bool writeToExcel(DataTable dT)
{
    // Create a new Excel document
    Console.Write("Opening Excel...");
    Excel.Application objExcel = new Excel.Application();
    objExcel.Visible = false;
    Excel.Workbooks objBooks = objExcel.Workbooks;
    Excel.Workbook objBook = objBooks.Add();
    Excel.Worksheet objSheet = (Excel.Worksheet)objExcel.Worksheets["Sheet1"]; objSheet.Name = "Data";
    Excel.Worksheet newWorksheet;
    newWorksheet = (Excel.Worksheet)objExcel.Worksheets.Add();
    Excel.Worksheet objSheet2 = (Excel.Worksheet)objExcel.Worksheets["Sheet2"]; objSheet2.Name = "Data2";
    Excel.Range objRange;

    objSheet2.Cells[2, 1] = "###";
    objSheet2.Cells[3, 1] = "@@@";
    objSheet2.Cells[4, 1] = "$$$";

    objSheet2.Cells[1, 2] = "%%%";
    objSheet2.Cells[2, 2] = %%%Count;
    objSheet2.Cells[3, 2] = %%%RespCount;
    objSheet2.Cells[4, 2] = Math.Round(%%%RespCount / %%%Count, 1);

    objSheet2.Cells[1, 3] = "^^^";
    objSheet2.Cells[2, 3] = ^^^Count;
    objSheet2.Cells[3, 3] = ^^^RespCount;
    objSheet2.Cells[4, 3] = Math.Round(^^^Count / ^^^RespCount, 1);

    objSheet2.Cells[1, 5] = "&&&";
    objSheet2.Cells[2, 5] = &&&;
    objSheet2.Cells[3, 5] = &&&Resp;
    objSheet2.Cells[4, 5] = Math.Round(&&& / &&&Resp, 1);

    objSheet2.Cells[1, 6] = "***";
    objSheet2.Cells[2, 6] = ***;
    objSheet2.Cells[3, 6] = ***Resp;
    objSheet2.Cells[4, 6] = Math.Round(*** / ***Resp, 1);


    // Add the column headers
    int colCount = 1;
    int rowCount = 1;
    foreach (DataColumn column in dT.Columns)
    {
        Console.Write(".");
        objSheet.Cells[rowCount, colCount] = DB.GetColumnName(column.ColumnName);
        if (column.DataType == System.Type.GetType("System.String"))
        {
            objRange = objExcel.Range[objExcel.Cells[2, colCount], objExcel.Cells[dT.Rows.Count + 1, colCount]];
            objRange.NumberFormat = "@";
        }
        if (column.DataType == System.Type.GetType("System.DateTime"))
        {
            objRange = objExcel.Range[objExcel.Cells[2, colCount], objExcel.Cells[dT.Rows.Count + 1, colCount]];
            objRange.NumberFormat = "m/d/yy h:mm AM/PM";
        }
        colCount++;
    }

    // Format the column headers
    Console.WriteLine();
    Console.Write("Adding columns...");

    try
    {
        objSheet.Range[objExcel.Cells[rowCount + 1, 1], objExcel.Cells[rowCount + 1, dT.Columns.Count]].Select();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}

我在运行Office 2013的Win 7 PC上尝试时遇到的错误就是这个。

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC 
at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, 
   ExcepInfo& excepInfo, UInt32 argErr, String message)
at CallSite.Target(Closure , CallSite , Object , Object , Object )   
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at CscSurveyReporter.Program.writeToExcel(DataTable dT) in c:\tfs_csc\csc\CscSurveyReporter\CscSurveyReporter\Program.cs:line 236

我遇到例外的一行是:

objSheet.Range[objExcel.Cells[rowCount + 1, 1], objExcel.Cells[rowCount + 1, dT.Columns.Count]].Select();

我已经在网上寻找解决方案,我试图更新Microsoft.Office.Interop.Excel,我也试图删除一个注册表文件夹,但我找不到任何东西并尝试过。

1 个答案:

答案 0 :(得分:0)

我找到了两种解决问题的方法: 1)

                int column = 0, row = 0;
            foreach (DataColumn col in dT.Columns)
            {
                objExcel.Cells[1, ++column] = col.ColumnName;
            }
            foreach (DataRow r in dT.Rows)
            {
                row++;
                column = 0;
                foreach (DataColumn c in dT.Columns)
                {
                    objExcel.Cells[row + 1, ++column] = r[c.ColumnName];
                }
            }

这有效,但增加了应用程序的运行时间。

在我开始工作之后,我更多地查看了错误的行并将其更改为:

objSheet.Range[objExcel.Cells[rowCount + 1, 1], objExcel.Cells[rowCount + 1, dT.Columns.Count]].Select();

这也是:

objSheet.Range[objSheet.Cells[rowCount + 1, 1], objSheet.Cells[rowCount + 1, dT.Columns.Count]].Select();

我发布这个只是因为其他人有同样的问题。