[Visual Basic] Public Shared Function DatatableToExcel( _
ByVal aDataTable As DataTable, _
ByVal aOutputFilename As String _
) As Boolean
[Visual Basic] Public Shared Function DatatableToExcel(ByVal aDataTable As DataTable, ByVal aOutputFilename As String) As Boolean
Application.DoEvents()
Dim f As New FrmExcelExport
Dim app As New Object
Dim wb As New Object
Dim ws As New Object
Try
f.Show()
Application.DoEvents()
f.ProgressBar1.Minimum = 0
f.ProgressBar1.Maximum = aDataTable.Rows.Count
app = CreateObject("Excel.application")
'Dim wb As Object
'Dim ws As Object
' Dim app As New Excel.ApplicationClass
' Dim wb As Excel.Workbook
' Dim ws As Excel.Worksheet
wb = app.Workbooks.add()
'wb = app.Workbooks.Add()
ws = wb.ActiveSheet()
Dim dc As DataColumn
Dim dr As DataRow
Dim colIndex As Integer
Dim rowIndex As Integer
' Columns erstellen
For Each dc In aDataTable.Columns
colIndex += 1
app.Cells(1, colIndex) = dc.ColumnName
Next
' Rows hinzufügen
For Each dr In aDataTable.Rows
f.ProgressBar1.Value = rowIndex
rowIndex += 1
colIndex = 0
For Each dc In aDataTable.Columns
colIndex += 1
app.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
ws.Columns.AutoFit()
wb.SaveAs(aOutputFilename)
app.Workbooks.Open(aOutputFilename)
' Excel anzeigen wenn fertig exportiert
app.Visible = True
ws = Nothing
wb = Nothing
app = Nothing
f.Close()
Return True
Catch ex As Exception
f.Close()
MsgBox(ex.Message)
End Try
End Function
Plattformen: Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition