Hi
I' also trying to export data from a sql database to a Excel file but i still cant get there... i've tied diferent ways but i think the most (nearly) correct is this one!! but the sentence inside the double cycle isn't correct!! Please somebody help me!!
here his the code
Sub createFile()
Dim dsExcelExport As New dSet
Dim daExcelExport As SqlClient.SqlDataAdapter
Dim Excel As New Excel.Application
Dim intColumn, intRow, intColumnValue As Integer
Dim strExcelFile As String
Dim strFileName As String
Dim dt As New DataTable 'define new data table for temporary strorage
daExcelExport = New SqlClient.SqlDataAdapter("SELECT * FROM tBjf(nolock)", cnn)
daExcelExport.Fill(dsExcelExport)
dt = DS.Tables("tBjf")
Dim strAppPath = System.Reflection.Assembly.GetExecutingAssembly.Location.Substring(0, System.Reflection.Assembly.GetExecutingAssembly.Location.LastIndexOf("\") + 1)
With Excel
.SheetsInNewWorkbook = 3
.Workbooks.Add()
.Worksheets(1).Select()
'For displaying the column name in the the excel file.
For intColumn = 0 To dsExcelExport.Tables(0).Columns.Count - 1
.Cells(1, intColumn + 1).Value = dsExcelExport.Tables(0).Columns(intColumn).ColumnName.ToString
Next
'For displaying the column value row-by-row in the the excel file.
For intRow = 1 To dsExcelExport.tBjf.Rows.Count - 1
For intColumnValue = 1 To dsExcelExport.tBjf.Columns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value = dsExcelExport.Tables(0).Rows
Next
Next
'i think the error is inside the cycle above
'save Excel File
strFileName = InputBox("Please enter the file name.", "FOCUS")
strExcelFile = strAppPath & strFileName
.ActiveWorkbook().SaveAs(strExcelFile)
.ActiveWorkbook.Close()
End With
MessageBox.Show("File exported sucessfully.", "Exporting done", MessageBoxButtons.OK, MessageBoxIcon.Information)
NormalExit:
Excel.Quit()
Excel = Nothing
GC.Collect()
Exit Sub
End Sub
this is a exemple that i found into he internet! but i still can't get it how to write thi in the correct way to fill up the file with the data from databank
Thanks for your help, and your time!!!