Excel创建工作表4,错误:DISP_E_BADINDEX

问题描述:

我正在尝试创建名为AutoIntegration的sheet4,但它显示错误:DISP_E_BADINDEX,其工作正常至Sheet3,但在sheet4失败



请帮助





如果dlgSave.ShowDialog = Windows.Forms.DialogResult.OK那么

试试


xlApp =新的Microsoft.Office.Interop.Excel.Application

xlBook = xlApp.Workbooks.Add(oValue)



'##############出口到SHEET1 #################### ######



Dim xlRow As Long = 2

Dim xlCol As Short = 1

xlSheet = xlBook.Worksheets(Sheet1)

xlSheet.Name =General



For each col As DataColumn in dtgl.Columns

xlSheet.Cells(1,xlCol)= col.ColumnName

xlCol + = 1

下一页

每行作为DataRow在dtgl.Rows

xlCol = 1

For每个col As DataColumn In dtgl.Columns

xlSheet.Cells(xlRow,xlCol)= row(xlCol - 1)

xlCol + = 1

下一页

xlRow + = 1

下一页

'##############出口到SHEET2 ##########################

Dim xlRow2 As Long = 2

Dim xlCol2 Short = 1

xlSheet = xlBook.Worksheets(Sheet2)

xlSheet.Name =Transport



For each col As DataColumn in dttt.Columns

xlSheet.Cells(1,xlCol2)= col.ColumnName

xlCol2 + = 1

N.分钟



For Each row As DataRow in dttt.Rows

xlCol2 = 1

For each col As DataColumn在dttt.Columns中

xlSheet.Cells(xlRow2,xlCol2)= row(xlCol2 - 1)

xlCol2 + = 1

下一页

xlRow2 + = 1

下一页



'############## #EXPORT TO SHEET3 ##########################

Dim xlRow3 As Long = 2

Dim xlCol3 As Short = 1

xlSheet = xlBook.Worksheets(Sheet3)

xlSheet.Name =Radio



For each col As DataColumn in dtr.Columns

xlSheet.Cells(1,xlCol3)= col.ColumnName

xlCol3 + = 1

下一页



For Each Row As DataRow in dtr.Rows

xlCol3 = 1

For each col As DataColumn in dtr.Columns

xlSheet.Cells(xlRow3,xlCol3)= row(xlCol3 - 1)

xlCol3 + = 1

下一页

xlRow3 + = 1

下一页



'##############出口到SHEET4 ####### ###################



Dim xlRow4 As Long = 2

Dim xlCol4 As Short = 1

xlSheet = xlBook.Worksheets(Sheet4)

xlSheet.Name =AutoIntegrate



For each col As DataColumn in dtau.Columns

xlSheet.Cells(1,xlCol4)= col.ColumnName

xlCol4 + = 1

下一页


For Each Row As DataRow in dtau.Rows

xlCol4 = 1

For each col As DataColumn in dtau.Columns

xlSheet.Cells(xlRow4,xlCol4)= row(xlCol4 - 1)

xlCol4 + = 1

下一页

xlRow4 + = 1

下一步

I'm trying to create sheet4 with name as "AutoIntegration" but it showing error:" DISP_E_BADINDEX", its working fine upto sheet3, but failing at sheet4

Please help


If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
Try

xlApp = New Microsoft.Office.Interop.Excel.Application
xlBook = xlApp.Workbooks.Add(oValue)

'############### EXPORT TO SHEET1 ##########################

Dim xlRow As Long = 2
Dim xlCol As Short = 1
xlSheet = xlBook.Worksheets("Sheet1")
xlSheet.Name = "General"

For Each col As DataColumn In dtgl.Columns
xlSheet.Cells(1, xlCol) = col.ColumnName
xlCol += 1
Next
For Each row As DataRow In dtgl.Rows
xlCol = 1
For Each col As DataColumn In dtgl.Columns
xlSheet.Cells(xlRow, xlCol) = row(xlCol - 1)
xlCol += 1
Next
xlRow += 1
Next
'############### EXPORT TO SHEET2 ##########################
Dim xlRow2 As Long = 2
Dim xlCol2 As Short = 1
xlSheet = xlBook.Worksheets("Sheet2")
xlSheet.Name = "Transport"

For Each col As DataColumn In dttt.Columns
xlSheet.Cells(1, xlCol2) = col.ColumnName
xlCol2 += 1
Next

For Each row As DataRow In dttt.Rows
xlCol2 = 1
For Each col As DataColumn In dttt.Columns
xlSheet.Cells(xlRow2, xlCol2) = row(xlCol2 - 1)
xlCol2 += 1
Next
xlRow2 += 1
Next

'############### EXPORT TO SHEET3 ##########################
Dim xlRow3 As Long = 2
Dim xlCol3 As Short = 1
xlSheet = xlBook.Worksheets("Sheet3")
xlSheet.Name = "Radio"

For Each col As DataColumn In dtr.Columns
xlSheet.Cells(1, xlCol3) = col.ColumnName
xlCol3 += 1
Next

For Each row As DataRow In dtr.Rows
xlCol3 = 1
For Each col As DataColumn In dtr.Columns
xlSheet.Cells(xlRow3, xlCol3) = row(xlCol3 - 1)
xlCol3 += 1
Next
xlRow3 += 1
Next

'############### EXPORT TO SHEET4 ##########################

Dim xlRow4 As Long = 2
Dim xlCol4 As Short = 1
xlSheet = xlBook.Worksheets("Sheet4")
xlSheet.Name = "AutoIntegrate"

For Each col As DataColumn In dtau.Columns
xlSheet.Cells(1, xlCol4) = col.ColumnName
xlCol4 += 1
Next

For Each row As DataRow In dtau.Rows
xlCol4 = 1
For Each col As DataColumn In dtau.Columns
xlSheet.Cells(xlRow4, xlCol4) = row(xlCol4 - 1)
xlCol4 += 1
Next
xlRow4 += 1
Next

您可以使用开放式XML而不是使用互操作库来使用通用解决方案。

http://exportaspnetlistdatatoexcelworkbook.blogspot.com/
You can use a generic solution by using open XML instead of using interop library.
http://exportaspnetlistdatatoexcelworkbook.blogspot.com/


尝试使用以下代码

try with below code
xlApp = New Microsoft.Office.Interop.Excel.Application
Dim iSheetsPerBook As Integer
iSheetsPerBook = xlApp.SheetsInNewWorkbook
xlApp.SheetsInNewWorkbook = 4
xlBook = xlApp.Workbooks.Add
xlApp.SheetsInNewWorkbook = iSheetsPerBook