ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Export GridView To Word Excel PDF CSV Formats in ASP.Net
Author Name: Mudassar Khan Published Date: March 14, 2009
Filed Under :
ASP.Net
 |
Excel
 |
GridView
Views: 44471

In this article, I will explain how to export GridView to Word, Excel, PDF and CSV formats.

Exporting to Word, Excel and CSV can be easily achieved using ASP.Net without any third party tools, but for exporting GridView to PDF I am using iTextSharp which is a free library for exporting html to PDF.

 

To start with I have a GridView in which I am showing Customers records from the NorthWind Database.

The HTML markup of the GridView is as shown below

 

<asp:GridView ID="GridView1" runat="server"

    AutoGenerateColumns = "false" Font-Names = "Arial"

    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 

    HeaderStyle-BackColor = "green" AllowPaging ="true"  

    OnPageIndexChanging = "OnPaging" >

   <Columns>

    <asp:BoundField ItemStyle-Width = "150px" DataField = "CustomerID"

    HeaderText = "CustomerID" />

    <asp:BoundField ItemStyle-Width = "150px" DataField = "City"

    HeaderText = "City"/>

    <asp:BoundField ItemStyle-Width = "150px" DataField = "Country"

    HeaderText = "Country"/>

    <asp:BoundField ItemStyle-Width = "150px" DataField = "PostalCode"

    HeaderText = "PostalCode"/>

   </Columns>

</asp:GridView>

 

In the figure below the GridView is shown with four buttons

1.     Export To Word

2.     Export To Excel

3.     Export To PDF

4.     Export To CSV



GridView with Sample Data



Export to Microsoft Word Format

 

C#

protected void btnExportWord_Click(object sender, EventArgs e)

{

    Response.Clear();

    Response.Buffer = true;

    Response.AddHeader("content-disposition",

    "attachment;filename=GridViewExport.doc");

    Response.Charset = "";

    Response.ContentType = "application/vnd.ms-word ";

    StringWriter sw= new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;

    GridView1.DataBind();

    GridView1.RenderControl(hw);

    Response.Output.Write(sw.ToString());

    Response.Flush();

    Response.End();

}

 

VB.Net

Protected Sub btnExportWord_Click(ByVal sender As Object,

    ByVal e As EventArgs)

        Response.Clear()

        Response.Buffer = True

        Response.AddHeader("content-disposition",

        "attachment;filename=GridViewExport.doc")

        Response.Charset = ""

        Response.ContentType = "application/vnd.ms-word "

        Dim sw As New StringWriter()

        Dim hw As New HtmlTextWriter(sw)

        GridView1.AllowPaging = False

        GridView1.DataBind()

        GridView1.RenderControl(hw)

        Response.Output.Write(sw.ToString())

        Response.Flush()

        Response.End()

    End Sub

 

The above function renders the GridView contents as Microsoft Word format. You will notice I have disabled paging before exporting, so that all the pages are exported.

The Output Exported File



GridView data exported to Word Document



Export to Microsoft Excel Format

 

For exporting the document to Excel if you do it directly as done in case of word the row background color is applied throughout to all the columns in the Excel Sheet hence in order to avoid it. I have done a workaround below.

First I am changing the background color of each row back to white.

Then I am applying the background color to each individual cell rather than the whole row. Thus when you export now you will notice that the formatting is applied only to the GridView cells and not all

Also I am applying textmode style class to all cells and then adding the style CSS class to the GridView before rendering it, this ensures that all the contents of GridView are rendered as text.

 

protected void btnExportExcel_Click(object sender, EventArgs e)

{

Response.Clear();

Response.Buffer = true;

 

Response.AddHeader("content-disposition",

"attachment;filename=GridViewExport.xls");

Response.Charset = "";

Response.ContentType = "application/vnd.ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

 

GridView1.AllowPaging = false;

GridView1.DataBind();

 

//Change the Header Row back to white color

GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");

 

//Apply style to Individual Cells

GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");

GridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");

GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");

GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");  

 

for (int i = 0; i < GridView1.Rows.Count;i++ )

{

    GridViewRow row = GridView1.Rows[i];

 

    //Change Color back to white

    row.BackColor = System.Drawing.Color.White;

 

    //Apply text style to each Row

    row.Attributes.Add("class", "textmode");

 

    //Apply style to Individual Cells of Alternating Row

    if (i % 2 != 0)

    {

        row.Cells[0].Style.Add("background-color", "#C2D69B");

        row.Cells[1].Style.Add("background-color", "#C2D69B");

        row.Cells[2].Style.Add("background-color", "#C2D69B");

        row.Cells[3].Style.Add("background-color", "#C2D69B");  

    }

}

GridView1.RenderControl(hw);

 

//style to format numbers to string

string style = @"<style> .textmode { mso-number-format:\@; } </style>";

Response.Write(style);

Response.Output.Write(sw.ToString());

Response.Flush();

Response.End();

}

 

 

