Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use a snippet.</p> <p><a href="http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx" rel="nofollow"><strong>This one</strong></a> is great. Take a look on it.</p> <p>This is an example of usage:</p> <p><strong>HTML</strong></p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" Font-Names = "Arial" Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" HeaderStyle-BackColor = "green" AllowPaging ="true" OnPageIndexChanging = "OnPaging" &gt; &lt;Columns&gt; &lt;asp:BoundField ItemStyle-Width = "150px" DataField = "CustomerID" HeaderText = "CustomerID" /&gt; &lt;asp:BoundField ItemStyle-Width = "150px" DataField = "City" HeaderText = "City"/&gt; &lt;asp:BoundField ItemStyle-Width = "150px" DataField = "Country" HeaderText = "Country"/&gt; &lt;asp:BoundField ItemStyle-Width = "150px" DataField = "PostalCode" HeaderText = "PostalCode"/&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p><strong>C#</strong> for pdf example</p> <pre><code>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(); } </code></pre> <p><strong>C#</strong> for excel example</p> <pre><code>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 &lt; 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 = @"&lt;style&gt; .textmode { mso-number-format:\@; } &lt;/style&gt;"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload