Wednesday, August 30, 2017

office/Export GridView to

Download

Introduction

This Sample Describes How to Export Gridview in a doc/access/excel/html/xml/text/pdf/csv/print format.

Building the Sample

To export GridView in PDF format it requires ITextSharp Library. The download link of this library is given below.

itextsharp.dll

Description

This Sample of Application describes how to export Gridview in a different file format.

1) Export GridView to Word Document.

2) Export GridView to Excel Document.

3) Export GridView to Access Document.

4) Export GridView to XML File.

5) Export GridView to PDF File.

6) Export GridView to CSV File.

7) Export GridView to HTML File.

8) Export GridView to Text File.

9) Export GridView to Print.


Here I explain How to Export GridView in a different file Format using ASP.NET and C#.

Export GridView to Print File Format using JavaScript.

JavaScript
Edit|Remove
<script type="text/javascript">       function PrintGridData() {           var printGrid = document.getElementById('<%=GridView1.ClientID %>');           var printwin = window.open('''PrintGridView''left=100,top=100,width=400,height=400,tollbar=0,scrollbars=1,status=0,resizable=1');           printwin.document.write(printGrid.outerHTML);           printwin.document.close();           printwin.focus();           printwin.print();           printwin.close();       }      </script> 
 Export GridView to PDF File.
 
  To export GridView to PDF File you require itextsharp library. which is open source and you can download from this website.
Include the namespace.
C#
Edit|Remove
using System.Textusing iTextSharp.text;   using iTextSharp.text.pdf;   using iTextSharp.text.html;   using iTextSharp.text.html.simpleparser;
 
C#
Edit|Remove
 protected void btnExportPDF_Click(object sender, EventArgs e)           {               Response.ContentType = "application/pdf";               Response.AddHeader("content-disposition""attachment;filename=Student.pdf");               Response.Cache.SetCacheability(HttpCacheability.NoCache);               StringWriter sw = new StringWriter();               HtmlTextWriter hw = new HtmlTextWriter(sw);               GridView1.AllowPaging = false;               HtmlForm frm = new HtmlForm();               GridView1.Parent.Controls.Add(frm);               frm.Attributes["runat"] = "server";               frm.Controls.Add(GridView1);               frm.RenderControl(hw);               GridView1.DataBind();               StringReader sr = new StringReader(sw.ToString());               iTextSharp.text.Document pdfDoc = new iTextSharp.text.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();           }
 Export GridView to Wod File.
C#
Edit|Remove
protected void btnWord_Click(object sender, EventArgs e)           {               HtmlForm form = new HtmlForm();               Response.Clear();               Response.Buffer = true;               Response.Charset = "";               Response.AddHeader("content-disposition"string.Format("attachment;filename={0}""Student.doc"));               Response.ContentType = "application/ms-msword";               StringWriter sw = new StringWriter();               HtmlTextWriter hw = new HtmlTextWriter(sw);               GridView1.AllowPaging = false;               BindGridDetails(GridView1);               form.Attributes["runat"] = "server";               form.Controls.Add(GridView1);               this.Controls.Add(form);               form.RenderControl(hw);               string style = @"";               Response.Write(style);               Response.Output.Write(sw.ToString());               Response.Flush();               Response.End();           }
 

Export GridView to Excel File.

C#
Edit|Remove
protected void btnExcelExport_Click(object sender, EventArgs e)           {               HtmlForm form = new HtmlForm();               Response.Clear();               Response.Buffer = true;               Response.Charset = "";               Response.AddHeader("content-disposition"string.Format("attachment;filename={0}""Student.xls"));               Response.ContentType = "application/ms-excel";               StringWriter sw = new StringWriter();               HtmlTextWriter hw = new HtmlTextWriter(sw);               GridView1.AllowPaging = false;               BindGridDetails(GridView1);               form.Attributes["runat"] = "server";               form.Controls.Add(GridView1);               this.Controls.Add(form);               form.RenderControl(hw);               string style = @"";               Response.Write(style);               Response.Output.Write(sw.ToString());               Response.Flush();               Response.End();           }
Export GridView to Access File.
C#
Edit|Remove
 protected void btnAccess_Click(object sender, EventArgs e)           {               HtmlForm form = new HtmlForm();               GridView1.AllowPaging = false;               //GridView1.DataBind();               BindGridDetails();               Response.ClearContent();               Response.AddHeader("content-disposition"string.Format("attachment; filename={0}""Customers.mdb"));               Response.Charset = "";               Response.ContentType = "application/ms-access";               StringWriter sw = new StringWriter();               HtmlTextWriter htw = new HtmlTextWriter(sw);               // GridView1.RenderControl(htw);               form.Attributes["runat"] = "server";               form.Controls.Add(GridView1);               this.Controls.Add(form);               Form.RenderControl(htw);               Response.Write(sw.ToString());               Response.Flush();               Response.End();           }
 
Export GridView to CSV File.
C#
Edit|Remove
 protected void btnExportCSV_Click(object sender, EventArgs e)           {               BindGridDetails(GridView1);                   Response.Clear();                   Response.Buffer = true;                   Response.AddHeader("content-disposition","attachment;filename=Student.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();           }
 

Export GridView to XML File

 

C#
Edit|Remove
protected void btnXML_Click(object sender, EventArgs e)           {               Thread staThread = new Thread(new ThreadStart(XMLExport));               staThread.ApartmentState = ApartmentState.STA;               staThread.Start();           }              public void XMLExport()           {               SaveFileDialog saveDialog = new SaveFileDialog();               saveDialog.Filter = "Xml files (*.xml)|*.xml";               saveDialog.FilterIndex = 2;               saveDialog.RestoreDirectory = true;               saveDialog.InitialDirectory = "c:\\";               saveDialog.FileName = "Student";               saveDialog.Title = "XML Export";               if (saveDialog.ShowDialog() == DialogResult.OK)               {                   BindGridDetails(GridView1);                   DataSet ds = new DataSet();                  DataTable dt = (DataTable)GridView1.DataSource;                   ds.Tables.Add(dt);                   ds.WriteXml(File.OpenWrite(saveDialog.FileName));               }            }
 

 

Export GridView to HTML File

C#
Edit|Remove
  protected void btnHTML_Click(object sender, EventArgs e)           {               HtmlForm form = new HtmlForm();               GridView1.AllowPaging = false;               BindGridDetails(GridView1);               Response.ClearContent();               Response.AddHeader("content-disposition"string.Format("attachment; filename={0}""Student.html"));               Response.Charset = "";               Response.ContentType = "text/html";               StringWriter sw = new StringWriter();               HtmlTextWriter htw = new HtmlTextWriter(sw);               form.Attributes["runat"] = "server";               form.Controls.Add(GridView1);               this.Controls.Add(form);               Form.RenderControl(htw);               Response.Write(sw.ToString());               Response.Flush();               Response.End();           }

Source Code Files

  • Source Code C#
  • source code file name #2 - summary for this source code file.

More Information

For more information on X, see ...?

No comments:

Post a Comment