473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

Apr 25 '07 #1
5 34030
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
Apr 26 '07 #2
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

Apr 26 '07 #3
"Rick" <Ri**@LakeValleySeed.comwrote in message
news:OC**************@TK2MSFTNGP02.phx.gbl...
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*

Apr 26 '07 #4
Mike Wilson schreef:
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/>
Apr 26 '07 #5
"Tim Van Wassenhove" <ti***@newsgroup.nospamwrote in message
news:uj**************@TK2MSFTNGP03.phx.gbl...
Mike Wilson schreef:
>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

Apr 29 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table....
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
0
by: artteam | last post by:
Hi All, I try to export the table in DataGridView control on my form to MS excel with the following code. private void fnExtractToExcel() { ...
2
by: Mike Wilson | last post by:
Dear all, I'm sure someone has already done this, so as not to wish to re-invent the wheel - and Google not turning up anything I can use, does anyone have a suitable function into which I can...
7
by: RafaulWolf | last post by:
Hi, anyone know how can i export the information in the datagridview to an Excel file? i understand that the crystal report tool has such a build in function for importing to pdf or execl, then...
0
by: nitin005 | last post by:
I have to export the data present in the datagridview to Microsoft Excel. It is a window application using c#. Does anyone know how to do it. Please help. Thanks
19
by: cj2 | last post by:
#1 Is there a quick way to export a datatable to an excel file? Or delimited file? #2 Where is the appropriate Microsoft monitored group to ask about writing reports in SQL Reporting services...
2
by: ISATownsME | last post by:
Hello, I have a program that loads a datagridview from excel to visual studio. Can anyone tell me how to export the cell values of the datagridview to my access database?
2
by: Narz1986 | last post by:
hi...i m trying to export data from the datagrid to excel...here is my code HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.