VB.Net

 

Protected Sub btnExportExcel_Click(ByVal sender As Object,

ByVal e As EventArgs)

  Response.Clear()

  Response.Buffer = True

 

  Response.AddHeader("content-disposition",

  "attachment;filename=GridViewExport.xls")

  Response.Charset = ""

  Response.ContentType = "application/vnd.ms-excel"

 

  Dim sw As New StringWriter()

  Dim hw As New HtmlTextWriter(sw)

 

  GridView1.AllowPaging = False

  GridView1.DataBind()

 

  'Change the Header Row back to white color

  GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")

 

  'Apply style to Individual Cells

  GridView1.HeaderRow.Cells(0).Style.Add("background-color", "green")

  GridView1.HeaderRow.Cells(1).Style.Add("background-color", "green")

  GridView1.HeaderRow.Cells(2).Style.Add("background-color", "green")

  GridView1.HeaderRow.Cells(3).Style.Add("background-color", "green")

 

  For i As Integer = 0 To GridView1.Rows.Count - 1

   Dim row As GridViewRow = GridView1.Rows(i)

 

   'Change Color back to white

   row.BackColor = System.Drawing.Color.White

 

   'Apply text style to each Row

   row.Attributes.Add("class", "textmode")

 

   'Apply style to Individual Cells of Alternating Row

   If i Mod 2 <> 0 Then

    row.Cells(0).Style.Add("background-color", "#C2D69B")

    row.Cells(1).Style.Add("background-color", "#C2D69B")

    row.Cells(2).Style.Add("background-color", "#C2D69B")

    row.Cells(3).Style.Add("background-color", "#C2D69B")

   End If

  Next

  GridView1.RenderControl(hw)

 

  'style to format numbers to string

  Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"

  Response.Write(style)

  Response.Output.Write(sw.ToString())

  Response.Flush()

  Response.End()

End Sub

 

The Output Exported File



GridView data exported to Excel Document



Export to Portable Document Format

 

For exporting the GridView to PDF I am using the iTextSharp Library. You will need to Add Reference for the iTextSharp Library in your Website.

Then import the following Namespaces

 

C#

using iTextSharp.text;

using iTextSharp.text.pdf;

using iTextSharp.text.html;

using iTextSharp.text.html.simpleparser;

 

VB.Net

Imports iTextSharp.text

Imports iTextSharp.text.pdf

Imports iTextSharp.text.html

Imports iTextSharp.text.html.simpleparser

 

By default the iTextSharp Library does not support background color of table cells or table rows

Hence when you render it as PDF your GridView is rendered without any formatting.

Recently I read an article on hamang.net where the author has provided the snippet to modify the iTextSharp so that it exports the HTML with background color.

 

For this tutorial, I have already modified the iTextSharp Library DLL so that the GridView is rendered with all the background color used. You can refer the code for exporting GridView to PDF below

 

              

C#

protected void btnExportPDF_Click(object sender, EventArgs e)

{

    Response.ContentType = "application/pdf";

    Response.AddHeader("content-disposition",

     "attachment;filename=GridViewExport.pdf");

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;

    GridView1.DataBind();

    GridView1.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());

    Document pdfDoc = new Document(PageSize.A4, 10f,10f,10f,0f);

    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();

    htmlparser.Parse(sr);

    pdfDoc.Close();

    Response.Write(pdfDoc);

    Response.End(); 

}

 

VB.Net

