1、首先你要先在项目中添加 owc 引用 2、执行下面的过程'导出数据到excel Public Sub GridToExcel(ByVal dt As DataTable, ByVal file_name As String) Dim xlsheet As New OWC.SpreadsheetClass Dim row As Int32 = dt.Rows.Count Dim t, k As Int32 For t = 0 To row - 1 k = 0 If Not IsDBNull(dt.Rows(t)("Col1")) Then xlsheet.ActiveSheet.Cells(t + 1, k + 1) = CStr(dt.Rows(t)("Col1")) End If k = k + 1 If Not IsDBNull(dt.Rows(t)("Col2")) Then xlsheet.ActiveSheet.Cells(t + 1, k + 1) = CStr(dt.Rows(t)("Col2")) End If k = k + 1 ................................ Next xlsheet.ActiveSheet.Export(file_name, OWC.SheetExportActionEnum.ssExportActionNone) End Sub
2、执行下面的过程'导出数据到excel
Public Sub GridToExcel(ByVal dt As DataTable, ByVal file_name As String)
Dim xlsheet As New OWC.SpreadsheetClass
Dim row As Int32 = dt.Rows.Count
Dim t, k As Int32
For t = 0 To row - 1
k = 0
If Not IsDBNull(dt.Rows(t)("Col1")) Then
xlsheet.ActiveSheet.Cells(t + 1, k + 1) = CStr(dt.Rows(t)("Col1"))
End If
k = k + 1 If Not IsDBNull(dt.Rows(t)("Col2")) Then
xlsheet.ActiveSheet.Cells(t + 1, k + 1) = CStr(dt.Rows(t)("Col2"))
End If k = k + 1
................................ Next xlsheet.ActiveSheet.Export(file_name, OWC.SheetExportActionEnum.ssExportActionNone)
End Sub