【问题标题】:Excel Automation From .NET - creating a new worksheet.NET 中的 Excel 自动化 - 创建新工作表
【发布时间】: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


    【解决方案1】:

    我犯的错误是将 null 用于我不想设置的可选值。

    我应该使用System.Reflection.Missing.Value

            Worksheet workSheet = (Worksheet)newWorkbook.Sheets.Add
                    (
                        existingWorksheet, // before
                        System.Reflection.Missing.Value,
                        System.Reflection.Missing.Value, // 1,
                        System.Reflection.Missing.Value //XlSheetType.xlWorksheet
                    );
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 仅仅发布一个指向大页面文本的链接不算作答案。
      猜你喜欢
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      相关资源
      最近更新 更多