zoukankan      html  css  js  c++  java
  • 特定的ExcelCSS样式Excel导出

    后台代码

            /// <summary>
            /// 导出Excel
            /// </summary>
            /// <param name="excelName"></param>
            /// <param name="html"></param>
            /// <param name="cssName"></param>
            public static void OutExcel(string excelName, string html, string cssName)
            {
                using (StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(string.Format("../style/form/{0}", cssName)), Encoding.UTF8))
                {
                    cssName = sr.ReadToEnd();
                    sr.Close();
                }

                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Charset = "UTF-8";
                HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.Default;
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + excelName + ".xls");

                HttpContext.Current.Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
                HttpContext.Current.Response.Write("<head>");
                HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");
                HttpContext.Current.Response.Write("<meta name=ProgId content=Excel.Sheet>");
                HttpContext.Current.Response.Write("<meta name=Generator content=\"Microsoft Excel 11\">");

                HttpContext.Current.Response.Write("<style>");
                HttpContext.Current.Response.Write(cssName);
                HttpContext.Current.Response.Write("</style>");

                HttpContext.Current.Response.Write("<!--[if gte mso 9]>");
                HttpContext.Current.Response.Write("<xml>");
                HttpContext.Current.Response.Write(" <x:ExcelWorkbook>");
                HttpContext.Current.Response.Write("  <x:ExcelWorksheets>");
                HttpContext.Current.Response.Write("   <x:ExcelWorksheet>");
                HttpContext.Current.Response.Write(string.Format("    <x:Name>{0}</x:Name>", excelName));
                HttpContext.Current.Response.Write("    <x:WorksheetOptions>");
                HttpContext.Current.Response.Write("      <x:Print>");
                HttpContext.Current.Response.Write("       <x:ValidPrinterInfo />");
                HttpContext.Current.Response.Write("       <x:PaperSizeIndex>9</x:PaperSizeIndex>");//打印A4纸
                HttpContext.Current.Response.Write("      </x:Print>");
                HttpContext.Current.Response.Write("    </x:WorksheetOptions>");
                HttpContext.Current.Response.Write("   </x:ExcelWorksheet>");
                HttpContext.Current.Response.Write("  </x:ExcelWorksheets>");

                HttpContext.Current.Response.Write("</x:ExcelWorkbook>");
                HttpContext.Current.Response.Write("</xml>");
                HttpContext.Current.Response.Write("<![endif]-->");
                HttpContext.Current.Response.Write("</head>");

                HttpContext.Current.Response.Write("<body>");
                HttpContext.Current.Response.Write(html);
                HttpContext.Current.Response.Write("</body></html>");

                HttpContext.Current.Response.End();
            }


    //页面存放控件
    <p id="tbForm">
    <!=页面要到导出的内容->
    </p>
    <asp:Button  Text="导出Excel" CssClass="butBase" ID="btnExcel" runat="server" UseSubmitBehavior="false" onclick="btnExcel_Click"   OnClientClick="$('#hfHtml').val($('#tbForm').html())"/>
    <asp:HiddenField ID="hfHtml" runat="server" />

  • 相关阅读:
    JSON.parse()和JSON.stringify()(转载)
    Bootstrap 栅格系统(转载)
    基于Java的WebSocket推送【转载】
    VirtualBox中的虚拟机要如何设置,才能够上网
    String类和StringBuffer类的区别
    split命令参数及用法详解
    linux du小练习
    内部类(innerclasses)、匿名对象
    构造器、方法的重载、方法的重写
    继承、封装、多态、抽象、接口
  • 原文地址:https://www.cnblogs.com/Cynosure/p/2117879.html
Copyright © 2011-2022 走看看