Protected Sub btnExportPDF_Click(ByVal sender As Object,

ByVal e As EventArgs)

 Response.ContentType = "application/pdf"

 Response.AddHeader("content-disposition",

 "attachment;filename=GridViewExport.pdf")

 Response.Cache.SetCacheability(HttpCacheability.NoCache)

 Dim sw As New StringWriter()

 Dim hw As New HtmlTextWriter(sw)

 GridView1.AllowPaging = False

 GridView1.DataBind()

 GridView1.RenderControl(hw)

 Dim sr As New StringReader(sw.ToString())

 Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)

 Dim htmlparser As New HTMLWorker(pdfDoc)

 PdfWriter.GetInstance(pdfDoc, Response.OutputStream)

 pdfDoc.Open()

 htmlparser.Parse(sr)

 pdfDoc.Close()

 Response.Write(pdfDoc)

 Response.End()

End Sub

 

The Output Exported File



GridView data exported to PDF Document



Export to Text/CSV

 

Finally comes exporting GridView to CSV or Text File delimited by a separator like comma.

To export the GridView as CSV, I am running a two for loops. While looping through the GridView columns and appending comma after each column and while looping through rows appending new line character. Refer the code below.

 

C#

protected void btnExportCSV_Click(object sender, EventArgs e)

{

    Response.Clear();

    Response.Buffer = true;

    Response.AddHeader("content-disposition",

     "attachment;filename=GridViewExport.csv");

    Response.Charset = "";

    Response.ContentType = "application/text";

 

    GridView1.AllowPaging = false;

    GridView1.DataBind();

 

    StringBuilder sb = new StringBuilder();

    for (int k = 0; k < GridView1.Columns.Count; k++)

    {

        //add separator

        sb.Append(GridView1.Columns[k].HeaderText + ',');

    }

    //append new line

    sb.Append("\r\n");

    for (int i = 0; i < GridView1.Rows.Count; i++)

    {

        for (int k = 0; k < GridView1.Columns.Count; k++)

        {

            //add separator

            sb.Append(GridView1.Rows[i].Cells[k].Text + ',');

        }

        //append new line

        sb.Append("\r\n");

    }

    Response.Output.Write(sb.ToString());

    Response.Flush();

    Response.End();

}

      

 

VB.Net

Protected Sub btnExportCSV_Click(ByVal sender As Object,

ByVal e As EventArgs)

 Response.Clear()

 Response.Buffer = True

 Response.AddHeader("content-disposition",

 "attachment;filename=GridViewExport.csv")

 Response.Charset = ""

 Response.ContentType = "application/text"

 

 GridView1.AllowPaging = False

 GridView1.DataBind()

 

 Dim sb As New StringBuilder()

 For k As Integer = 0 To GridView1.Columns.Count - 1

  'add separator

  sb.Append(GridView1.Columns(k).HeaderText + ","c)

 Next

 'append new line

 sb.Append(vbCr & vbLf)

 For i As Integer = 0 To GridView1.Rows.Count - 1

  For k As Integer = 0 To GridView1.Columns.Count - 1

   'add separator

   sb.Append(GridView1.Rows(i).Cells(k).Text + ","c)

  Next

  'append new line

  sb.Append(vbCr & vbLf)

 Next

 Response.Output.Write(sb.ToString())

 Response.Flush()

 Response.End()

End Sub

 

The Output Exported File



GridView data exported to CSV File



When you run the application first time and click export you might receive the following error



Error encountered when you click export



To avoid the error you will need to add this event which ensures that the GridView is Rendered before exporting.

  

C#

public override void VerifyRenderingInServerForm(Control control)

{

    /* Verifies that the control is rendered */

}

 

VB.Net

Public Overloads Overrides Sub VerifyRenderingInServerForm

(ByVal control As Control)

    ' Verifies that the control is rendered

End Sub

 

This completes the article you can view the live demo here

The source code is available in C# and VB.Net here

 

GridViewExport.zip (1.15 mb)


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

karthi said:
The pdf coversion served my purpose Thank you
January 05, 2010  

duffer said:
but this is exporting whole aspx page on my machine
January 19, 2010  

danis said:
i have an error when use ur coding..can u help me..when i debug there is no error but the button export doesnt work...can u help me plz..br in my code have underline blue at StringWriter and HtmlTextWriter..br when i put this namespace the color on the text become gray and mention it using directive is not required:usingSystem.Web.UI.WebControls.WebPartsbr using System.Web.UI.HtmlControlsbr br br br StringWriter sw new StringWriter()br HtmlTextWriter hw new HtmlTextWriter(sw)
January 19, 2010  

