【问题标题】:Why can't Visual Basic convert my datatable into an excel datatable?为什么 Visual Basic 不能将我的数据表转换为 excel 数据表?
【发布时间】:2015-01-26 19:46:39
【问题描述】:

我正在尝试使用本地数据库制作一个程序,我可以在其中概述我们公司的节省。没什么特别的。无论如何,当我单击导出按钮时,我想将数据集导出到 Excel 文件。这是我的代码

Dim dt As DataTable

    Dim xl As New Microsoft.Office.Interop.Excel.Application
    Dim ds As New DataSet
    ds = GeldopslagDataSet
    xl.UserControl = True
    Dim oldCI As System.Globalization.CultureInfo = _
        System.Threading.Thread.CurrentThread.CurrentCulture
    System.Threading.Thread.CurrentThread.CurrentCulture = _
        New System.Globalization.CultureInfo("en-US")
    xl.Workbooks.Add()
    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI
    xl.ActiveSheet.Name = "Spaardoel"
    xl.Visible = True
    xl.Range("A1").Value = "Loading the DataSet...."

    Try
        xl.ScreenUpdating = False

        dt = ds.Tables("Geldopslag")

        'Add the column headings for Geldopslag
        Dim dc As DataColumn
        Dim iCols As Int32 = 0
        For Each dc In dt.Columns
            xl.Range("A1").Offset(0, iCols).Value = dc.ColumnName
            iCols += 1
        Next
        'Add the data
        Dim iRows As Int32
        For iRows = 0 To dt.Rows.Count - 1
            xl.Range("A2").Offset(iRows).Resize(1, iCols).Value = _
              dt.Rows(iRows).ItemArray()
        Next
    Catch ex As Exception
    Finally
        xl.ScreenUpdating = True
    End Try

    'Make the sheet pretty
    With xl.ActiveSheet.Range("A1")
        .AutoFilter()
        .AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatSimple)
    End With

    xl = Nothing

我开始调试时发现以下错误:

COMException 未处理 旧格式或无效类型库。 (HRESULT 异常:0x80028018 (TYPE_E_INVDATAREAD))

此外,如您所见,我将 xl 设置为 Microsoft.Office.Interop.Excel.Application 而不是在顶部使用 Imports Microsoft.interop.excel。当我这样做时,我得到了我的数据表无法转换为 excel 数据表的错误。

【问题讨论】:

  • sn-p 解决了您的问题吗?如果是这样,请将其标记为已解决,否则请评论有什么问题。

标签: vb.net excel datatable interop export


【解决方案1】:

我使用了这个功能,效果很好:

Imports Microsoft.Office.Interop

Public Class ExcelTools

' Export a DataTable into an Excel Datasheet
Public Shared Function DatatableToExcel(ByVal aDataTable As DataTable) As Boolean
    Dim myExcel As New Microsoft.Office.Interop.Excel.Application
    Try
        ' Excel.Application
        myExcel.Application.Workbooks.Add()
        myExcel.Visible = True
        Dim myColumn As DataColumn
        Dim colIndex As Integer
        Dim rowIndex As Integer
        For Each myColumn In aDataTable.Columns
            colIndex += 1
            myExcel.Cells(1, colIndex) = myColumn.ColumnName
        Next

        Dim myRow As DataRow
        rowIndex = 1
        For Each myRow In aDataTable.Rows
            rowIndex += 1
            colIndex = 0
            Dim myColumn2 As DataColumn
            For Each myColumn2 In aDataTable.Columns
                colIndex += 1
                myExcel.Cells(rowIndex, colIndex) = myRow( _
                  myColumn2.ColumnName)
            Next myColumn2
        Next myRow
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    myExcel.Quit()

End Function
end class

【讨论】:

  • 谢谢鲁迪!但我的问题还是一样。我似乎错过了 excel 应用程序的库文件。它无法添加新工作簿。我不知道我需要什么 dll 文件来启用应用程序驱动的订单以在 Excel 中添加新工作簿
  • 您是否在为您的项目添加引用下设置了对 microsoft excel 14.0 的引用?
猜你喜欢
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 2022-07-26
相关资源
最近更新 更多