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

  • 相关阅读:
    python_元组
    python_列表
    python_字符串
    python_序列
    RFS一些基本概念
    学习RFS,所有文章的参考
    HDU 1754 线段树 单点跟新 HDU 1166 敌兵布阵 线段树 区间求和
    第四届河南省ACM SUBSTRING 字符串处理
    蓝桥杯 手链样式 排列组合
    蓝桥杯 牌型种数 DFS
  • 原文地址:https://www.cnblogs.com/zspbolg/p/3610608.html
Copyright © 2011-2022 走看看