C# 来自.NET的Excel自动化-创建新工作表
我正在尝试一项看似简单的任务:使用C#创建包含新工作表的新Excel文档 由于某种原因,我收到一个奇怪的COM错误(0x800A03EC) 有没有人能让它工作起来?有人对如何解决这个问题有什么建议吗 我已将其隔离到最少的代码中:C# 来自.NET的Excel自动化-创建新工作表,c#,excel,export-to-excel,C#,Excel,Export To Excel,我正在尝试一项看似简单的任务:使用C#创建包含新工作表的新Excel文档 由于某种原因,我收到一个奇怪的COM错误(0x800A03EC) 有没有人能让它工作起来?有人对如何解决这个问题有什么建议吗 我已将其隔离到最少的代码中: using Microsoft.Office.Interop.Excel; using System.Diagnostics; namespace ExcelAutomation { public static class ExcelTests {
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));
}
}
}
}
输出窗口现在显示:
被抓住的例外。消息:“来自HRESULT:0x800A03EC的异常”我犯的错误是对我不想设置的可选值使用null 相反,我应该使用
我想这对你有帮助
访问仅仅发布一个链接到一个大的文本页面并不算是一个答案。
Worksheet workSheet = (Worksheet)newWorkbook.Sheets.Add
(
existingWorksheet, // before
System.Reflection.Missing.Value,
System.Reflection.Missing.Value, // 1,
System.Reflection.Missing.Value //XlSheetType.xlWorksheet
);