服务器端直接将HTML下载成Excel

时间:2022-08-26 17:49:50

今天,有一个功能需要,需要将网页的数据表格下载成Excel。之前没有做过这样的需求。然后在网上摆渡了一下,网上的方案有很多种

  1. 通过使用Ajax获取Html
  2. 将标签变为服务器端控件,通过InnerHtml的方式获取。
  3. 还有其他方式参考

个人觉得第一中太繁琐,我只是想获取个HTML而已,干嘛还给我搞个Ajax 出来,那么我就索性使用了第二种方案。将标签加上runat=“server”,通过属性来获取里面包含的html。

代码如下:

前段代码:
   <div class="container-fluid " runat="server" id="DivAll">

后端代码:
  string dd= this.DivAll.InnerHtml;

这样貌似没有什么问题,结果一运行,大跌我眼镜!出现了煞白煞白的错误提示。

服务器端直接将HTML下载成Excel

于是乎,有使用了万事都能解决的摆渡,在上面寻找答案,真希望百度,哪天能帮我摆渡一下。花费我洪荒之力,终于找到了解决方案,不过说实在的这解决方案还要研究一下,不知道为什么还有一个套一个使用!

  System.Text.StringBuilder strb = new System.Text.StringBuilder();
                System.IO.StringWriter sw = new System.IO.StringWriter(strb);
                System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
                this.DivAll.RenderControl(htw);

这样在后台代码中就可以直接使用前台的Div中的html代码啦。只需要将strb.ToString();

那么下一步就是将Html放在后台中直接下载出来。那问题又来了,下载方法不会写呀!我艹又要上摆渡,真是够够的啦!


                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/bootstrap.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();

听说将这段代码中的.xls换成doc可以直接下载成Word格式!

号外号外,在这段代码中还可以神奇的加入Css文件。真是够流弊的!

好啦,不写啦,直接贴上整段代码

 protected void DownExcel_Click(object sender, EventArgs e)
        {
            try
            {
                System.Text.StringBuilder strb = new System.Text.StringBuilder();
                System.IO.StringWriter sw = new System.IO.StringWriter(strb);
                System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
                //前段div的id
                this.DivAll.RenderControl(htw);

                string html = strb.ToString();
                htw.Close();
                sw.Close();


                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/bootstrap.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();

            }
            catch (Exception)
            {

                throw;
            }
        }

TIP:若遇到文件下载格式问题,请查看

http://blog.csdn.net/younghaiqing/article/details/70846850