Asp.net 导出Excel 我直接绑定查询方法 请问如何添加页眉页脚
protected void Button2_Click(object sender, EventArgs e)
{
    string zhilingName = this.TxtCode.Text;
    MakeFile();
    //System.IO.StreamWriter sw = new StreamWriter(@"d:\"+DateTime.Now.Month.ToString()+@"\"+zhilingName+".xls");
    //System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);

    //打印方法
    DataTable dt = ZhiDAL.getFreeDetaliPrint(zhilingName, "5");

    System.Web.HttpContext curContext = System.Web.HttpContext.Current;
    if (dt != null)
    {
        curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//改成utf-8
        curContext.Response.ContentType = "application/excel";
        curContext.Response.ClearContent();
        curContext.Response.AddHeader("content-disposition", "attachment; filename=" + zhilingName + "-" + DateTime.Now.ToShortDateString() + ".xls");

        //导出excel文件
        System.IO.StringWriter sws = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sws);
        GridView dg = new GridView();
        dg.DataSource = dt.DefaultView;
        dg.DataBind();
        dg.RenderControl(hw);
        dg.Dispose();
        curContext.Response.Write(sws.ToString());

        try
        {
            //在指定文件夹下 保存
            // DateTime.Now.Month.ToString()
            System.IO.StreamWriter sw = new StreamWriter(@"d:\" +DateTime.Now.ToString("yyyy-MM") + @"\" + zhilingName + ".xls");
            sw.Write(sws.ToString());
            sw.Close();
        }
        catch (Exception ex)
        {
        }
        //hw = new System.Web.UI.HtmlTextWriter(sws);

        curContext.Response.End(); 

    }


}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