码迷,mamicode.com
首页 > 其他好文 > 详细

小功能集锦

时间:2014-06-03 09:46:45      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

1. 导出功能

bubuko.com,布布扣
    public static void OutExcel(DataTable dt)
        {
            GridView gv1 = new GridView();

            gv1.DataSource = dt;
            gv1.DataBind();
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.GetEncoding("GB2312")));
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv1.RenderControl(htw);
            HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"></head><body>");
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.Write("</body></html>");
            HttpContext.Current.Response.End();
        }
bubuko.com,布布扣

这样可以解决导出乱码的问题,以前的方式,乱码有时一条记录时出现,有时多条记录时出现,有时甚至导出了韩文。用这种方法,到目前为止还没有出现过乱码。

小功能集锦,布布扣,bubuko.com

小功能集锦

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/crazylight/p/3760498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!