zoukankan      html  css  js  c++  java
  • 【C#】导出Excel

    导出Excel的方法
    1.利用web控件导出
            public static void OutputToExcel(string fileName,System.Web.UI.Control dg)
            {
                string str = fileName + ".xls";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +

    HttpUtility.UrlPathEncode(str));
                HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                StringBuilder sb = new StringBuilder();
                StringWriter writer = new StringWriter(sb);
                HtmlTextWriter writer2 = new HtmlTextWriter(writer);
                dg.RenderControl(writer2);
                writer2.Close();
                writer.Close();
                HttpContext.Current.Response.Write("<meta http-equiv="content-type" content="application/ms-excel;

    charset=gb2312"/>");
                HttpContext.Current.Response.Write(sb.ToString());
                HttpContext.Current.Response.End();
            }
    2.利用table字符串导出(与1基本差不多)
            public static void OutputToExcel(string fileName, string dtStr)
            {
                string str = fileName + ".xls";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +

    HttpUtility.UrlPathEncode(str));
                HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                HttpContext.Current.Response.Write("<meta http-equiv="content-type" content="application/ms-excel;

    charset=gb2312"/>");
                HttpContext.Current.Response.Write(dtStr);
                HttpContext.Current.Response.End();
            }

  • 相关阅读:
    hdu 1754 I Hate It
    hdu 2546 饭卡
    hdu 4506 小明系列故事——师兄帮帮忙
    hdu 3665 Seaside
    hdu 3790 最短路径问题
    hdu 1869 六度分离
    最长递增字串的三种做法
    问题集???
    IOS开发学习 碎片S
    超级台阶 (NYOJ—76)
  • 原文地址:https://www.cnblogs.com/zspbolg/p/3610608.html
Copyright © 2011-2022 走看看