【发布时间】:2015-07-17 21:04:10
【问题描述】:
我正在尝试更新适用于 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,还尝试删除注册表文件夹,但我发现并尝试过的都没有奏效。
【问题讨论】: