Click to See Complete Forum and Search --> : Is't possible to put "Data" in Excel Sheet or value in DBGrid on Excel Sheet


regis
February 17th, 2000, 04:37 PM
I would like to Export Table .mdb connect with Data1 to an Excel Sheet. Is't possible with VB code?

Second option I have the same data in DBGrid is't possible to put Data in DBGrid on Excel Sheet.

Thanks
Redg

Kyle Burns
February 18th, 2000, 07:31 AM
This can be accomplished fairly easily. Here's a generic routine to get you started.

public Sub ExportToExcel(rs as Recordset, FileName as string)
Dim xlApp as Excel.Application
Dim xlBook as Excel.Workbook
Dim xlSheet as Excel.Worksheet

'Instantiate your Excel Objects
set xlApp = new Excel.Application
set xlBook = xlApp.Workbooks.Add()
set xlSheet = xlBook.ActiveSheet

Dim iRow as Integer
Dim iCol as Integer

iRow = 1
iCol = 1

'print Column Headers
Dim fld as Field
for Each fld In rs.Fields
xlSheet.Cells(iRow, iCol).FormulaR1C1 = fld.Name
iCol = iCol + 1
next fld

'set The Column Back to 1 and increment the row
iCol = 1
iRow = iRow + 1

'Now get the Data
Do Until rs.EOF
for Each fld In rs.Fields
xlSheet.Cells(iRow, iCol).FormulaR1C1 = fld.Value
iCol = iCol + 1
next fld
rs.MoveNext
iCol = 1
iRow = iRow + 1
Loop
'Save the WorkBook and Clean Up
xlBook.SaveAs FileName
xlBook.Close
set xlSheet = nothing
set xlBook = nothing
set xlApp = nothing
End Sub

hokiedrum
March 29th, 2000, 10:32 AM
I used your code, but at the line that says set xlApp = new Excel.Application I get an error saying Class not defined: 'Excel' I'm trying to get this to run on the server...is that possible, or is this just a client side script? If it is client side, I run all of my Database stuff server side through ASP, so will it recognize the recordset that was retrieved by the server? Thanks...