代码如下:
Public Function ExportToExcel(ByVal FName As String, ByVal DTable As DataTable) As Boolean
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
Dim i, count, j As Integer
On Error GoTo HandleErr
rowIndex = 1
colIndex = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
'jasf;lkad
xlApp.Cells.Select()
xlApp.Selection.NumberFormatLocal = "@"
'kajsfa
For i = 1 To DTable.Columns.Count
xlApp.Cells(rowIndex, i) = DTable.Columns(i - 1).Caption
Next
count = DTable.Rows.Count
If count > 0 Then
For j = 0 To count - 1
rowIndex = rowIndex + 1
For i = 1 To DTable.Columns.Count
xlApp.Cells(rowIndex, i) = DTable.Rows(j)(i - 1)
Next i
Next j
xlBook.SaveAs(FileName:=FName)
xlApp.Quit()
Else
MsgBox("没有数据,无法导出")
End If
Return True
Exit Function
HandleErr:
MsgBox(Err.Description)
Return False
End Function