標籤:style blog http ar io color os sp for
前些天寫項目的時候,客戶要求用HTML表格把資訊展示出來,後面還要用展示的內容要匯出Excel。本來想想在後台操作的話估計是要做死了,但是經過細想,Excel能夠發布成HTML,一定可以由HTML轉成Excel。經過幾次搜尋,算是把問題完善解決了代碼如下:
1 Response.ContentType = "application/force-download"; 2 Response.AddHeader("content-disposition", 3 "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); 4 Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">"); 5 Response.Write("<head>"); 6 Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"); 7 8 9 #region 樣式的讀取10 11 string fileCss = Server.MapPath("~/Content/CalCSS/tableCss.css");12 string cssText = string.Empty;13 StreamReader sr = new StreamReader(fileCss);14 var line = string.Empty;15 while ((line = sr.ReadLine()) != null)16 {17 cssText += line;18 }19 sr.Close();20 Response.Write("<style>" + cssText + "</style>");21 22 #endregion23 24 Response.Write("<!--[if gte mso 9]><xml>");25 Response.Write("<x:ExcelWorkbook>");26 Response.Write("<x:ExcelWorksheets>");27 Response.Write("<x:ExcelWorksheet>");28 Response.Write("<x:Name>Report Data</x:Name>");29 Response.Write("<x:WorksheetOptions>");30 Response.Write("<x:Print>");31 Response.Write("<x:ValidPrinterInfo/>");32 Response.Write("</x:Print>");33 Response.Write("</x:WorksheetOptions>");34 Response.Write("</x:ExcelWorksheet>");35 Response.Write("</x:ExcelWorksheets>");36 Response.Write("</x:ExcelWorkbook>");37 Response.Write("</xml>");38 Response.Write("<![endif]--> ");39 40 Response.Write(strHtml1);//這裡是前台頁面的HTML41 42 Response.Flush();43 Response.End();
經過測試能夠完全的展示頁面的樣式和規格:
前台HTML:
產生後的HTML:
第十二篇 HTML 直接轉成Excel