Oct 9th, 2005, 08:06 AM
#1
Thread Starter
Hyperactive Member
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
Oct 9th, 2005, 08:30 AM
#2
Lively Member
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?
Oct 9th, 2005, 08:56 AM
#3
Thread Starter
Hyperactive Member
Re: SQL database to Excel
JanZuk:
I'm using an SQL database. And yes it must be done programmatically.
Oct 9th, 2005, 09:05 AM
#4
Lively Member
Re: SQL database to Excel
Quick way of getting dataTable rows into an excel spreadsheet
VB Code:
Dim excelApp As New Excel.Application
Dim excelBook As Excel.Workbook
Dim excelWorksheet As Excel.Worksheet
excelBook = excelApp.Workbooks.Add
excelWorksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
Dim i As Integer = 1
Dim dr As DataRow
For Each dr In dt.Rows
excelWorksheet.Range("A" & i.ToString).Value = dr("Row1")
excelWorksheet.Range("B" & i.ToString).Value = dr("Row2")
i += 1
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 .
Oct 9th, 2005, 09:18 AM
#5
Thread Starter
Hyperactive Member
Re: SQL database to Excel
I will test it right away and will post back. Thanks JanZuk.
Oct 9th, 2005, 09:22 AM
#6
Lively Member
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).
Oct 9th, 2005, 09:28 AM
#7
Thread Starter
Hyperactive Member
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
Oct 9th, 2005, 09:36 AM
#8
Lively Member
Re: SQL database to Excel
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
call
VB Code:
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 .
Oct 9th, 2005, 09:51 AM
#9
Thread Starter
Hyperactive Member
Re: SQL database to Excel
Do I need to import any system namespace? I’m getting these errors:
Attached Images
Oct 9th, 2005, 09:54 AM
#10
Thread Starter
Hyperactive Member
Re: SQL database to Excel
Attached Images
Oct 9th, 2005, 10:05 AM
#11
Thread Starter
Hyperactive Member
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?
Oct 9th, 2005, 10:16 AM
#12
Lively Member
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
Oct 9th, 2005, 10:31 AM
#13
Thread Starter
Hyperactive Member
Re: SQL database to Excel
Attached Images
Oct 9th, 2005, 10:37 AM
#14
Lively Member
Re: SQL database to Excel
Instead of excelApp being of type system.aplicationid, can you make it of type Microsoft.Office.Interop.Excel.Application?
Oct 9th, 2005, 12:03 PM
#15
Thread Starter
Hyperactive Member
Re: SQL database to Excel
OK, it seems like we rae in a good track: see this
Attached Images
Oct 9th, 2005, 12:06 PM
#16
Thread Starter
Hyperactive Member
Re: SQL database to Excel
Attached Images
Oct 9th, 2005, 12:39 PM
#17
Thread Starter
Hyperactive Member
Re: SQL database to Excel
Don't bother, I did not read you post as I should have.
Thanks for your help.
Oct 9th, 2005, 12:44 PM
#18
Thread Starter
Hyperactive Member
Re: SQL database to Excel
One last thing ran the button click event and got an Invalid Cast exception:
Attached Images
Nov 14th, 2005, 12:07 PM
#19
New Member
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.
Nov 14th, 2005, 02:21 PM
#20
Thread Starter
Hyperactive Member
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
Forum Rules
Click Here to Expand Forum to Full Width