Connecting Tech Pros Worldwide Help | Site Map

Export DataGridView to Excel

  #1  
Old April 26th, 2007, 12:35 AM
Mike Wilson
Guest
 
Posts: n/a
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

  #2  
Old April 26th, 2007, 01:35 AM
Rick
Guest
 
Posts: n/a

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


  #3  
Old April 26th, 2007, 02:05 AM
Mike Wilson
Guest
 
Posts: n/a

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

  #4  
Old April 26th, 2007, 02:15 AM
Mike Wilson
Guest
 
Posts: n/a

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*

  #5  
Old April 26th, 2007, 09:35 PM
Tim Van Wassenhove
Guest
 
Posts: n/a

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/>
  #6  
Old April 29th, 2007, 01:25 PM
Mike Wilson
Guest
 
Posts: n/a

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

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export DataGridView to Access with .net ISATownsME answers 2 October 24th, 2008 02:14 PM
How to export datagridview to excel? RafaulWolf answers 7 November 30th, 2007 10:15 AM
WTD: Simple Function to Export DataGridView to Excel Mike Wilson answers 2 April 6th, 2007 12:45 PM
export datagridview to excel artteam answers 0 March 6th, 2007 04:21 AM