Public Shared Sub ExportExcelRpt(ByVal sqlds As DataSet)
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = Nothing
xlSheet = Nothing
xlBook = xlApp.Workbooks().Add
xlSheet = CType(xlBook.Worksheets("sheet1"), Excel.Worksheet)
'xlBook = xlApp.Workbooks.Open("c:/1.xls")
'xlSheet = CType(xlBook.Worksheets("sheet1"), Excel.Worksheet)
'Dim a As New DataSet
Dim Table As New System.Data.DataTable
Table = sqlds.Tables(0)
''?所得到的表的列名,?值??元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(1, colIndex) = Col.ColumnName
Next
''得到的表所有行,?值??元格
For Each Row In Table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName)
Next
Next
'--------
With xlSheet
.Range(.Cells(1, 1), .Cells(1, Table.Columns.Count)).Font.Name = "黑體"
'設標題為黑體字
.Range(.Cells(1, 1), .Cells(1, Table.Columns.Count)).Font.Bold = True
'標題字體加粗
.Range(.Cells(1, 1), .Cells(Table.Rows.Count + 1, Table.Columns.Count)).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
'設表格邊框樣式
End With
xlApp.Visible = True
xlApp = Nothing '"交還控制給Excel
xlBook = Nothing
xlSheet = Nothing
End Sub
End Class
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = Nothing
xlSheet = Nothing
xlBook = xlApp.Workbooks().Add
xlSheet = CType(xlBook.Worksheets("sheet1"), Excel.Worksheet)
'xlBook = xlApp.Workbooks.Open("c:/1.xls")
'xlSheet = CType(xlBook.Worksheets("sheet1"), Excel.Worksheet)
'Dim a As New DataSet
Dim Table As New System.Data.DataTable
Table = sqlds.Tables(0)
''?所得到的表的列名,?值??元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(1, colIndex) = Col.ColumnName
Next
''得到的表所有行,?值??元格
For Each Row In Table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName)
Next
Next
'--------
With xlSheet
.Range(.Cells(1, 1), .Cells(1, Table.Columns.Count)).Font.Name = "黑體"
'設標題為黑體字
.Range(.Cells(1, 1), .Cells(1, Table.Columns.Count)).Font.Bold = True
'標題字體加粗
.Range(.Cells(1, 1), .Cells(Table.Rows.Count + 1, Table.Columns.Count)).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
'設表格邊框樣式
End With
xlApp.Visible = True
xlApp = Nothing '"交還控制給Excel
xlBook = Nothing
xlSheet = Nothing
End Sub
End Class