|
-
Nov 15th, 2010, 05:59 AM
#1
Thread Starter
Hyperactive Member
How to Create Excel from DataGridView
Please can you tell me how do we create an excel file for each datatable of a dataset ?
I tried some codes I found in the net, they don't seem working, so can someone provide me a tutorial or code that shows how to create an excel file from datagrid view ??
-
Nov 15th, 2010, 06:08 AM
#2
Hyperactive Member
Re: How to Create Excel from DataGridView
i have an example if you need?
-
Nov 15th, 2010, 06:11 AM
#3
Hyperactive Member
Re: How to Create Excel from DataGridView
With this code you have a start
Code:
Public Sub ExportToExcel(ByVal vData As Data.DataTable)
Dim vSheet As Excel.Worksheet = Nothing
Dim vRange As Excel.Range = Nothing
Dim intColumnValue As Integer
Try
vWorkbooks = vExcel.Workbooks
vWorkbook = vWorkbooks.Add
vSheet = CType(vWorkbook.Sheets(1), Worksheet)
vRange = vSheet.Cells
'SET THE COLUMN NAMES
For i As Integer = 0 To (vData.Columns.Count - 1)
vRange(1, (i + 1)) = vData.Columns(i).ColumnName.ToString
Next
'SET THE CELLS VALUES
For y As Integer = 0 To (vData.Rows.Count - 1)
For intColumnValue = 0 To (vData.Columns.Count - 1)
vRange(y + 2, (intColumnValue + 1)) = vData.Rows(y).ItemArray(intColumnValue).ToString
Next
Next
vSheet.Columns().EntireColumn.AutoFit()
Catch Err As Exception
Throw New Exception("I-Synergy.Reporting.Business.MSExcel.ExportToExcel:" & vbCrLf & Err.Message)
End Try
End Sub
-
Nov 15th, 2010, 06:14 AM
#4
Thread Starter
Hyperactive Member
Re: How to Create Excel from DataGridView
Thanks a lot hassa046 for your fast reply, I will try it after lunch break and let you know if it works, thanks a lot
-
Nov 15th, 2010, 06:16 AM
#5
Hyperactive Member
Re: How to Create Excel from DataGridView
you're welcome
-
Nov 15th, 2010, 09:33 AM
#6
Thread Starter
Hyperactive Member
Re: How to Create Excel from DataGridView
Thanks man, with some edits to fit my code, your code work.
I have in my grid, a column with delete button, with your code it's not showing it's value and not making problem, which is great, but I wonder why with the code in the two following links it was not working due to this column, while your code work ?
Any idea ?
Link1
Link2
-
Nov 15th, 2010, 01:51 PM
#7
Hyperactive Member
Re: How to Create Excel from DataGridView
Maybe i made smarter code? LOL 
The first link uses a combination of early- and late bound variable to excel. Maybe there lies the problem.
Frankly, i don't know.
-
Nov 16th, 2010, 06:14 AM
#8
Thread Starter
Hyperactive Member
Re: How to Create Excel from DataGridView
Lol, as much as it works, than it's good 
By the way, I saw in the net some ways that allow to create an xml and open it by excel, it seems faster than looping through the cells, what do you think about that methods ? do you recommand them ? or it's better to do the way you showed me ?
-
Nov 16th, 2010, 08:13 AM
#9
Hyperactive Member
Re: How to Create Excel from DataGridView
The method showed there is another way to generate a xml file with excel formatting. In this case you better can use my method. If you want a real xml what also is importable in excel you should look for another solution to convert a datatable to xml.
-
Nov 16th, 2010, 09:01 AM
#10
Thread Starter
Hyperactive Member
Re: How to Create Excel from DataGridView
Thanks a lot again.
Do you have any code to import from Excel to datatable or dataset ?
-
Nov 16th, 2010, 09:03 AM
#11
Hyperactive Member
Re: How to Create Excel from DataGridView
i convert excel first to csv. that is a lot easier to import. but excel should work also.
I will look into it.
-
Nov 16th, 2010, 09:17 AM
#12
Re: How to Create Excel from DataGridView
Whe I do that (move Excel data to database tables) I open the Execl file like a database, select the data into a datareader or DataTable, Open a new connection to the database then use the datareader/Datatable holding the data from Execl and create insert statements to the database table
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Nov 16th, 2010, 10:04 AM
#13
Thread Starter
Hyperactive Member
Re: How to Create Excel from DataGridView
GaryMazzone, do you mind to pass me the code please ?
-
Nov 16th, 2010, 10:06 AM
#14
Re: How to Create Excel from DataGridView
What don't you know how to do. Both parts are faily stright forward. Attempt it and people will help.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Nov 16th, 2010, 11:38 AM
#15
Thread Starter
Hyperactive Member
Re: How to Create Excel from DataGridView
 Originally Posted by GaryMazzone
What don't you know how to do. Both parts are faily stright forward. Attempt it and people will help.
using Excel as a database !
If you don't want to provide your code, there is no problem, don't get mad, i will search in google and find how, but I supposed that it would be faster to ask you cuz you said U already did and i have thought based on your previous post that you are proposing help !
Nevermind
Peace
-
Nov 16th, 2010, 12:07 PM
#16
Re: How to Create Excel from DataGridView
Check www.connectionstrings.com for a connection string to treat Excel as a database. Then you use the worksheet name as a table name
Sometimes the Programmer
Sometimes the DBA
Mazz1
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
|