Export DataGridView To Excel

anithab

New member
Joined
Nov 29, 2010
Messages
2
Programming Experience
Beginner
Hello All,

I want to export datagridvalues to excel sheet.
Right now I am exporting using the Microsoft.Office.Interop.Excel. But I am able to export to Office 2007 only. The data should be exported to any version of office, either 2003,2007 or 2010.

Can any body suggest on this.
Thanks in advance.

--Anitha
 
Try looking at the following link, it looks like a couple of other people have had the same problem

Microsoft.Office.Interop.Excel

Specifically the part where they mention:

"Please ensure you added Office Object Library with correct version.
If you are using Office Excel 2007, you only need to add referance to Microsoft Office 12.0 Object Library, and Imports Microsoft.Office.Interop.Excel.

If you are using Office Excel 2003, you only need to add referance to Microsoft Office 11.0 Object Library, and Imports Microsoft.Office.Interop.Excel."
 
Hello All,

I want to export datagridvalues to excel sheet.
Right now I am exporting using the Microsoft.Office.Interop.Excel. But I am able to export to Office 2007 only. The data should be exported to any version of office, either 2003,2007 or 2010.

Can any body suggest on this.
Thanks in advance.

--Anitha
With a WorkBook you can use the SaveAs method where you specify FileFormat, here you can choose which version you want to output as.
SaveAs Method [Excel 2007 Developer Reference] check the link for XlFileFormat enumeration also.
 
this is my first post im not good in english ; and here is my code and i hope it's will work fine 4u
Private Sub ExporterDtgView()
Try
'chose the distination file
If SaveFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
'It validates the edition of the DataGridView
DataGridView1.EndEdit()
'We are preparing a brief to write formatted data to the file
Dim ToSave As New StringBuilder()
'We'll put the headers as required :)
Dim Headers As String = String.Empty
For index As Integer = 0 To DataGridView1.Columns.Count - 1
Headers &= DataGridView1.Columns(index).HeaderText & ";"
Next
'The loop adds a ";" at the end if it's useless
Headers = Headers.Remove(Headers.LastIndexOf(";"), 1)
'Now it is stored in the memory
ToSave.AppendLine(Headers)

'Loop over all available lines
For i As UInt64 = 0 To DataGridView1.Rows.Count - 1
'It is a variable to store a line
Dim OneRow As String = String.Empty
'You can do a loop on all the columns available if the line is not empty
If DataGridView1.Rows(i).IsNewRow = False Then
For j As Integer = 0 To DataGridView1.Rows(i).Cells.Count - 1
OneRow &= DataGridView1.Rows(i).Cells(j).Value & ";"
Next
OneRow = OneRow.Remove(OneRow.LastIndexOf(";"), 1)
ToSave.AppendLine(OneRow)
End If
Next
'now trying to write the file
IO.File.WriteAllText(SaveFileDialog1.FileName, ToSave.ToString(), Encoding.Default)
End If

Catch ex As Exception
MessageBox.Show(String.Format("{0}{1}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace))
End Try
End Sub
 
Back
Top