execl导出(HTML,repearter控件)

protected void btn_execl_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMdd") + ".xls");
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/vnd.ms-excel";
            this.EnableViewState = false;
          //Export("application / force - download", "测试.xls");
        }

注意:只是转成execl文件的格式,页面不是表格。

  function getBodyHtml() {
              var html = $("html").html();
                  //$("html").html();
                  //$("#html_container").html();
              $("#hfHtml2Excel").val(html);        
          }

<asp:Button ID="btnExportExcel" runat="server" Text="To Excel" OnClick="btnExportExcel_Click" OnClientClick="return getBodyHtml();"/>

 protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string html = hfHtml2Excel.Value;

            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\">");


            #region 样式的读取

            //string fileCss = System.AppDomain.CurrentDomain.BaseDirectory +"template/stock_summary.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>");

            #endregion

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

        }

posted @ 2017-11-15 17:35  suan1717  阅读(201)  评论(0编辑  收藏  举报