Mudassar Khan said:
Reply To: duffer
That can only happen in cases when your HTML is invalid or incorrect make sure everything is proper and all HTML is well formed
January 20, 2010  

Mudassar Khan said:
Reply To: danis
Try to download the code sample from this site and use the code provided in it
January 20, 2010  

Chamndralekha said:
im getting only table header in the csv file while exporting gridview to csv please help me..
February 05, 2010  

Rahul said:
csv is not comming properly it is not seperated by comma and more over both excel and csv looks same only background colour differes... mudassar u have done a gud job but check this error first.
February 05, 2010  

Mudassar Khan said:
Reply To: Chamndralekha
Hi,
Debug and Check whether Your Gridview has rows in it before export
February 05, 2010  

Mudassar Khan said:
Reply To: Rahul
There is no error. To View commas open the file with notepad instead of MS Excel
February 05, 2010  

anu said:
when i use convert to pdf.i have got an error the document has no pages.
February 14, 2010  

Mudassar Khan said:
Reply To: anu
This means your Grid does not have any rows in it. Bind Gridview before exporting
February 15, 2010  

Noora said:
Thank you Mudassar for this great code and its work fine with me but the question is how can i add a title to the pdf page and make the font smaller i dont want to use a datatable for adding a title is there any other way for adding title
March 02, 2010  

Mudassar Khan said:
Reply To: Noora
hi,
You will need to refer the tutorials of iTextSharp
itextsharp.sourceforge.net/tutorial/index.html
March 02, 2010  

Noora said:
Thank you Mudassar for this great code and its work fine with me but the question is how can i add a title to the pdf page and make the font smaller i dont want to use a datatable for adding a title is there any other way for adding title
March 02, 2010  

Nedley said:
If you use AutoGenerateColumns true on the gridview the CSV export wont work because there are no columns in the collection. Hope that helps I see a few people having problems with that.
March 03, 2010  

Mudassar Khan said:
Reply To: Noora
For that you will need to use the elements of iTextSharp
http://itextsharp.sourceforge.net/tutorial/ch01.html
March 04, 2010  

alok aggarwal said:
hi sir br br a great piece of code for gridviewbr but it failed in gridview paging.br please tell how to copy the whole data till end page if paging in enabled in gridview.br br ill be highly grateful to you for your help.br br kind regardsbr alok aggarwal
March 09, 2010  

Mudassar Khan said:
Reply To: alok aggarwal
Hi,

The above code exports all the rows

GridView1.AllowPaging = false;

I have set this before exporting check above Else download the sample
March 10, 2010  

Bryan said:
When exporting to a pdf I get the following error caused by GridView1.RenderControl(hw):br br Control MainContentDonorMainContentGridView1 of type GridView must be placed inside a form tag with runatserver.br br Im using master pages with nested master pages which is what I think might be problem but Im not sure. Anyone know how to fix this
March 16, 2010  

Mudassar Khan said:
Reply To: Bryan
Refer here
http://www.aspsnippets.com/Articles/Exception---Control-GridView1-of-type-GridView-must-be-placed-inside-a-form-tag-with-runatserver.aspx
March 16, 2010  

shahed said:
The code works just fine my only concern is in the pdf format which does not refelect and display the styles (color font). The data rows are fine and accurate Pls let me know what could be the reason
March 17, 2010  

Mudassar Khan said:
Reply To: shahed
Hi,
Yes that's issue with itextsharo it does not recognize many styles.
March 18, 2010  

Vineesha M Das Kerala said:
good article....Thanks for ur helpbr but i hav 1 problembr br GridView1.AllowPaging falsebr GridView1.DataBind() br br This code doesnt working for me...all pages are not exported into excel..why this happens And since am a newbie to .net just explain the code for br protected void btnExportExcelClick(object sender EventArgs e)br br Send ur reply to my mail-id alsobr br vineesha.das@gmail.combr
March 19, 2010  

VR Hari said:
I used ur code but i am not getting the desired result. The PDF shows the pages but no data in it. What could be the problem. I am binging the GridView with SqlDatasource.br br thanks
March 22, 2010  

