Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,
I want to export the contents of Datagrid to Excel file in VB.NET Windows Application.
Please provide the code for this.
Posted

 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-Jul-11 2:24am    
Good links, my 5, but the whole approach OP is using needs a fix.
Please see my solution.
--SA
Uday P.Singh 11-Jul-11 2:36am    
thanks SA :)
[no name] 11-Jul-11 2:33am    
Nice Links. +5.
Uday P.Singh 11-Jul-11 2:36am    
thanks Koushik :)
Dalek Dave 11-Jul-11 3:20am    
Nice Links.
In addition to answer by Uday:

Don't export content of any controls to other controls, prints, reports, export document. You need to have a separate data layer. Probably you already using some data layer to bind with you data grid. Use data for generation of PDF, not data grid or any other control.

If this is not clear (and even it it is clear), learn about the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

—SA
 
Share this answer
 
v2
Comments
[no name] 11-Jul-11 2:34am    
Good Call. My 5.
Sergey Alexandrovich Kryukov 11-Jul-11 2:36am    
Thank you, Ramalinga.
--SA
[no name] 12-Jul-11 0:06am    
It's my pleasure SA.
Uday P.Singh 11-Jul-11 2:37am    
correct my 5!
Sergey Alexandrovich Kryukov 11-Jul-11 2:38am    
Thank you, Uday. Teamwork! :-)
--SA
Protected Sub btnsndexl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsndexl.Click
        If gdhidden.Rows.Count > 0 Then
            ExportGridView()
        Else
            ScriptManager.RegisterClientScriptBlock(btnsndexl, Me.GetType(), "MyScript", "alert('No Record Found');", True)
        End If
    End Sub


Private Sub ExportGridView()
       gdhidden.Visible = True
       Dim attachment As String = "attachment; filename=Report.xls"
       Response.ClearContent()
       Response.AddHeader("content-disposition", attachment)
       Response.ContentType = "application/ms-excel"
       Dim sw As New IO.StringWriter
       Dim htw As New UI.HtmlTextWriter(sw)
       gdhidden.RenderControl(htw)
       Response.Write(sw.ToString())
       Response.End()
       gdhidden.Visible = False
   End Sub
 
Share this answer
 
v2
Comments
[no name] 11-Jul-11 2:31am    
Edited for Codeblock.
Rashmi Krishna 11-Jul-11 3:10am    
Hi Jas121122... Thanks alot... Can u please confirm whether it is for Windows Application :)
jas121122 18-Jul-11 1:25am    
no its for web application
What if I want to export two datagridviews (Parent/Child ) on excel?

and the output is like this:

Parent Parent Parent
Child Child Child
Parent Parent Parent
Child Child Child
Parent Parent Parent
Child Child Child
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900