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

  • 相关阅读:
    全程图解】ADSL+笔记本电脑 组建WIFI网络让5800实现WIFI上网(更新完毕)
    JSP用户管理系统【上学应付作业用】
    c++按位操作符
    F#: .NET中的函数编程语言
    Visual Studio OpenGL 配置方法
    Linux下挂载U盘方法
    开发者该以什么为骄傲
    POSIX约定与GNU长选项
    修复移动硬盘"文件或目录损坏且无法读取"
    某国外论坛关于什么是Computer Science的争论,你怎么看?
  • 原文地址:https://www.cnblogs.com/zspbolg/p/3610608.html
Copyright © 2011-2022 走看看