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

  • 相关阅读:
    C# 字典类 Dictionary 基本用法 Mark
    SQL语句监测耗时
    jQuery Select Option 操作 删除新增
    C# DataTable 过滤重复数据
    IE8 overflow:hidden 无效问题解决方案
    动态拼接LINQ 查询条件
    解决.net中"未能创建 Mutex”异常
    创建Cookies 包含子健和无子健的创建及用法 做个笔记留着参考
    常用的一些加密算法,留着以备不时之需
    Centos7 nginx安装
  • 原文地址:https://www.cnblogs.com/zspbolg/p/3610608.html
Copyright © 2020-2023  润新知