Web探索|Asp.net||Jquery|MVC

Web前沿技术、移动解决方案
  博客园  :: 首页  :: 新随笔  :: 管理

导出EXCEL方法

Posted on 2012-07-19 11:35  reckcn  阅读(141)  评论(0编辑  收藏  举报
/// <summary>
    /// 导出EXCEL方法
    /// </summary>
    /// <param name="page"></param>
    /// <param name="fileName"></param>
    /// <param name="html"></param>
    protected void ExportDsToXls(Page page, string fileName, string html)
    {

        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        Response.Clear();
        Response.Charset = "gb2312";
        Response.ContentType = "application/vnd.ms-excel";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName) + ".xls");
        Response.Write("<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body>");

        Response.Write(html);

        Response.Write(tw.ToString());
        Response.Write("</body></html>");
        Response.End();
        hw.Close();
        hw.Flush();
        tw.Close();
        tw.Flush();
    }