【发布时间】:2017-02-04 01:45:40
【问题描述】:
我正在尝试在 Excel 中创建一个包含多个工作表的Workbook,但我不知道如何创建多个工作表。我可以创建一个就好了,但是当我尝试创建第二个来写入时,我得到了一个错误。
Dim app As Application = New Application
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim newXlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application
Dim newXlWorkbook As Excel.Workbook
Dim newXlSheet As Excel.Worksheet
Dim newXlSheet2 As Excel.Worksheet
Public Sub createWorkBook()
newXlWorkbook = newXlApp.Workbooks.Add()
newXlSheet = newXlWorkbook.Sheets("Sheet1")
newXlSheet2 = newXlWorkbook.Sheets.Add("Sheet2")
newXlSheet.Cells(1, 1) = "Task ID"
newXlSheet.Cells(1, 2) = "Collective Tasks"
newXlSheet.Cells(1, 3) = "Supported Task"
newXlSheet2.Cells(1, 1) = "Parent Collective Task"
newXlSheet2.Cells(1, 2) = "Individual Task"
End Sub
我不确定它是否重要,但我还有一个单独的 Excel Workbook 打开我正在查询。
【问题讨论】:
-
哪一行错误?您似乎只在代码中添加了一张工作表。
-
@sjr
newXlSheet2 = newXlWorkbook.Sheets.Add("Sheet2")抛出An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred -
认为错误是“System.Runtime.InteropServices.COMException”类型的第一次机会异常。如果他们有同样的错误,任何人都可以参考这个问题。
标签: excel vb.net excel-interop