CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 1999
    Location
    Québec (Canada)
    Posts
    210

    Is't possible to put "Data" in Excel Sheet or value in DBGrid on Excel Sheet

    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


  2. #2
    Join Date
    Feb 2000
    Location
    Indiana
    Posts
    308

    Re: Is't possible to put "Data" in Excel Sheet or value in DBGrid on Excel Sheet

    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





  3. #3
    Join Date
    Mar 2000
    Posts
    19

    Re: Is't possible to put "Data" in Excel Sheet or value in DBGrid on Excel Sheet

    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...


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured