Tuesday, March 10, 2009

Exporting GridView data to Excel,Word, & Text file is a very common task which is performed in most of the asp.net web applications to meet the customized reporting purposes. There are various techniques of exporting the GridView to excel and it highly depends on the application scenario. I will show you that how you can export your GridView data to Excel file, Word file and also Text file. When exporting keep the format of data is a very frequent requirement and also user might want to set different format which i will discuss in the later section of this post. So lets start with direct export to excel. Atfirst we need to create a table in sql server database. Table looks like:

Now we need to add a page. Add a GridView, Button in our page to export data. Now page design is completed. So now we need to bind data to the GridView first. Write the below code under Page_Load event to read data from sql server table:

Don't forget to override the VerifyRenderingInServerForm method. For exporting each page must have override this method:

public

overridevoid VerifyRenderingInServerForm(Control control) { }

Export to Excel when GridView has paging:Its a little bit tricky to export data when GridView has Paging functionality. Just set the AllowPaging property to False beforeexporting data. Now export button click event code like:

Export to Excel when GridView contains control:Replace corresponding GridView cells by controls value. Let you have a supplier list & you wantto display each supplier location as well as you want a facility that when user open thelist then user can see the regions other suppliers in a popup. To do that you have to add a link button in your GridView so that user can click on the link to view the other suppliers. Alsoyou have to export region value to Excel file. So how you can handle. Answer is replace the cell valueby link text just before exporting. Code sample:

Export to Excel with proper fomatting:Hope from above example now one can easily export data from GridView. But an important concern is to setthe formatting to the target file. In this reagard mso-number-format ease our life. Lets we need todisplay date & time in the GridView but we wants short date format when exporting. To do the one can use mso-number-format in the following way. Modify your export button click event by the following code segment.

Note: If you do not find your required format from above list then try creating a spreadsheet with a single cell entry according to your required format. Then from file menu select the 'Save as Web Page' option to create a html file. Read & examine the code for this and findout the mso-number-format from the style declarations.***Click for more details on GridView Column Formatting in my another article

Good question Swapna. Basically using this code its not possible. You have to use framework 3.5 or upper. Lot of MS document related methods defined in new frameworks with appropriate documentation which is absent in 2.0. So study that since i didn't study yet.

It works well enough to get going. Now, if you have an idea how to export into a word document or pdf, it will be great!

for tpinting out the header row just add the following code:

Dim str As New System.Text.StringBuilder() For i As Integer = 0 To GridView1.HeaderRow.Cells.Count - 1 str.Append(GridView1.HeaderRow.Cells(i).Text + ", ") Next str.Append(vbCr & vbLf)

Here is the complete procedure: Public Sub exportInCSV() Dim str As New System.Text.StringBuilder() For i As Integer = 0 To GridView1.HeaderRow.Cells.Count - 1 str.Append(GridView1.HeaderRow.Cells(i).Text + ", ") 'str.Append(vbCr & vbLf) Next str.Append(vbCr & vbLf) For Each oItem As GridViewRow In GridView1.Rows For j As Integer = 0 To oItem.Cells.Count - 1 If oItem.Cells(j).Text.Contains("=") Then Continue For End If str.Append(oItem.Cells(j).Text + ", ") Next str.Append(vbCr & vbLf) Next Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.txt") Response.ContentType = "application/vnd.text" Response.Write(str.ToString()) Response.[End]() End Sub