473,554 Members | 3,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 34043
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(By Val dgv As DataGridView)

Dim OutputStr As String = ""

Dim nCol As DataGridViewCol umn
' 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.Start sWith(",") Then OutputStr = OutputStr.Subst ring(1,
OutputStr.Lengt h - 1)

Dim nRow As DataGridViewRow
Dim nCell As DataGridViewCel l
Dim OutputLine As String = ""
For Each nRow In dgv.Rows
For Each nCell In nRow.Cells
If nCell.Visible = True Then
OutputLine = OutputLine & "," &
nCell.Formatted Value.ToString. Replace(",", "")
End If
Next
' Trim off the first comma
If OutputLine.Star tsWith(",") Then OutputLine =
OutputLine.Subs tring(1, OutputLine.Leng th - 1)
' New line? Add it
If OutputLine <"" Then OutputStr = OutputStr & vbCrLf &
OutputLine
Next
MsgBox(OutputSt r)
End Sub

Apr 26 '07 #3
"Rick" <Ri**@LakeValle ySeed.comwrote in message
news:OC******** ******@TK2MSFTN GP02.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.LightGree n 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***@newsgrou p.nospamwrote in message
news:uj******** ******@TK2MSFTN GP03.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.LightGree n 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
5014
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. Before I iterate through the recordset I instruct the browser that the content type is Excel using the following line: (Response.ContentType =...
13
13190
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" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to...
0
2313
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() { Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); excel.Application.Workbooks.Add(true) ; ...
2
4971
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 pass a DataGridView - and have all the columns and rows parsed and an Excel Workbook created with the DataGridView contents and column headers...
7
14581
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 how can i convert the information in datagrid to a crystral report?
0
1451
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
11214
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 or SQL Server Business Intelligence Development Studio or whatever it's called?
2
2853
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
2348
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", "attachment;filename=" + exportfile.FileName + ""); HttpContext.Current.Response.Charset = "";...
0
7596
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8039
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7887
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6140
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5431
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3545
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1130
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.