Click here to Skip to main content
Licence 
First Posted 13 May 2006
Views 66,467
Downloads 1,095
Bookmarked 31 times

Simple Way To Export Formatted Data Grid To Excel Sheet

By | 19 Feb 2007 | Article
Exporting datagrid to formatted excel report or presentation

Sample Image - DataGrid_To_Excel.jpg

Introduction

Exporting datagrid to excel is very common practice and is mostly used for reporting purposes so the exported datagrid should be fully formatted. Here I have tried a bit for this that the exported grid should be formatted enough for presentation.

How it Works

I have developed a ddl for this and refer that ddl to web project you can make changes in this ddl and can do the same but if your are not comfortable you can use the same code in your desired application. If you want to use the ddl you just need to pass some parameters your desired excel sheet will be populated.

Code

Here I will mention the code used in the ddl.

Imports System.Data.OleDb
Imports System.Web.HttpResponse
Public Class DataGrid
    'if u develop ddl you need to inhertit this
    Inherits System.Web.UI.Page
    'this is the main function to export data to sheet
    'here query is your desired SQL query
    'report_title the title of the report if you want any
    'Connection_String is the database connection, you can use any kind of connection like access, sql server, oracle
    Public Sub Excel_View(ByVal Query As String, ByVal Report_Title As String, ByVal Connection_String As String)
        'defin and open new database connection
         Dim conn As New OleDbConnection(Connection_String)
        conn.Open()
         'new datagrid control
        Dim dg As New System.Web.UI.WebControls.DataGrid()
        Dim da As New OleDbDataAdapter(Query, conn)
        Dim ds As DataSet
        ds = New DataSet()
        'fill datagrid
         da.Fill(ds)
        dg.DataSource = ds
        dg.DataBind()
         'datagrid formatting
        dg.Font.Name = "Verdana"
        dg.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid
        dg.HeaderStyle.BackColor = System.Drawing.Color.Blue
        dg.HeaderStyle.ForeColor = System.Drawing.Color.White
        dg.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center
        dg.HeaderStyle.Wrap = False
        dg.HeaderStyle.Font.Bold = True
 
         'export to excel
        context.Response.Buffer = True
        context.Response.ClearContent()
        context.Response.ClearHeaders()
        context.Response.ContentType = "application/vnd.ms-excel"
        EnableViewState = True
        Dim tw As New System.IO.StringWriter()
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)
        dg.RenderControl(hw)
        context.Response.Write(Now)
        context.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" & Report_Title & "</font></center></b>")
        Context.Response.Write(tw.ToString())
        Context.Response.Flush()
        context.Response.Close()
        context.Response.End()
        da.Dispose()
        da = Nothing
        ds.Dispose()
        ds = Nothing
        conn.Close()
        conn.Dispose()
        conn = Nothing
    End Sub
End Class

Wokring With Demo Project

if you want to run the attached demo then extract the zip file and place it in your localhost folder, Then create a virtual directory ExcelTest and run this in browser as http://localhost/exceltest

Working With Code

If you want to use the same ddl then download the demo project copy ddl to your project and use the following code.

<p style="TEXT-ALIGN: justify">    'create new object
    Dim abc As New GridToExcel.DataGrid
    'call function to populate data
    abc.Excel_View(query,report_title,connection_String)
</p>

but if you want to use the code you can use it according to your needs and can accordingly modify it.

Point of Interest

  • Using this ddl you do not need to write the code again and again
  • As you pass connection string so you donot need to be worried about the database, you can use every kind of database and pass the required database string.
  • Even you can customize the report according to your desired format.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Malik Nasir

Team Leader
ZonG, China Mobile Company
Pakistan Pakistan

Member

I am currently based in Islamabad,Pakistan. I have done my MS Computer Science and I am involved in Microsoft Visual Studio.net Windows/Web application development since 2003 untill that i have been working in VB, ASP and web scripting languages. Currently i am working in ZonG as Team LEad IT Projects and Planing. I am also the administrator of Business Process

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAlternative PinmemberFilipKrnjic4:51 27 Jul '09  
GeneralExport data from DataGrid to Excel PinmemberPranitaPS5:56 14 Jul '08  
Questionneed more guidance Pinmembercchhkknnmm19:14 27 Apr '08  
hii malik...i m absolutely new to this field...i need more guidance regarding how to export grid data into excel....thru C#.
as u had provided the C# code for it...bt i m not clear how to use it n chk it.
waiting for reply...
GeneralAbout the Code Pinmembermib151521:36 26 Feb '08  
QuestionError while using with Update Panel PinmemberJaydeep20:10 6 Dec '07  
Questionusing ASP to export data from SQL database to excel (server side) PinmemberIT_GIRL13:06 22 Oct '07  
Questioni have problem in Export data's in Datagrid to Excel PinmemberDeepika Shalini0:07 28 Jun '07  
QuestionExporting Data PinmemberMember #387219619:34 27 Feb '07  
Generalgood but change the code PinmemberMember #38040310:35 19 Feb '07  
GeneralRe: good but change the code PinmemberMalik Nasir2:41 19 Feb '07  
GeneralExcel Export with multiple worksheets Pinmemberumaramiya22:26 4 Feb '07  
Generali need asp page report to excel Pinmemberlaksh_us23:42 10 Jul '06  
GeneralRe: i need asp page report to excel PinmemberMalik Nasir19:51 18 Sep '06  
Generaldoesn't show any data Pinmembermahila22:17 28 May '06  
GeneralRe: doesn't show any data PinmemberMalik Nasir22:52 28 May '06  
GeneralRe: doesn't show any data PinmemberMalik Nasir23:29 28 May '06  
GeneralRe: doesn't show any data Pinmembermahila4:03 29 May '06  
GeneralRe: doesn't show any data PinmemberMalik Nasir4:26 29 May '06  
GeneralRe: doesn't show any data Pinmembermahila19:35 29 May '06  
GeneralRe: doesn't show any data PinmemberMalik Nasir19:46 29 May '06  
GeneralRe: doesn't show any data Pinmembermahila21:03 29 May '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 19 Feb 2007
Article Copyright 2006 by Malik Nasir
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid