Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export to Excel

Status
Not open for further replies.

brews

Technical User
Dec 12, 2007
194
US
Trying to export fields to excel. Have set the references to Excel 12, But, the i, j loop cuts short the number of fields to export to 10 the first time and then exports all 19 which results in an Excel file with duplicates.
Code:
Imports Excel = Microsoft.Office.Interop.Excel.
Dim i As Integer
Dim j As Integer

Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim sFileName As String = "tmp.xls"
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & Application.StartupPath & "\temp.mdb")
command = New OleDbCommand("SELECT * FROM Members", conn)
da.SelectCommand = command
da.Fill(ds, "Members")
'get active worksheet
xlWorkSheet = DirectCast(xlWorkBook.ActiveSheet, Excel.Worksheet)
xlWorkSheet.Name = "Members"
For i = 0 To ds.Tables(0).Rows.Count - 1
	For j = 0 To ds.Tables(0).Columns.Count - 1
		xlWorkSheet.Cells(i + 1, j + 1) = ds.Tables(0).Rows(i).Item(j)
	Next
Next
Any ideas would be great. Thanks.
 
Found the reason for the dupes. Now I would like to add headers to the Excel file. thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top