Export From Multiple Grid to Excel and PDF Formate
protected void btn_Exportxecel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Export.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter StringWriter = new System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
HtmlTextWriter.Write("<table width='100%'><tr><td colspan='9' align='center'><b>" + "Personal Detail" + "</b></td></tr></table>");
Grid_PersonalDetail.RenderControl(HtmlTextWriter);
HtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Br);
HtmlTextWriter.Write("<table width='100%'><tr><td colspan='9' align='center'><b>" + "Address Deteail" + "</b></td></tr></table>");
gridadress.RenderControl(HtmlTextWriter);
HtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Br);
HtmlTextWriter.Write("<table width='100%'><tr><td colspan='9' align='center'><b>" + "Invoice Detail" + "</b></td></tr></table>");
grd_Invoicedetail.RenderControl(HtmlTextWriter);
HtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Br);
HtmlTextWriter.Write("<table width='100%'><tr><td colspan='9' align='center'><b>" + "Product Detail" + "</b></td></tr></table>");
grd_product.RenderControl(HtmlTextWriter);
HtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Br);
HtmlTextWriter.Write("<table width='100%'><tr><td colspan='9' align='center'><b>" + "AMC/CMC Detail" + "</b></td></tr></table>");
grd_AMC.RenderControl(HtmlTextWriter);
HtmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Br);
HtmlTextWriter.Write("<table width='100%'><tr><td colspan='9' align='center'><b>" + "Service Detail" + "</b></td></tr></table>");
grid_Service.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
}
protected void btn_exporttoPdf_Click(object sender, EventArgs e)
{
CssClass_GridView(gridadress);
StringWriter sw0 = new StringWriter();
HtmlTextWriter hw0 = new HtmlTextWriter(sw0);
if (gridadress != null && gridadress.Rows.Count > 0)
{
hw0.Write("<table width='100%'><tr ><td><font size='5' color='white' align='left' style='text-align:left;'>");
hw0.Write("Expense Record");
hw0.Write("</font></td></tr>");
hw0.Write("<tr><td>");
hw0.Write("<font size='1' align='left' style='text-align:left;'>");
StringWriter sw2 = new StringWriter();
//EnableEventValidation = "false";
//this.EnableViewState = False;
sw2.Write("<table width='100%' Font-Bold='true' Font-Size='Large'><tr><td colspan='9' align='center'><b>" + "Personal Detail" + "</b></td></tr></table>");
Grid_PersonalDetail.RenderControl(new HtmlTextWriter(sw2));
sw2.Write("<table width='100%' Font-Bold='true' Font-Size='Large'><tr><td colspan='9'></td></tr></table>");
gridadress.RenderControl(new HtmlTextWriter(sw2));
sw2.Write("<table width='100%' Font-Bold='true' Font-Size='Large'><tr><td colspan='9' align='center'><b>" + "Invoice Deteail" + "</b></td></tr></table>");
grd_Invoicedetail.RenderControl(new HtmlTextWriter(sw2));
sw2.Write("<table width='100%' Font-Bold='true' Font-Size='Large'><tr><td colspan='9' align='center'><b>" + "Product Deteail" + "</b></td></tr></table>");
grd_product.RenderControl(new HtmlTextWriter(sw2));
sw2.Write("<table width='100%' Font-Bold='true' Font-Size='Large'><tr><td colspan='9' align='center'><b>" + "AMC/CMC Deteail" + "</b></td></tr></table>");
grd_AMC.RenderControl(new HtmlTextWriter(sw2));
sw2.Write("<table width='100%' Font-Bold='true' Font-Size='Large'><tr><td colspan='9' align='center'><b>" + "Service Deteail" + "</b></td></tr></table>");
grid_Service.RenderControl(new HtmlTextWriter(sw2));
string html = sw2.ToString();
hw0.Write(html);
hw0.Write("</font>");
hw0.Write("</td></tr></table>");
StringReader sr = new StringReader(sw0.ToString().Replace("%20", " "));
Document pdfDoc = new Document(PageSize.A4, 9f, 9f, 9f, 0f);
//
iTextSharp.text.html.simpleparser.HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc);
string filename = Session["UserName"].ToString() + "_Details_" + DateTime.Now.Ticks + ".pdf";
string file_Path = "~\\ExportFiles\\" + filename;
PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath(file_Path), FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
// Response.Redirect("../ExportFiles/" + filename);
Response.Redirect("../ExportFiles/" + filename, "_blank", "menubar=0,scrollbars=1,width=780,height=900,top=10");
}
else
{
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", "alert('There Is No Data To Export Here. Please Fill Data Firstly....')", true);
}
}
protected void CssClass_GridView(GridView NewGrid)
{
NewGrid.Style.Add("width", "100%");
NewGrid.Attributes.Add("ShowFooter", "True");
NewGrid.Attributes.Add("CellPadding", "1");
NewGrid.Font.Size = 10;
NewGrid.ForeColor = System.Drawing.Color.FromArgb(51, 51, 51);
NewGrid.GridLines = GridLines.None;
NewGrid.ShowFooter = true;
NewGrid.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
NewGrid.EditRowStyle.BackColor = System.Drawing.Color.FromArgb(36, 97, 191);
NewGrid.FooterStyle.BackColor = System.Drawing.Color.FromArgb(80, 124, 209);
NewGrid.FooterStyle.Font.Bold = true;
NewGrid.FooterStyle.ForeColor = System.Drawing.Color.White;
NewGrid.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(80, 124, 209);
NewGrid.HeaderStyle.Font.Bold = true;
NewGrid.HeaderStyle.ForeColor = System.Drawing.Color.White;
NewGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
NewGrid.RowStyle.BackColor = System.Drawing.Color.FromArgb(239, 243, 251);
NewGrid.RowStyle.HorizontalAlign = HorizontalAlign.Left;
}
No comments:
Post a Comment