Results 1 to 20 of 20

Thread: SQL database to Excel

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    SQL database to Excel

    Need to find an example of how to export a small SQL database to Excel? Can this be done?

    Thanks all

  2. #2
    Lively Member
    Join Date
    Nov 2002
    Location
    Cranston, RI
    Posts
    110

    Re: SQL database to Excel

    The easiest way is to do it from enterprise manager (export data) or query analyzer (choose "SAVE AS" from the results window).

    Are you using sql server or MDBE?

    Does this have to be done programmatically?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    JanZuk:

    I'm using an SQL database. And yes it must be done programmatically.

  4. #4
    Lively Member
    Join Date
    Nov 2002
    Location
    Cranston, RI
    Posts
    110

    Re: SQL database to Excel

    Quick way of getting dataTable rows into an excel spreadsheet

    VB Code:
    1. Dim excelApp As New Excel.Application
    2. Dim excelBook As Excel.Workbook
    3. Dim excelWorksheet As Excel.Worksheet
    4.  
    5. excelBook = excelApp.Workbooks.Add
    6. excelWorksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
    7.  
    8. Dim i As Integer = 1
    9. Dim dr As DataRow
    10.  
    11. For Each dr In dt.Rows
    12.       excelWorksheet.Range("A" & i.ToString).Value = dr("Row1")
    13.       excelWorksheet.Range("B" & i.ToString).Value = dr("Row2")
    14.       i += 1
    15. Next

    EDIT: I have a reference to the Microsoft Excel 9.0 Object Library
    Last edited by JanZuk; Oct 9th, 2005 at 09:17 AM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    I will test it right away and will post back. Thanks JanZuk.

  6. #6
    Lively Member
    Join Date
    Nov 2002
    Location
    Cranston, RI
    Posts
    110

    Re: SQL database to Excel

    Cool.

    Once upon a time I was looking for a way to print my data from a datagrid and the example I found was to first convert it to an excel spreadsheet and then print that spreadsheet. This is that code (slightly condensed).

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    You have given me a great idea. Instead of exporting directly from the database, blindly to the user, I will show the data first in a DataGridView and then export on a button.click event.

    Does the code changes somehow?

    Thanks

  8. #8
    Lively Member
    Join Date
    Nov 2002
    Location
    Cranston, RI
    Posts
    110

    Re: SQL database to Excel

    Quote Originally Posted by piscis
    You have given me a great idea. Instead of exporting directly from the database, blindly to the user, I will show the data first in a DataGridView and then export on a button.click event.

    Does the code changes somehow?

    Thanks
    All you would be changing is the datasource in the for each loop (using the datagrid's datasource instead). In your click event, instead of
    VB Code:
    1. For Each dr In dt.Rows

    call
    VB Code:
    1. For Each dr In (CType(dg1.DataSource, DataTable)).Rows

    EDIT: to print out the excel spreadsheet just add excelWorksheet.PrintOut()
    Last edited by JanZuk; Oct 9th, 2005 at 09:42 AM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    Do I need to import any system namespace? I’m getting these errors:
    Attached Images Attached Images  

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    And this:
    Attached Images Attached Images  

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    I tried adding a reference to the Microsoft Excel 9.0 Object Library but in VB.NET 2005 we only have these two and they both return errors.

    Microsoft Excel 5.0 Object Library
    Microsoft Excel 11.0 Object Library

    any idea?

  12. #12
    Lively Member
    Join Date
    Nov 2002
    Location
    Cranston, RI
    Posts
    110

    Re: SQL database to Excel

    1) try the 11.0 object library. Shouldn't be *that* different (knock on wood).
    2) try importing from System.Data or use Data.DataTable/Data.DataRow

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    Did it, 2 errors only:
    Attached Images Attached Images  

  14. #14
    Lively Member
    Join Date
    Nov 2002
    Location
    Cranston, RI
    Posts
    110

    Re: SQL database to Excel

    Instead of excelApp being of type system.aplicationid, can you make it of type Microsoft.Office.Interop.Excel.Application?

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    OK, it seems like we rae in a good track: see this
    Attached Images Attached Images  

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    AND this:
    Attached Images Attached Images  

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    Don't bother, I did not read you post as I should have.

    Thanks for your help.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    One last thing ran the button click event and got an Invalid Cast exception:
    Attached Images Attached Images  

  19. #19
    New Member
    Join Date
    Nov 2005
    Posts
    3

    Re: SQL database to Excel

    I have also tried this code out and recieved the Invalid Cast execption error. Does anybody know how to fix this. Thank you very much.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Re: SQL database to Excel

    I just order VB2005 Pro from Amazon, they will ship on Dec 2. I believe this version comes with Crystal Report. If so then I do not need to export to Excel any longer, I could just do the report and export to PDF.

    That is if Crystal Report does allow to export to PDF.(I will post this question, just to make sure.)

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