//////////////////////////// Use Namespace
//////////////////////////// Excel format
//////////////////////////// CSV format
//////////////////////////// MS Word format
//////////////////////////// PDF format
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
//////////////////////////// Excel format
protected void Btn_ExportToExcel(object
sender, EventArgs e)
{
string strQuery = "select
CustomerID, ContactName, City, PostalCode from customers";
SqlCommand cmd = new
SqlCommand(strQuery);
DataTable dt = GetData_Gridview(cmd);
//Create a dummy GridView
GridView GridView1 = new
GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=Result.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new
StringWriter();
HtmlTextWriter hw = new
HtmlTextWriter(sw);
for (int i = 0; i
< GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Attributes.Add("class",
"textmode");
}
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();
}
//////////////////////////// CSV format
protected void
Btn_ExportToCSV(object sender, EventArgs e)
{
string strQuery = "select
CustomerID, ContactName, City, PostalCode from customers";
SqlCommand cmd = new
SqlCommand(strQuery);
DataTable dt = GetData_Gridview(cmd);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=Result.csv");
Response.Charset = "";
Response.ContentType = "application/text";
StringBuilder sb = new
StringBuilder();
for (int k = 0; k
< dt.Columns.Count; k++)
{
//add separator
sb.Append(dt.Columns[k].ColumnName + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i
< dt.Rows.Count; i++)
{
for (int k = 0; k
< dt.Columns.Count; k++)
{
//add separator
sb.Append(dt.Rows[i][k].ToString().Replace(",",
";") + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
//////////////////////////// MS Word format
protected void
Btn_ExportToMSWord(object sender, EventArgs e)
{
string strQuery = "select
CustomerID, ContactName, City, PostalCode from customers";
SqlCommand cmd = new
SqlCommand(strQuery);
DataTable dt = GetData_Gridview(cmd);
//Create a dummy GridView
GridView GridView1 = new
GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=Result.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word
";
StringWriter sw = new
StringWriter();
HtmlTextWriter hw = new
HtmlTextWriter(sw);
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
//////////////////////////// PDF format
protected void
Btn_ExportToPFD(object sender, EventArgs e)
{
string strQuery = "select
CustomerID, ContactName, City, PostalCode from customers";
SqlCommand cmd = new
SqlCommand(strQuery);
DataTable dt = GetData_Gridview(cmd);
//Create a dummy GridView
GridView GridView1 = new
GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition","attachment;filename=Result.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new
StringWriter();
HtmlTextWriter hw = new
HtmlTextWriter(sw);
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();
}
No comments:
Post a Comment