1Public Function DataSetToExcel(ByVal dtSource As System.Data.DataSet, ByVal sFileName As String) As Boolean
2
3 'Better way to do it. Not using it at this time.
4 Dim iRowCount As Integer = dtSource.Tables(0).Rows.Count
5 Dim iColCount As Integer = dtSource.Tables(0).Columns.Count
6 Dim oData(iRowCount, iColCount) As Object
7 Dim iRow As Integer, iCol As Integer
8 For iRow = 0 To iRowCount - 1
9 For iCol = 0 To iColCount - 1
10 oData(iRow, iCol) = dtSource.Tables(0).Rows(iRow).Item(iCol)
11 Next
12 Next
13
14 ' Start Excel and get Application object
15 Dim oExcel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
16 oExcel.Visible = True ' Make visible
17
18 ' Get a new workbook
19 Dim oBook As Microsoft.Office.Interop.Excel.Workbook = CType(oExcel.Workbooks.Add(), Microsoft.Office.Interop.Excel.Workbook)
20 Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(oBook.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)
21
22 Dim oRange As Microsoft.Office.Interop.Excel.Range = oSheet.Range("A2")
23 oRange = oRange.Resize(iRowCount, iColCount)
24 oRange.Value = oData
25
26 ' oSheet.SaveAs(sFileName)
27 ' oExcel.Workbooks.Close()
28 ' oExcel.Quit()
29
30 oBook = Nothing
31 oSheet = Nothing
32 oExcel = Nothing
33 End Function
34
35