Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 为什么可以';Visual Basic是否将我的数据表转换为excel数据表?_Vb.net_Excel_Datatable_Interop_Export - Fatal编程技术网

Vb.net 为什么可以';Visual Basic是否将我的数据表转换为excel数据表?

Vb.net 为什么可以';Visual Basic是否将我的数据表转换为excel数据表?,vb.net,excel,datatable,interop,export,Vb.net,Excel,Datatable,Interop,Export,我正在尝试制作一个带有本地数据库的程序,在那里我可以对我们公司的储蓄进行概述。没什么特别的。无论如何,我想在单击“导出”按钮时将数据集导出到excel文件。这是我的密码 Dim dt As DataTable Dim xl As New Microsoft.Office.Interop.Excel.Application Dim ds As New DataSet ds = GeldopslagDataSet xl.UserControl = True

我正在尝试制作一个带有本地数据库的程序,在那里我可以对我们公司的储蓄进行概述。没什么特别的。无论如何,我想在单击“导出”按钮时将数据集导出到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(类型_E_INVDATAREAD))


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

我使用此函数,它工作正常:

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

这个片段解决了你的问题吗?如果是这样,请将其标记为“解决”,否则请评论问题所在。谢谢ruedi!但我的问题还是一样。我似乎错过了excel应用程序的库文件。无法添加新工作簿。我不知道启用应用程序驱动命令以在excel中添加新工作簿所需的dll文件xou是否在项目的“添加引用”下设置了对microsoft excel 14.0的引用?