• C#将HTML导出Excel


    首先这个 不能用ajax 操作,不过 我现在讲的 这个方法和ajax 的效果一样。

    你在你需要导出的页面写个方法 

    function DaoChu ()
    {
      location.href = "DaoChu.aspx";
    }

    然后在 DaoChu.aspx 页面的后台Page_Load  中 直接写以下代码 

     string html = "<table><tr><td>1</td><td>11</td></tr><tr><td>2</td><td>22</td></tr></table>";
                    Response.ContentType = "application/force-download";
                    Response.AddHeader("content-disposition",
                        "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
                    Response.Write("<html xmlns:x="urn:schemas-microsoft-com:office:excel">");
                    Response.Write("<head>");
                    Response.Write("<META http-equiv="Content-Type" content="text/html; charset=utf-8">");
                    string fileCss = Server.MapPath("~/css/daoChuCSS.css");
                    string cssText = string.Empty;
                    StreamReader sr = new StreamReader(fileCss);
                    var line = string.Empty;
                    while ((line = sr.ReadLine()) != null)
                    {
                        cssText += line;
                    }
                    sr.Close();
                    Response.Write("<style>" + cssText + "</style>");
                    Response.Write("<!--[if gte mso 9]><xml>");
                    Response.Write("<x:ExcelWorkbook>");
                    Response.Write("<x:ExcelWorksheets>");
                    Response.Write("<x:ExcelWorksheet>");
                    Response.Write("<x:Name>Report Data</x:Name>");
                    Response.Write("<x:WorksheetOptions>");
                    Response.Write("<x:Print>");
                    Response.Write("<x:ValidPrinterInfo/>");
                    Response.Write("</x:Print>");
                    Response.Write("</x:WorksheetOptions>");
                    Response.Write("</x:ExcelWorksheet>");
                    Response.Write("</x:ExcelWorksheets>");
                    Response.Write("</x:ExcelWorkbook>");
                    Response.Write("</xml>");
                    Response.Write("<![endif]--> ");
                    Response.Write(html);//HTML
                    Response.Flush();
                    Response.End();

    这样 就好了 ,html 代码 最好是table里面写,因为EXCEL  其实就是 table 。

    希望帮助到的同学 留下你宝贵的评论,谢谢

  • 相关阅读:
    wrk压测工具使用
    Mac 抓包工具wireshark使用
    hadoop无法停止
    非root用户如何使用docker命令
    too many open files
    kafka性能测试1.0.0
    命令查看linux主机配置
    ELK(Logstash+Elasticsearch+Kibana)的原理和详细搭建
    分布式session实现
    NUC972裸机调试步骤
  • 原文地址:https://www.cnblogs.com/lvphon/p/4755449.html
Copyright © 2020-2023  润新知