【解決方法】Excel 作成シート 4、エラー: DISP_E_BADINDEX

プログラミングQA


「AutoIntegration」という名前のシート4を作成しようとしていますが、エラーが表示されます:「DISP_E_BADINDEX」、シート3までは正常に機能しますが、シート4で失敗します

助けてください

dlgSave.ShowDialog = Windows.Forms.DialogResult.OK の場合
試す

xlApp = 新しい Microsoft.Office.Interop.Excel.Application
xlBook = xlApp.Workbooks.Add(oValue)

‘################ シート 1 にエクスポート ######################################

xlRow As Long = 2 を暗くする
Dim xlCol As Short = 1
xlSheet = xlBook.Worksheets(“Sheet1”)
xlSheet.Name = “一般”

dtgl.Columns の DataColumn として列ごとに
xlSheet.Cells(1, xlCol) = col.ColumnName
xlCol += 1

For Each row As DataRow In dtgl.Rows
xlCol = 1
dtgl.Columns の DataColumn として列ごとに
xlSheet.Cells(xlRow, xlCol) = 行(xlCol – 1)
xlCol += 1

xlRow += 1

‘################ シート 2 にエクスポート ######################################
Dim xlRow2 As Long = 2
Dim xlCol2 As Short = 1
xlSheet = xlBook.Worksheets(“Sheet2”)
xlSheet.Name = “トランスポート”

dttt.Columns の DataColumn として列ごとに
xlSheet.Cells(1, xlCol2) = col.ColumnName
xlCol2 += 1

For Each row As DataRow In dttt.Rows
xlCol2 = 1
dttt.Columns の DataColumn として列ごとに
xlSheet.Cells(xlRow2, xlCol2) = 行(xlCol2 – 1)
xlCol2 += 1

xlRow2 += 1

‘################ シート 3 にエクスポート ######################################
Dim xlRow3 As Long = 2
Dim xlCol3 As Short = 1
xlSheet = xlBook.Worksheets(“Sheet3”)
xlSheet.Name = “ラジオ”

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) = 行(xlCol3 – 1)
xlCol3 += 1

xlRow3 += 1

‘################ シート 4 にエクスポート ######################################

Dim xlRow4 As Long = 2
Dim xlCol4 As Short = 1
xlSheet = xlBook.Worksheets(“Sheet4”)
xlSheet.Name = “自動統合”

dtau.Columns の DataColumn として列ごとに
xlSheet.Cells(1, xlCol4) = col.ColumnName
xlCol4 += 1

For Each row As DataRow In dtau.Rows
xlCol4 = 1
dtau.Columns の DataColumn として列ごとに
xlSheet.Cells(xlRow4, xlCol4) = 行(xlCol4 – 1)
xlCol4 += 1

xlRow4 += 1

解決策 2

以下のコードで試してください

VB
xlApp = New Microsoft.Office.Interop.Excel.Application
Dim iSheetsPerBook As Integer
iSheetsPerBook = xlApp.SheetsInNewWorkbook
xlApp.SheetsInNewWorkbook = 4
xlBook = xlApp.Workbooks.Add
xlApp.SheetsInNewWorkbook = iSheetsPerBook

解決策 1

相互運用ライブラリを使用する代わりに、オープン XML を使用して汎用解決策を使用できます。

コメント

タイトルとURLをコピーしました