来自HRESULT的Excelexception:0x800A03EC

我正在尝试更新一个适用于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个人电脑上试用时得到的错误是这样的。

 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(); 

我已经在线寻找解决scheme,我试图更新Microsoft.Office.Interop.Excel,我也试图删除一个registry文件夹,但没有发现,并已尝试工作。

我find了两种方法来解决这个问题: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(); 

我张贴这只是incase其他人有同样的问题。