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

    HTML格式导出Excel

    public static string ExportTable()
            {
                StringBuilder sb = new StringBuilder();
     
                sb.Append("<html xmlns:v="urn:schemas-microsoft-com:vml"xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns=".w3.org/TR/REC-html40">");
                sb.Append("<head>");
                sb.AppendLine("<meta http-equiv="Content-Type" content="text/html; charset=utf-8">");
                sb.Append("</head>");
                sb.Append("<body>");
     
                sb.AppendLine("<table cellspacing="0" cellpadding="5" rules="all" border="1" width="800px">");
     
                sb.AppendLine("<tr style="font-weight: bold; white-space: nowrap;800px;">");
                sb.Append("<td style="60px;">序</td>");
                sb.Append("<td style="200px;">姓名</td>");
                sb.Append("<td style="100px;">年龄</td>");
                sb.Append("</tr>");
     
                Random rm = new Random();
     
                for (int i = 0; i < 100; i++)
                {
                    sb.Append("<tr>");
                    sb.AppendFormat("<td>{0}</td>", i + 1);
                    sb.AppendFormat("<td>张{0}</td>", i + 1);
                    sb.AppendFormat("<td>{0}</td>", rm.Next(0,100));
                    sb.Append("</tr>");
                }
     
                sb.AppendLine("</table>");
                sb.Append("</body>");
                sb.Append("</html>");
     
                return sb.ToString();
            }
     
            public static void ExportToExcel(Page page)
            {
                page.Response.Clear();
                page.Response.Buffer = true;
                //page.Response.Charset = "GB2312";
                page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("导出Excel", System.Text.Encoding.UTF8) + ".xls");
                page.Response.ContentEncoding = System.Text.Encoding.UTF8;
                page.Response.ContentType = "application/ms-excel";
                page.EnableViewState = false;
                page.Response.Write(ExportTable());
                page.Response.End();
            }
  • 相关阅读:
    ASP.NET MVC路由模块
    线程安全的单例模式
    MVC自带表单效验
    MSsql 中 in 语法排序的说明
    Web.Config配置错误页面处理
    WCF基本应用
    .NET微信自定义分享标题、缩略图、超链接及描述的设置方法
    .NET微信通过授权获取用户的基本信息
    C#中获取服务器IP,客户端IP以及网卡物理地址
    .NET获取客户端、服务器端的信息
  • 原文地址:https://www.cnblogs.com/nxxshxf/p/5591057.html
Copyright © 2011-2022 走看看