Why can't Visual Basic convert my datatable into an excel datatable?

0

I'm trying to make a program with a local database where I can have an overview for our company savings. Nothing really special. Anyway, I want to export the dataset to an excel file when I click on the export button. This is my code

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

Whem I start DeBugging I catch the following error:

COMException was unhandled Old format or invalid type library. (Exception of HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

Furthermore, as you can see, i set xl as Microsoft.Office.Interop.Excel.Application instead of using Imports Microsoft.interop.excel on top. When I do this I get the error that my datatable couldn't be converted into an excel datatable.

vb.net
excel
datatable
interop
export
asked on Stack Overflow Nov 28, 2014 by Dusa SFRJ

1 Answer

0

I use this function and it works perfect:

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
answered on Stack Overflow Nov 28, 2014 by ruedi

User contributions licensed under CC BY-SA 3.0