public static void xlsport(GridView grd, string _fnm, Page p) { grd.Visible = true; gridstrip(grd); grd.DataBind(); HtmlForm form = new HtmlForm(); string attachment = "attachment; filename=" + _fnm + csclass.ranfn() + ".xls"; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; p.EnableViewState = false; HttpContext.Current.Response.Charset = string.Empty; System.IO.StringWriter stw = new System.IO.StringWriter(); HtmlTextWriter htextw = new HtmlTextWriter(stw); form.Controls.Add(grd); p.Controls.Add(form); form.RenderControl(htextw); HttpContext.Current.Response.Write(stw.ToString()); HttpContext.Current.Response.End(); }
/// <summary> /// Export Products data to Excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnExport_Click(object sender, EventArgs e) { try { gdvInboundProducts.Columns[gdvInboundProducts.Columns.Count - 1].Visible = false; gdvInboundProducts.AllowPaging = false; gdvInboundProducts.EditIndex = -1; GetProducts(); Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=InboudTracking.xls"); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); Controls.Add(form); form.Controls.Add(gdvInboundProducts); form.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End(); gdvInboundProducts.Columns[gdvInboundProducts.Columns.Count - 1].Visible = true; gdvInboundProducts.AllowPaging = true; } catch (Exception ex) { EventLogProvider.LogException("Export data to excel", "btnExport_Click()", ex, CurrentSite.SiteID, ex.Message); } }
protected void btnexporttoexc_Click(object sender, EventArgs e) { try { string sysDates = DateTime.Now.ToString("dd/MM/yyyy"); string FileName = sysDates; string strFileName = FileName + ".xls"; btnSearch_Click(sender, e); //GridViewExportUtil.ExportExcell(strFileName, Grdiworkreg); string attachment = "attachment; filename=" + strFileName + " "; 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(); GridView2.AllowPaging = false; GridView2.Parent.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(GridView2); frm.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } catch (Exception ex) { ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + ex.Message + "');", true); } }
protected void ButtonDownload_Click(object sender, EventArgs e) { string filename = String.Format("Results_{0}_{1}.xls", DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString()); if (!string.IsNullOrEmpty(GridView1.Page.Title)) { filename = "Export_" + DateTime.Today.ToString() + ".xls"; } HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.Charset = ""; System.IO.StringWriter stringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); GridView1.Parent.Controls.Add(form); form.Controls.Add(GridView1); form.RenderControl(htmlWriter); HttpContext.Current.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); HttpContext.Current.Response.Write(stringWriter.ToString()); HttpContext.Current.Response.End(); }
public void ExportGridviewToExcel(GridView GridViewexp) { // Reference your own GridView here string filename = String.Format("Quotations_{0}_{1}_{2}.xls", DateTime.Today.Day.ToString(), DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString()); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); HttpContext.Current.Response.Charset = ""; // SetCacheability doesn't seem to make a difference (see update) HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); HttpContext.Current.Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); // Replace all gridview controls with literals GridViewexp.BackColor = System.Drawing.Color.WhiteSmoke; GridViewRow grh = GridViewexp.HeaderRow; grh.BackColor = System.Drawing.Color.Teal; grh.ForeColor = System.Drawing.Color.White; foreach (TableCell cl in grh.Cells) { PrepareControlForExport_GridView(cl); cl.BackColor = System.Drawing.Color.Teal; cl.ForeColor = System.Drawing.Color.White; } foreach (GridViewRow gr in GridViewexp.Rows) { gr.Cells[0].Visible = true; foreach (TableCell cl in gr.Cells) { PrepareControlForExport_GridView(cl); } } System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); Controls.Add(form); form.Controls.Add(GridViewexp); form.RenderControl(htmlWriter); HttpContext.Current.Response.Write(stringWriter.ToString()); HttpContext.Current.Response.End(); }
protected void btnExportErrors_Clicked(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename=ImportErrorList.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.xls"; StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); HtmlForm newForm = new HtmlForm(); this.Controls.Add(newForm); newForm.Controls.Add(gvErrorDetails); newForm.RenderControl(htmlWrite); Response.Write(stringWrite.ToString()); Response.End(); this.Dispose(); }
public void ExportWord(System.Web.UI.WebControls.GridView exportGV, System.Web.HttpResponse Response, System.Web.UI.HtmlControls.HtmlForm htmlForm) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=Export.doc"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-word "; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); exportGV.Parent.Controls.Add(htmlForm); htmlForm.Attributes["runat"] = "server"; htmlForm.Controls.Add(exportGV); htmlForm.RenderControl(hw); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); }
protected void btnExcel_Click(object sender, EventArgs e) { HtmlForm form = new HtmlForm(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.ContentType = "application/ms-excel"; Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Times New Roman;'>"); HttpContext.Current.Response.Write("<BR><BR><BR>"); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); form.Controls.Add(grDSSum); this.Controls.Add(form); form.RenderControl(hw); Response.Write(sw.ToString()); Response.End(); }
protected void btXuatExcel_Click(object sender, EventArgs e) { HtmlForm form = new HtmlForm(); string fileName = string.Format("BaoCao_SuCo_NgungGiam_{0}", DateTime.Now.ToString("MMyyyy_ddhhmm")); Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls"); Response.ContentType = "application/ms-excel"; Response.Charset = string.Empty; Response.ContentEncoding = Encoding.Unicode; Response.BinaryWrite(Encoding.Unicode.GetPreamble()); StringWriter strWri = new StringWriter(); HtmlTextWriter txtWri = new HtmlTextWriter(strWri); Load_Data(); form.Controls.Add(gvSuCo); this.Controls.Add(form); form.RenderControl(txtWri); Response.Write(strWri.ToString()); Response.End(); }
/// <summary> /// Exporte la GridView en Excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btn_export_excel_Click(object sender, EventArgs e) { // Reference your own GridView here if (gvw_assiduite.Rows.Count > 65535) { throw new Exception("Trop de colonnes"); } string filename = String.Format("General_Attendance_{1}.xls", DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString()); Response.Clear(); Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); Response.Charset = ""; // SetCacheability doesn't seem to make a difference (see update) Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); // Replace all gridview controls with literals ClearControls(gvw_assiduite); // Throws exception: Control 'ComputerGrid' of type 'GridView' // must be placed inside a form tag with runat=server. // ComputerGrid.RenderControl(htmlWrite); // Alternate to ComputerGrid.RenderControl above System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); Controls.Add(form); form.Controls.Add(gvw_assiduite); form.RenderControl(htmlWriter); Response.Write(stringWriter.ToString()); Response.End(); }
public static void Export(WebControl grid, Page page, string Filename, bool treatAsText, params int[] textColumns) { if ((treatAsText)) { try { foreach (GridViewRow row in ((GridView)grid).Rows) if (textColumns == null || textColumns.Length == 0) { foreach (TableCell cell in row.Cells) cell.Attributes.Add("class", "text"); } else { foreach (int i in textColumns) row.Cells[i].Attributes.Add("class", "text"); } } catch { } } HttpResponse response = HttpContext.Current.Response; response.Clear(); response.Buffer = true; response.AddHeader("content-disposition", string.Concat("attachment;filename=", Filename)); response.ContentEncoding = Encoding.Unicode; response.BinaryWrite(Encoding.Unicode.GetPreamble()); response.Cache.SetCacheability(HttpCacheability.NoCache); response.ContentType = "application/vnd.ms-excel"; page.EnableViewState = false; HtmlForm frm = new HtmlForm(); page.Controls.Add(frm); frm.Controls.Add(grid); StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); frm.RenderControl(htmlWrite); response.Write(@"<style>.text { mso-number-format:\@; }</style>"); response.Write(stringWrite.ToString()); response.Flush(); response.End(); }
protected void doPdfButton_Click(object sender, EventArgs e) { 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(); panel1.Parent.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(panel1); 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(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }
protected void btLuu_Click(object sender, EventArgs e) { try { HtmlForm form = new HtmlForm(); string fileName = string.Format("CORE_{0}", DateTime.Now.ToString("yyMMdd_hhmmss")); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls"); Response.ContentType = "application/ms-excel"; Response.Charset = string.Empty; Response.ContentEncoding = Encoding.Unicode; Response.BinaryWrite(Encoding.Unicode.GetPreamble()); StringWriter strWri = new StringWriter(); HtmlTextWriter txtWri = new HtmlTextWriter(strWri); form.Controls.Add(gvCore); this.Controls.Add(form); form.RenderControl(txtWri); Response.Write(strWri.ToString()); Response.End(); } catch { } }
protected void btnExport_Click(object sender, EventArgs e) { HtmlForm form = new HtmlForm(); Response.Clear(); Response.Buffer = true; string filename="GridViewExport_"+DateTime.Now.ToString()+".xls"; Response.AddHeader("content-disposition","attachment;filename="+filename); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); gvParent.AllowPaging = false; //gvParent.DataBind(); form.Controls.Add(gvParent); this.Controls.Add(form); form.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(); }
public void ExportExcel(System.Web.UI.WebControls.GridView exportGV, System.Web.HttpResponse Response, System.Web.UI.HtmlControls.HtmlForm htmlForm) { exportGV.AllowPaging = false; exportGV.Visible = true; string strFileName = "XcelReport-" + System.DateTime.Now.Date.ToString("dd") + System.DateTime.Now.AddMonths(0).ToString("MM") + System.DateTime.Now.AddYears(0).ToString("yyyy"); string attachment = "attachment; filename=" + strFileName + ".xls"; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); // Create a form to contain the grid exportGV.Parent.Controls.Add(htmlForm); htmlForm.Attributes["runat"] = "server"; htmlForm.Controls.Add(exportGV); htmlForm.RenderControl(htw); Response.Write(sw.ToString()); exportGV.Visible = false; Response.End(); }
protected void btnWord_Click(object sender, EventArgs e) { HtmlForm form = new HtmlForm(); //Response.Clear(); //Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc"); Response.ContentType = "application/ms-word"; Response.Charset = "UTF-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; //set font HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>"); HttpContext.Current.Response.Write("<BR><BR><BR>"); //Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble()); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); // Bỏ phân trang - Nếu chỉ muỗn Export Trang hiện hành thì chọn =true form.Controls.Add(grDSSum); this.Controls.Add(form); form.RenderControl(hw); //grDSSum.RenderControl(hw); //Thay đổi Style hw.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"); Response.Write(sw.ToString()); //Response.Flush(); Response.End(); }
/// <summary> /// To Export gridview data to excel /// </summary> /// <param name="GridViewexp"> Grid view name </param> /// <param name="ExportHeader"> Dislay Header in excel </param> public void ExportGridviewToExcel(GridView GridViewexp, string ExportHeader) { try { string filename = String.Format("Vetting_{0}_{1}_{2}.xls", DateTime.Today.Day.ToString(), DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString()); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); HttpContext.Current.Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); // Replace all gridview controls with literals GridViewRow grh = GridViewexp.HeaderRow; grh.ForeColor = System.Drawing.Color.Black; grh.HorizontalAlign = HorizontalAlign.Left; GridViewexp.GridLines = GridLines.Both; grh.Cells[11].Visible = false; foreach (TableCell cl in grh.Cells) { cl.HorizontalAlign = HorizontalAlign.Left; cl.Attributes.Add("class", "text"); PrepareControlForExport_GridView(cl); } foreach (GridViewRow gr in GridViewexp.Rows) { gr.Cells[11].Visible = false; foreach (TableCell cl in gr.Cells) { cl.HorizontalAlign = HorizontalAlign.Left; cl.Attributes.Add("class", "text"); PrepareControlForExport_GridView(cl); } } System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); Controls.Add(form); Label lbl = new Label(); lbl.Text = ExportHeader; lbl.Font.Size = 14; lbl.Font.Bold = true; form.Controls.Add(lbl); form.Controls.Add(GridViewexp); form.RenderControl(htmlWriter); string style = @"<style> .text { mso-number-format:\@; } </style> "; HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Write(stringWriter.ToString()); HttpContext.Current.Response.End(); } catch (Exception ex) { UDFLib.WriteExceptionLog(ex); } }
//protected static string ReadCss() //{ // string res; // string fileName = string.Format("table.css"); // string path = Path.Combine(HttpRuntime.AppDomainAppPath, @"App_Themes\Default\" + fileName); // using (StreamReader s = new StreamReader(path)) // { // res = s.ReadToEnd(); // } // return res; //} public static bool Save(WebControl grid, string path) { try { using (StreamWriter s = new StreamWriter(path, false, Encoding.Unicode)) { Page page = new Page(); page.EnableViewState = false; HtmlForm frm = new HtmlForm(); frm.Attributes["runat"] = "server"; page.Controls.Add(frm); frm.Controls.Add(grid); StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); frm.RenderControl(htmlWrite); string start = @"<style>.text { mso-number-format:\@; }</style>"; s.Write(start); s.Write(stringWrite.ToString()); } return true; } catch { throw; } }
public void ExportGridviewToExcel(GridView GridViewexp, string ExportHeader) { string filename = String.Format("Vetting_{0}_{1}_{2}.xls", DateTime.Today.Day.ToString(), DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString()); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); HttpContext.Current.Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); // Replace all gridview controls with literals GridViewRow grh = GridViewexp.HeaderRow; grh.ForeColor = System.Drawing.Color.Black; grh.HorizontalAlign = HorizontalAlign.Left; GridViewexp.GridLines = GridLines.Both; grh.Cells[4].Visible = false; foreach (TableCell cl in grh.Cells) { cl.HorizontalAlign = HorizontalAlign.Left; cl.Attributes.Add("class", "text"); PrepareControlForExport_GridView(cl); } foreach (GridViewRow gr in GridViewexp.Rows) { gr.Cells[4].Visible = false; foreach (TableCell cl in gr.Cells) { cl.HorizontalAlign = HorizontalAlign.Left; cl.Attributes.Add("class", "text"); PrepareControlForExport_GridView(cl); } } System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm(); Controls.Add(form); Label lbl = new Label(); lbl.Text = ExportHeader; lbl.Font.Size = 14; lbl.Font.Bold = true; form.Controls.Add(lbl); Table tbl = new Table(); tbl.Attributes.Add("Style", "border:1px solid #A9A9A9"); //tbl.BorderColor = System.Drawing.Color.Black; TableRow tr1 = new TableRow(); TableRow tr2 = new TableRow(); tr1.Attributes.Add("Style", "border:1px solid #A9A9A9"); tr2.Attributes.Add("Style", "border:1px solid #A9A9A9"); TableCell tdModule = new TableCell(); tdModule.Font.Bold = true; tdModule.Text = "Module"; TableCell tdModuleValue = new TableCell(); tdModuleValue.Text = txtModule.Text; TableCell tdVesTyp = new TableCell(); tdVesTyp.Font.Bold = true; tdVesTyp.Text = "Vessel Type"; TableCell tdVesTypValue = new TableCell(); tdVesTypValue.Text = txtVesselType.Text; TableCell tdQuestionnire = new TableCell(); tdQuestionnire.Font.Bold = true; tdQuestionnire.Text = "Questionnaire Name"; TableCell tdQuestionnirValue = new TableCell(); tdQuestionnirValue.Text = txtQuestionnaire.Text; TableCell tdVetTyp = new TableCell(); tdVetTyp.Font.Bold = true; tdVetTyp.Text = "Vetting Type"; TableCell tdVetTypValue = new TableCell(); tdVetTypValue.Text = txtVettingType.Text; TableCell tdNumber = new TableCell(); tdNumber.Font.Bold = true; tdNumber.Text = "Number"; TableCell tdNumberValue = new TableCell(); tdNumberValue.Text = txtNumber.Text; TableCell tdVersion = new TableCell(); tdVersion.Font.Bold = true; tdVersion.Text = "Version"; TableCell tdVersionValue = new TableCell(); tdVersionValue.Text = txtVersion.Text; tr1.Cells.Add(tdModule); tr1.Cells.Add(tdModuleValue); tr1.Cells.Add(tdVesTyp); tr1.Cells.Add(tdVesTypValue); tr1.Cells.Add(tdQuestionnire); tr1.Cells.Add(tdQuestionnirValue); tr2.Cells.Add(tdVetTyp); tr2.Cells.Add(tdVetTypValue); tr2.Cells.Add(tdNumber); tr2.Cells.Add(tdNumberValue); tr2.Cells.Add(tdVersion); tr2.Cells.Add(tdVersionValue); tbl.Rows.Add(tr1); tbl.Rows.Add(tr2); form.Controls.Add(tbl); Table tbl1 = new Table(); TableRow tr3 = new TableRow(); TableCell td1 = new TableCell(); tr3.Cells.Add(td1); tbl1.Rows.Add(tr3); form.Controls.Add(tbl1); form.Controls.Add(GridViewexp); form.RenderControl(htmlWriter); string style = @"<style> .text { mso-number-format:\@; } </style> "; HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Write(stringWriter.ToString()); HttpContext.Current.Response.End(); }
public static void pdfport(GridView gv, string fileName, int lans, Page p) { gv.Visible = true; gridstrip(gv); gv.DataBind(); string attachment = "attachment; filename=" + fileName + csclass.ranfn() + ".pdf"; HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.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.A3, 10f, 10f, 10f, 0f); if (lans == 1) { pdfDoc.SetPageSize(iTextSharp.text.PageSize.A3.Rotate()); } HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); HttpContext.Current.Response.Write(pdfDoc); HttpContext.Current.Response.End(); }
public static void GenerateExcelFiles(string filename, GridView gv) { string attachment = "attachment; filename=" + filename + ".xls"; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); // Create a form to contain the grid HtmlForm frm = new HtmlForm(); gv.Parent.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(gv); frm.RenderControl(htw); //GridView1.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); }