• asp.net将内容导出到Excel,Table表格数据(html)导出EXCEL


    代码:

       

     1      /// <summary>
     2         /// HTML Table表格数据(html)导出EXCEL
     3         /// </summary>
     4         /// <param name="tableHeader">表头</param>
     5         /// <param name="tableContent">内容</param>
     6         /// <param name="sheetName">文件名称</param>
     7         public void ExportToExcel(string tableHeader,string tableContent,string sheetName)
     8         {
     9             string fileName = sheetName + DateTime.Now.ToString("yyyyMMddHHmmss");
    10             string tabData = htmlTable;
    11             if (tabData != null)
    12             {
    13                 StringWriter sw = new System.IO.StringWriter();
    14                 sw.WriteLine("<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>");
    15                 sw.WriteLine("<table>");
    16                 sw.WriteLine("<tr style="background-color: #e4ecf7; text-align: center; font-weight: bold">");
    17                 sw.WriteLine(tableHeader);
    18                 sw.WriteLine("</tr>");
    19                 sw.WriteLine(tableContent);
    20                 sw.WriteLine("</table>");
    21                 sw.WriteLine("</body>");
    22                 sw.WriteLine("</html>");
    23                 sw.Close();
    24                 Response.Clear();
    25                 Response.Buffer = true;
    26                 Response.Charset = "UTF-8";
    27                 //Response.Charset = "GB2312";
    28                 //this.EnableViewState = false;
    29                 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
    30                 Response.ContentType = "application/ms-excel";
    31                 Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    32                 Response.Write(sw);
    33                 Response.End();
    34             }
    35         }
  • 相关阅读:
    域名、主机名、网站名以及 URL 基础概念
    c# oracle 数据库连接以及参数化查询
    c# 委托(Func、Action)
    xcode pod install 安装失败,提示缺少文件
    一个服务器的IIS只能绑定一个HTTPS也就是443端口
    APP UI设计及切图规范
    Day7 字符串和常用数据结构
    Day6 函数和模块的使用
    Day5 练习
    python 疑难杂症
  • 原文地址:https://www.cnblogs.com/linJie1930906722/p/5215925.html
Copyright © 2020-2023  润新知