"Exception from HRESULT: 0x800A03EC" 创建新工作表

"Exception from HRESULT: 0x800A03EC" creating new worksheet

我实际上是在一个多月前写这篇文章来准备一个项目的,而且我已经开始工作了。现在我得到这个错误并且无法弄清楚为什么它被抛出。我什至创建了一个文件夹并检查了它的权限。有什么建议吗?

namespace E_Report
{
    class Program
    {
        static void Main(string[] args)
        {
            Application xlApp = new Application();
            Workbook xlWorkbook = xlApp.Workbooks.Add("Report.xlsx");
            Worksheet xlWorksheet = xlApp.Worksheets.Add("Sheet1"); // Exception from HRESULT: 0x800A03EC

            xlWorksheet = (Worksheet)xlWorkbook.Worksheets.get_Item("Sheet1");

            xlWorksheet.Cells[1, 1] = "Account Number";
            xlWorksheet.Cells[1, 2] = "Amount";
            xlWorksheet.Cells[1, 3] = "Code";
            xlWorksheet.Cells[1, 4] = "Date";
            xlWorksheet.Cells[1, 5] = "Audit";
            xlWorksheet.Cells[1, 6] = "ID";
            xlWorksheet.Cells[1, 7] = "Customer Name";
            xlWorksheet.Cells[1, 8] = "Payment Source";

            xlWorkbook.SaveAs("C:\Temp\Report.xlsx");

            xlApp.Quit();

            xlWorkbook.Close(0);
        }
    }
}

感谢您的帮助!

对于 xlApp.Worksheets.Add("Sheet1"),您正在尝试 Sheet1 之前添加新作品sheet,它需要一个现有的 Worksheet 对象(0x800A03ECNAME_NOT_FOUND)。

我猜你想做的是添加一个 new sheet named Sheet1:

 Worksheet xlWorksheet = xlApp.Worksheets.Add();
xlWorksheet.Name = "Sheet1";

现在还有行xlWorksheet = (Worksheet)xlWorkbook.Worksheets.get_Item("Sheet1"); 可以简单地删除。