To export GridView in PDF format it requires ITextSharp Library. The download link of this library is given below.
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
<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#
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
C#
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#
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 = @"<!--mce:0-->";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Export GridView to Excel File.
C#
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 = @"<!--mce:2-->";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
Export GridView to Access File.
C#
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#
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#
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#
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();
}
Sample File:Click here