|
-
Nov 10th, 2011, 02:07 PM
#1
Thread Starter
Addicted Member
Excel and Datagridview
Hi All
The end of my current project is in site.
However, one thing still to sort.
I have a datagridview which I would like to be able to export the information to an Excel spreadsheet, and be able to reload the information later on from a spceified worksheet.
I've found a few "solutions" online, but none of them seem to work and so I need some major help.
My level of programming is very, very basic and so please forgive any ignorance which may happen throughout this!
Also, I'd like to avoid using Databases with Access etc because I am aware that some of the machines I'll be deploying the programme on do not have Access installed and would not want to have to install it either.
Thanks for your help
M
-
Nov 10th, 2011, 02:29 PM
#2
Frenzied Member
Re: Excel and Datagridview
What do you mean "reload the information later on from a specified worksheet?
-
Nov 10th, 2011, 02:31 PM
#3
Thread Starter
Addicted Member
Re: Excel and Datagridview
basically, save the information to that spreadsheet, and should the programme with the DGV in it be closed for whatever reason, be able to select the worksheet to reload to continue working on it ..
-
Nov 10th, 2011, 02:42 PM
#4
Frenzied Member
Re: Excel and Datagridview
open it from excel??
Here is some code that will create an excel sheet for you, may have to tweak it a bit but it should get you there
Code:
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = CType(xlWorkBook.Sheets("Sheet1"), Excel.Worksheet)
For Each col As DataGridViewColumn In DataGridView1.Columns
xlWorkSheet.Cells.Columns.CurrentRegion.Columns(col.Index + 1) = col.HeaderText.ToString
Next
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
If worker.CancellationPending Then
e.Cancel = True
Exit For
End If
xlWorkSheet.Cells(i + 2, j + 1) = _
DataGridView1(j, i).Value.ToString()
Next
Next
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
-
Nov 10th, 2011, 03:32 PM
#5
Thread Starter
Addicted Member
Re: Excel and Datagridview
Thanks for this! Getting some blue underlines saying "declaration expected" ...sorry for my lack of knowledge :S
-
Nov 10th, 2011, 03:47 PM
#6
Re: Excel and Datagridview
You might consider not dealing with Excel at all. Excel can open XML files easily, Excel can also write XML files easily, and that will be much easier for exporting and importing.
My usual boring signature: Nothing
 
-
Nov 10th, 2011, 03:50 PM
#7
Frenzied Member
Re: Excel and Datagridview
 Originally Posted by MacShand
Thanks for this! Getting some blue underlines saying "declaration expected" ...sorry for my lack of knowledge :S
Would help if you gave more information
but you probably need these place under you Public class form
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
-
Nov 10th, 2011, 09:52 PM
#8
Re: Excel and Datagridview
just go to this thread & find the signature "Export DGV to word ~ excel " in .pouls signature.
this is a good note
Tags for this Thread
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
|