• 【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学习32天(socket、tcp协议)
    Python学习第31天(异常、异常捕捉)
    Python之旅的第30天(过程记录,选课系统的基本实现)
    Python之旅的第29天(property补充、元类和自定义元类)
    Python之旅的第28天(描述符、类的装饰器)
    Python之旅的第27天(复习、习题实现、__enter__、__exit__)
    Python之旅第26天(__slots__等内置方法、软件开发规范)
    假期第二周
    假期第一周
    第十六周学习进度博客
  • 原文地址:https://www.cnblogs.com/zspbolg/p/3610608.html
Copyright © 2020-2023  润新知