我实际上是在一个月前写的,为了准备一个项目,我让它工作了。现在我得到了这个错误,无法弄清楚它被抛出的原因。我甚至创建了一个文件夹并检查了它的权限。有什么建议吗?
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);
}
}
}
感谢您的帮助!
答案 0 :(得分:0)
使用xlApp.Worksheets.Add("Sheet1")
您尝试在 Sheet1
之前添加新工作表时,它需要现有的Worksheet
对象(和0x800A03EC
}是NAME_NOT_FOUND
)。
我想你要做的是添加一个名为 Sheet1
的新工作表:
Worksheet xlWorksheet = xlApp.Worksheets.Add();
xlWorksheet.Name = "Sheet1";
现在也是xlWorksheet = (Worksheet)xlWorkbook.Worksheets.get_Item("Sheet1");
行
可以简单地放弃。