Exporting DataGrids to Excel ...
Ken Walker's article on exporting DataSets to Excel has proved very popular. This article by Mike Dolan presents a different technique for getting a DataGrid rather than the DataSet into Excel. We believe you will find it very useful.
BY:
Mike Dolan Date: August 13, 2003 Download The Code.
Printer Friendly Version
I have a Datagrid with a Dataset as its datasource that I want to export to Excel. Using Ken Walker's awesome tutorial (http://www.dotnetjohn.com/articles/articleid36.aspx) on how to pass Datasets to via Excel a component , I was able to easily send a dataset to excel from anywhere in my application. However, a few things did not end up working out for my application. First, I needed to clean up the formatting. Second, many of my datagrids had columns that were calculated from data in the datasets. Third, the header row in Excel always contained the database column names which were sometimes unintelligible to an ordinary user. My final problem was that often the Datasets contained data necessary for the datagrid generation, but that We Didn't want the end user to see.
To remedy all these issues I came up with the a simpler and more adaptable way to export the datagrid itself to Excel. I kept the concept to a component class so that it could easily be used throughout an application.
I created the following component I used VS.Net and left out the "Component Designer Generated Code Region" What you will find is that the class has one method that takes in two arguments:... A datagrid and a response It works by simply .
I also included sections on how to change formatting. In the application we built, we wanted our Excel exports to have a standardized look. The middle-end section shows different types of formatting changes that can be made at the class level.cmpDataGridToExcel.vb
Public Class cmpDataGridToExcel Inherits System.ComponentModel.ComponentPublic Shared Sub DataGridToExcel (ByVal dgExport As DataGrid, ByVal response As HttpResponse) 'clean up the response.object response.Clear () response.Charset = ""' set the response mime type for excel response .ContentType = "application / vnd.ms-excel" 'create a string writer Dim stringWrite As New System.IO.StringWriter ()' create an htmltextwriter which uses the stringwriter Dim htmlWrite As New System.Web.UI.HtmlTextWriter (stringWrite) 'instantiate a datagrid Dim dg As New DataGrid ()' just set the input datagrid = to the new dg grid dg = dgExport 'I want to make sure there are no annoying gridlines dg.GridLines = GridLines.None' Make the header text bold dg.HeaderStyle.Font.Bold = True 'If needed, here's how to change colors / formatting at the component level' dg.HeaderStyle.ForeColor = System.Drawing.Color.Black 'dg.ItemStyle.ForeColor = System.Drawing.Color .Bleck 'bind the modified datagr ID DG.DATABIND () 'Tell The DataGrid To Render Itself to Our HTMLTextWriter Dg.RenderControl (HTMLWRITE)' Output The Html Response.write (StringWrite.toString) Response.end () End Subend Class
After the component above was created / compiled, we then moved to the actual page that generated the Datagrid we would pass to the component One of our datagrids looked like the following:. As you can see we have totals, discounts, and Boolean approval checkboxes . ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
The following in based on a dataset taken from our SQL server. Just like in Ken's article, it does not matter how you generate the dataset or datagrid. Just create a datagrid and format it the way you want. Our datagrid above was generated with The Following:
For Security and Copyright Reasons, I Will Only include A Small Portion of Our Aspx Pages Mostly Template Column Generated In this fashion:
InvoiceApproval.aspx Code Snippet
GetStore and GetLabor are simply functions we called passing the database data for the customer id and labor / service charges respectively. You can create your datagrid however you want, this simply shows why a dataset export with intCustomerID and not a Store # as we needed would NOT Work.a Northwind Database Example
Every tutorial needs a full example from the Northwind or Pubs database. The following is the Example.aspx file that displays the datagrid. We are still using the same component as you see above for the export.
.........................
DataGridexport.aspx
! <% @ Page Language = "vb" AutoEventWireup = "false" Codebehind = "DataGridExport.aspx.vb" Inherits = "ExportExample.DataGridExport"%>