【发布时间】:2013-05-23 15:13:04
【问题描述】:
我正在尝试一项看似简单的任务:使用 C# 创建一个包含新工作表的新 Excel 文档。
由于某种原因,我收到了一个奇怪的 COM 错误 (0x800A03EC)
有没有人设法让它工作?有人对如何解决此问题有建议吗?
我已将其隔离为最少的代码:
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
namespace ExcelAutomation
{
public static class ExcelTests
{
public static void CreateWorksheet()
{
try
{
var app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = true;
var workBooks = app.Workbooks;
var newWorkbook = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet existingWorksheet = (Worksheet)newWorkbook.Sheets[1];
Worksheet workSheet = (Worksheet)newWorkbook.Sheets.Add
(
null, // before
existingWorksheet,
null, // 1,
null //XlSheetType.xlWorksheet
);
}
catch (System.Runtime.InteropServices.COMException ex)
{
Trace.WriteLine(string.Format("Caught COMException. Message: \"{0}\"", ex.Message));
}
}
}
}
输出窗口现在显示:
捕获 COMException。消息:“HRESULT 异常:0x800A03EC”
【问题讨论】:
标签: c# excel export-to-excel