注:DataGrid 中的数据是通过ADO.NET编程得到的数据集,并已填充到DataGrid 中,
急,,,,,,,,,,
Public Sub EduceToExcel2(ByVal StrTabl As String, Optional ByVal TatalName As String = "", Optional ByVal CaptionName As String = "") '导出Excel
Dim App As New Excel.Application
Dim Ws As New Excel.Worksheet
Dim StrValue As String
Dim i, j, a As Integer
Ws = App.Workbooks.Add.Worksheets(1)
App.Visible = True
Try
Dim Trow As DataRow = dtset.Tables(StrTabl).NewRow
For i = 0 To dtset.Tables(StrTabl).Columns.Count - 1
Ws.Cells(1, i + 1) = dtset.Tables(StrTabl).Columns.Item(i).ColumnName
Next
i = 2
For a = 0 To dtset.Tables(StrTabl).Rows.Count - 1
For j = 0 To dtset.Tables(StrTabl).Columns.Count - 1
StrValue = dtset.Tables(StrTabl).Rows(a).Item(j)
Ws.Cells(i, j + 1) = StrValue
Next
i = i + 1
Next
Ws.Columns.AutoFit()
If TatalName <> "" Then Ws.PageSetup.CenterHeader = TatalName
If CaptionName <> "" Then App.Caption = CaptionName '工作薄标题
Ws.PageSetup.PrintGridlines = True
Ws.PageSetup.CenterHorizontally = True
Catch ex As Exception
MsgBox(ex.Message)
Finally
Dtread.Close()
End Try
End Sub