Re: Excel and Datagridview
What do you mean "reload the information later on from a specified worksheet?
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 .. :)
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)
Re: Excel and Datagridview
Thanks for this! Getting some blue underlines saying "declaration expected" ...sorry for my lack of knowledge :S
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.
Re: Excel and Datagridview
Quote:
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
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