Code to export component art grid to pdf
private void Page_Load(System.Object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
Grid1.DataSource = buildGrid();
Grid1.DataBind();
}
}
public System.Data.DataSet buildGrid()
{
string conStr = null;
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
conStr += Server.MapPath("~/app_data/demo.mdb");
System.Data.OleDb.OleDbConnection dbCon = new System.Data.OleDb.OleDbConnection(conStr);
dbCon.Open();
string sql = null;
sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC";
System.Data.OleDb.OleDbDataAdapter dbAdapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbCon);
DataSet ds = new DataSet();
dbAdapter.Fill(ds);
return ds;
}
protected void btnExport_Click(object sender, System.EventArgs e)
{
ExportToExcel(buildGrid(), 0, Response, "MySheet-" + DateTime.Today.ToString("MM-dd-yyyy"));
}
public void ExportToExcel(DataSet dSet, int TableIndex, HttpResponse Response, string FileName)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
GridView gv = new GridView();
gv.DataSource = dSet.Tables[TableIndex];
gv.HeaderStyle.Font.Size = new FontUnit(4);
gv.RowStyle.Font.Size = new FontUnit(4);
gv.DataBind();
for (int x = 0; x < gv.Columns.Count; x++)
{
gv.Columns[x].ItemStyle.Width = Unit.Pixel(100);
}
gv.RenderControl(hw);
Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\myPDF.pdf", FileMode.Create));
HTMLWorker htmlparser = new HTMLWorker(Doc);
Doc.Open();
Chunk c = new Chunk("PDF EXPORT OF POSTS \n", FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk("By ComponentArt - www.componentart.com\n\n\n", FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
Doc.Add(p);
Doc.Add(p1);
System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(new StringReader(sw.ToString()));
htmlparser.Parse(new StringReader(sw.ToString()));
Doc.Close();
string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\myPDF.pdf";
ShowPdf(Path);
}
private void ShowPdf(string strS)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);
Response.TransmitFile(strS);
Response.End();
//Response.WriteFile(strS);
Response.Flush();
Response.Clear();
}
private void Page_Load(System.Object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
Grid1.DataSource = buildGrid();
Grid1.DataBind();
}
}
public System.Data.DataSet buildGrid()
{
string conStr = null;
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
conStr += Server.MapPath("~/app_data/demo.mdb");
System.Data.OleDb.OleDbConnection dbCon = new System.Data.OleDb.OleDbConnection(conStr);
dbCon.Open();
string sql = null;
sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC";
System.Data.OleDb.OleDbDataAdapter dbAdapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbCon);
DataSet ds = new DataSet();
dbAdapter.Fill(ds);
return ds;
}
protected void btnExport_Click(object sender, System.EventArgs e)
{
ExportToExcel(buildGrid(), 0, Response, "MySheet-" + DateTime.Today.ToString("MM-dd-yyyy"));
}
public void ExportToExcel(DataSet dSet, int TableIndex, HttpResponse Response, string FileName)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
GridView gv = new GridView();
gv.DataSource = dSet.Tables[TableIndex];
gv.HeaderStyle.Font.Size = new FontUnit(4);
gv.RowStyle.Font.Size = new FontUnit(4);
gv.DataBind();
for (int x = 0; x < gv.Columns.Count; x++)
{
gv.Columns[x].ItemStyle.Width = Unit.Pixel(100);
}
gv.RenderControl(hw);
Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\myPDF.pdf", FileMode.Create));
HTMLWorker htmlparser = new HTMLWorker(Doc);
Doc.Open();
Chunk c = new Chunk("PDF EXPORT OF POSTS \n", FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk("By ComponentArt - www.componentart.com\n\n\n", FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
Doc.Add(p);
Doc.Add(p1);
System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(new StringReader(sw.ToString()));
htmlparser.Parse(new StringReader(sw.ToString()));
Doc.Close();
string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\myPDF.pdf";
ShowPdf(Path);
}
private void ShowPdf(string strS)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);
Response.TransmitFile(strS);
Response.End();
//Response.WriteFile(strS);
Response.Flush();
Response.Clear();
}
No comments:
Post a Comment