简体   繁体   中英

Exporting Datatable and Gridview data simultaneously to pdf and excel

I have datatable which has single column and gridview 3 columns. On clicking this i need to display all 4. Is it possible to display? The datatable column can be below gridview columns also.

aspx:

<asp:Button ID="btnExportToExcel" Text="Export TO Excel"

runat="server" OnClick="btnExportToExcel_OnClick" />

code:

     protected void btnExportToExcel_OnClick(object sender, EventArgs e)
   {

    DataTable dtRiskFact = (DataTable)Session["dtRiskFact"];  //save datatable in session and get it 
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Summary_" + DateTime.Now.ToString("dd-MMM-yyyy") + ".xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter StringWriter = new System.IO.StringWriter();
    HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);

    GridView grdExprExcel = this.grdEditBranch;   //your grid
grdExprExcel.Columns[0].Visible=false         // hide columns like this
    grdExprExcel.AllowPaging = false;
    grdExprExcel.DataSource = dtRiskFact;
    grdExprExcel.DataBind();

    grdExprExcel.RenderControl(HtmlTextWriter);
    Response.Write(StringWriter.ToString());

    Response.End();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM