--Export to Excel:--
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
protected void btnexporttoexcel_click(object sender, EventArgs e)
{
string attachment = "attachment; filename=ContractorDetails.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gvclientcontractordetails.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gvclientcontractordetails);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
--Export to Word:---
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
protected void btnexporttoexcel_click(object sender, EventArgs e)
{
string attachment = "attachment; filename=ContractorDetails.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gvclientcontractordetails.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gvclientcontractordetails);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
--Export to Word:---
Response.AddHeader("content-disposition",
"attachment;filename=Export.doc");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType =
"application/vnd.word";
StringWriter stringWrite = new
StringWriter();
HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] =
"server";
frm.Controls.Add(gv);
frm.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
--Export to PDF--
Response.ContentType =
"application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=Export.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new
StringWriter();
HtmlTextWriter hw = new
HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] =
"server";
frm.Controls.Add(gv);
frm.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();
No comments:
Post a Comment