Export DataGridView to Excel 
April 25th, 2007, 11:35 PM
| | | Export DataGridView to Excel
Hello all,
I'd like to export a DataGridView to Excel. I am using .NET 2.0 and VB.NET.
But I don't know if the user has Excel on their machines, so can't use COM
Excel object.
Any ideas please? How is an Excel file put together?
Many thanks,
Mike | 
April 26th, 2007, 12:35 AM
| | | Re: Export DataGridView to Excel
Mile,
I was faced with the same problem. Because users had different versions of
Excel installed, I could not find one solution.
Finally I created an html table from the datagridview bindingsource. If you
save this as *.xls, it seems to open in all versions of Excel.
You can dress up the html with a header and description, but the basis is
the table.
See if that works for you.
Rick | 
April 26th, 2007, 01:05 AM
| | | Re: Export DataGridView to Excel
No problem, I've written some code to do this using CSV...
Public Sub WriteCSVFile(ByVal dgv As DataGridView)
Dim OutputStr As String = ""
Dim nCol As DataGridViewColumn
' Write the column headers
For Each nCol In dgv.Columns
' First row, write column names
If nCol.Visible = True Then
OutputStr = OutputStr & "," & nCol.HeaderText
End If
Next
' Trim off the first comma
If OutputStr.StartsWith(",") Then OutputStr = OutputStr.Substring(1,
OutputStr.Length - 1)
Dim nRow As DataGridViewRow
Dim nCell As DataGridViewCell
Dim OutputLine As String = ""
For Each nRow In dgv.Rows
For Each nCell In nRow.Cells
If nCell.Visible = True Then
OutputLine = OutputLine & "," &
nCell.FormattedValue.ToString.Replace(",", "")
End If
Next
' Trim off the first comma
If OutputLine.StartsWith(",") Then OutputLine =
OutputLine.Substring(1, OutputLine.Length - 1)
' New line? Add it
If OutputLine <"" Then OutputStr = OutputStr & vbCrLf &
OutputLine
Next
MsgBox(OutputStr)
End Sub | 
April 26th, 2007, 01:15 AM
| | | Re: Export DataGridView to Excel
"Rick" <Rick@LakeValleySeed.comwrote in message
news:OCyD5k5hHHA.3960@TK2MSFTNGP02.phx.gbl... Quote:
Mile,
>
I was faced with the same problem. Because users had different versions
of Excel installed, I could not find one solution.
>
Finally I created an html table from the datagridview bindingsource. If
you save this as *.xls, it seems to open in all versions of Excel.
>
You can dress up the html with a header and description, but the basis is
the table.
>
See if that works for you.
| Thanks Rick,
That's a great idea. Just a normal HTML table saved as ".xls"
I've made a CSV solution for now which works well but has of course no
formatting.
*fap* | 
April 26th, 2007, 08:35 PM
| | | Re: Export DataGridView to Excel
Mike Wilson schreef: Quote:
Hello all,
>
I'd like to export a DataGridView to Excel. I am using .NET 2.0 and VB.NET.
>
But I don't know if the user has Excel on their machines, so can't use
COM Excel object.
>
Any ideas please? How is an Excel file put together?
| I did it using CarlosAg Excel writer (it generates excel xml)... http://www.timvw.be/datagridview-to-excel/
(It tries to mimic the CellStyle and format as much as possible.. Be
aware that excel has a limitted color palette, eg: Color.LightGreen as
BackColor in a Cell would be greyish in the sheet)
--
Tim Van Wassenhove <url:http://www.timvw.be/> | 
April 29th, 2007, 12:25 PM
| | | Re: Export DataGridView to Excel
"Tim Van Wassenhove" <timvw@newsgroup.nospamwrote in message
news:ujlTmDEiHHA.1708@TK2MSFTNGP03.phx.gbl... Quote:
Mike Wilson schreef: Quote:
>Hello all,
>>
>I'd like to export a DataGridView to Excel. I am using .NET 2.0 and
>VB.NET.
>>
>But I don't know if the user has Excel on their machines, so can't use
>COM Excel object.
>>
>Any ideas please? How is an Excel file put together?
| >
I did it using CarlosAg Excel writer (it generates excel xml)...
> http://www.timvw.be/datagridview-to-excel/
>
(It tries to mimic the CellStyle and format as much as possible.. Be aware
that excel has a limitted color palette, eg: Color.LightGreen as BackColor
in a Cell would be greyish in the sheet)
| Hi Tim,
Useful, thanks. I'll have a read and pass the info on.
Mike | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|