Mudassar Khan said:
Reply To: VR Hari
You will need to again rebind the grid before export
March 22, 2010  

Mudassar Khan said:
Reply To: Vineesha M Das Kerala
Hi,
Please download the sample and check what you are missing
March 22, 2010  

Vineesha M Das Kerala said:
THANKS for ur reply at time.br I used ur code as given. But whenever i use GridView1.AllowPaging falseGridView1.DataBind() it displays nothing in the excel sheet...it only shows something as div div .I cant understand why this happens if i comment these 2 lines Gridview.Allow...... then gridview will be displayed but the first page only not the whole page...so plz tell me some solution.........br
March 23, 2010  

Mudassar Khan said:
Reply To: Vineesha M Das Kerala
Just DataBind wont help make sure you Reassign datasource and then call databind
March 24, 2010  

Mudassar Khan said:
Reply To: don
For this download you require a full postback and hence set the Trigger to postback trigger instead of Async trigger
March 24, 2010  

Leopold said:
Thanks for this nice examples.br Have a problem with excel export.br an error occurred in linebr GridView1.RenderControl(hw)br saying:br RegisterForEventValidation can only be called at Render()br do you have an answer..Thank you
March 28, 2010  

Mudassar Khan said:
Reply To: Leopold
Set EnableEventvalidation="false" in @Page Directive
March 28, 2010  

kaushal said:
Hibr br I have used your code for pdf when I click on my pdf image button it allows me to open or save but when I do it my adobe reader gives an error that file is damaged or could not open I uninstall and reinstall adobe reader 9.0 but its not working still give same error.. HELPbr br Kaushal
April 01, 2010  

Mudassar Khan said:
Reply To: kaushal
Verify whether the HTML of your page and also check out the demo on this site because you are the first one to report such issue
April 05, 2010  

Mohsen Eslamifar said:
best article for export from gridviews in asp.net ...br all export methods worked for me.
April 10, 2010  

Deepa said:
Hibr br I got your code is working but it looks very bad such that the pdf doesnt have any coulmn header and I can barely read the values and first page it is completely blank table with several rows and next page all my data is there but no header ata ll. Can you please tell me how can I align it to read better
April 12, 2010  

Mudassar Khan said:
Reply To: Deepa
check your grid HTML whether it is proper else download the sample attached
April 14, 2010  

Suresh said:
Hyie dudebr Nice article for exporting gridview.But CSV format is not working for me wen i open the saved csv file data is empty...plz chk out
April 14, 2010  

Mudassar Khan said:
Reply To: Suresh
Thanks I just checked the demo and it is working fine for me
April 14, 2010  

mohsen said:
there is this Error:br RegisterForEventValidation can only be called during Render()br Please Help me.br Thank you
April 15, 2010  

Deepa said:
Hi I got it worked by downloading your dll. Thank you so much. This solution made my day.
April 16, 2010  

Mudassar Khan said:
Reply To: mohsen
Hi Mohsen,
Refer here
http://www.aspsnippets.com/Articles/RegisterForEventValidation-can-only-be-called-during-Render.aspx
April 17, 2010  

Nguyen Bao said:
Hi Mudassar Khanbr I have a problem when my gridview have a picture i can export picture in gridview into excel. plz help ..
April 22, 2010  

sona said:
Im still facing that problem of document has no pages . Can anybody pls help.
April 22, 2010  

Mudassar Khan said:
Reply To: Nguyen Bao
Check this
http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx
April 22, 2010  

Mudassar Khan said:
Reply To: sona
Make sure your Gridview has rows when exporting. You will need to rebind Gridview with data before export
April 22, 2010  

sona said:
Hi Mudassar Khanbr Thank you for your fast response. Actually in my requirement im using other controls(may be textboxes and dropdowns) in the page and trying to export the whole page to pdf. Do you have any idea on that.
April 22, 2010  

Mudassar Khan said:
Reply To: sona
For that you will have to convert textboxes and otehr controls to label and the export. Post your code on forums.asp.net. You will get help there. I am also member there
April 23, 2010  

charles said:
hi mudassar khanbr i checked your code and its very helpful to my work i was just wondering why your export to pdf contained the same result with the html page but when i tried to copy the code it did not show a pdf with the correct colors only plain text. br thanks.
April 26, 2010  

