同时将Datatable和Gridview数据导出为pdf和excel

时间:2014-04-28 07:31:43

标签: asp.net sql-server c#-3.0

我有数据表,其中包含单列和gridview 3列。点击这个我需要显示全部4.是否可以显示?数据表列也可以在gridview列下面。

1 个答案:

答案 0 :(得分:0)

ASPX:

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

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

代码:

     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();
}