Mudassar Khan said:
Reply To: charles
Please download the sample code I am sure it will work for you
April 26, 2010  

Kumaravel said:
Hi Mudassar Khanbr Sorry to disturb again I am having another question. br Instead of GridView can we use the Panel or PlaceHolder to generate the Report formatsbr I tried for excel its working but for PDF format the PlaceHolder is not working. Please can you tell me the details
May 03, 2010  

Mudassar Khan said:
Reply To: Kumaravel
Yes panel should work as it is rendered as DIV
May 03, 2010  

Kumaravel said:
Hi Mudassar Khanbrbr I tried with Panel and PlaceHolder to export data to PDF format. But for both it showing error as System.FormatException: Input string was not in a correct format.. Can you please tell the solution for it
May 04, 2010  

Mudassar Khan said:
Reply To: Kumaravel
I think you need to post your code on asp.net forums I am sure you will get help there
May 07, 2010  

Lutch said:
hi Mudassarbr i have tried to export the gridview to pdf but im getting the error RegisterForEventValidation can be called during Render() and the GridView1.RenderControl(hw) is highlighted.please help me with that..thanks
May 08, 2010  

Maximilien Anakambi said:
Hello Mudassarbr br I just found your tutorial and went through it (reading) but I did not test it yet. So far this website is producing something deep in my feeling such that I will go and test the application if it produces what it seems to I will come back and wash your feet because you just put everything I was after in one package.br br Regardsbr br Max
May 11, 2010  

Mudassar Khan said:
Reply To: Maximilien Anakambi
Yes it will it is one of the oldest articles on this site and you can also try the demo

Thx
May 11, 2010  

Mudassar Khan said:
Reply To: Lutch
Refer here
http://www.aspsnippets.com/Articles/RegisterForEventValidation-can-only-be-called-during-Render.aspx
May 11, 2010  

VR Hair said:
Using the iTextSharp if you change the font size of the Gridview then the output PDF shows nothing.It works only with the dufault fontsize. I think the same is the problem with other formats docxls change in fontsize of the gridview is not reflected in the output document.Any remedy to this thanks..
May 12, 2010  

Mudassar Khan said:
Reply To: VR Hair
Only way I think is making use if iTextsharp table as shown here
http://www.aspsnippets.com/Articles/Export-ASP.Net-GridView-to-PDF-with-Custom-Columns-Widths-using-iTextSharp.aspx
May 15, 2010  

Milad said:
Hello I use your Code for Convert Gridview to PDF but when pdf created contain a empty tablebr i use databind and datasource for gridview but no result...br br Please Help me
May 22, 2010  

Mudassar Khan said:
Reply To: Milad
Make sure you bind the grid before export. Download and check the attached sample to see if that works fine
May 25, 2010  

Bhanu said:
Having issues in generating CSV file.br br brbr I am using Template field.br It doesnt read my columns Item template where the text is stored.br brbrbr It is able to read Header text though.br brbr I am using Item Template in GridViewbr brbrbr
June 01, 2010  

Mudassar Khan said:
Reply To: Bhanu
For such cases where iTextsharp does not support controls you will need to create your own table and then export
http://www.aspsnippets.com/Articles/Export-ASP.Net-GridView-to-PDF-with-Custom-Columns-Widths-using-iTextSharp.aspx
June 01, 2010  

Hiren Doshi said:
I am Save Pdf File in My System.br But when You Open at that time error is Adobe Reader could not open Mypdffile.pdf because either it is not a supported filetype or because the file has been damaged(for a exampleit was sent as an email attachment and wasnt correctly decoded).
June 25, 2010  

Satya said:
Hi Mudassarbr Thanks for such nice explanation.
June 26, 2010  

Mudassar Khan said:
Reply To: Hiren Doshi
Make sure you are applying thr right content type and extension
June 26, 2010  

Ashish said:
you guys rock like hell
July 01, 2010  

dave said:
Hi is it possible to encase the fields within some of my columns contain commas and exporting to a csv tends to break thingsbr br tabr Dave
July 05, 2010  

Mudassar Khan said:
Reply To: dave
you will need to encode commas before exporting
July 06, 2010  

Dave said:
Extremely helpful well written Thanks
July 15, 2010  

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code