首页 > 代码库 > C#巧用Excel模版变成把Table打印出来
C#巧用Excel模版变成把Table打印出来
将一个做好的Excel模版,通过程序填上数据然后打印出来这个需求有两种方法一种是通过代码打开Excel模版然后填入数据然后再打印。
第二种方法就是我将要介绍的
1、将Excel设置好格式另存为HTML页将Excel转化为HTML的Table
2、将HTML用记事本打开把这个HTML页将<style></style>和<table></table>标签中的内容粘贴出来。
3、在程序中写入打印代码。
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.IO; using BLL; using Model; using System.Data; public partial class WebUI_Order_OutExcel : System.Web.UI.Page { BT_XDMgr XDMgr = new BT_XDMgr(); DataTable dtOrderMainInfo = new DataTable(); DataTable dtOrderCongInfo = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["OrderID"] != null && Request.QueryString["CKFromID"] != null) { int OrderID =Convert.ToInt32( Request.QueryString["OrderID"].ToString().Trim()); int CKFromID = Convert.ToInt32(Request.QueryString["CKFromID"].ToString().Trim()); string strWhere = " and T_DLS_Order_CKFrom.OrderID=" + OrderID; dtOrderMainInfo = XDMgr.CKList(strWhere); dtOrderCongInfo = XDMgr.QueryCKCongInfobyCKFromID(CKFromID); string strText = getExcelContent(dtOrderMainInfo,dtOrderCongInfo); ExportExcel(this, "出库单表", strText); } } /// <summary> /// 导出Excel /// </summary> /// <param name="page">操作页面</param> /// <param name="fileName">导出的文件名称</param> /// <param name="text">要导出的内容</param> public void ExportExcel(Page page, string fileName, string text) { page.EnableViewState = false; try { page.Response.ClearContent(); page.Response.Buffer = true; page.Response.Charset = "GB2312";//设置字符集,解决中文乱码问题 page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); page.Response.Write("<meta http-equiv=Content-Type content=\"text/html;charset=GB2312\">");//解决乱码问题 ////解决HTTP头中文乱码问题 //string strExcelText = fileName;//Excel显示的内容 //string strEncode = System.Web.HttpUtility.UrlEncode(strExcelText, System.Text.Encoding.UTF8);//进行编码的格式,用gb2312出错 //page.Response.AddHeader("content-disposition", "attachment;filename=\"" + strEncode + ".xls\"");//对保存标题进行编码 //page.Response.ContentType = "application/excel";//设置输出格式 //page.Response.Write(getStyle()); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); htw.WriteLine(text);//将数据输出 page.Response.Write(sw.ToString()); //page.Response.Write("</body></html>"); page.Response.Flush(); page.Response.End(); } catch { return; } finally { //恢复原来控件内容 page.EnableViewState = true; } } //订单信息 private string getExcelContent(DataTable dtMainInfo,DataTable dtCongInfo) { StringBuilder strExcel = new StringBuilder(); //Table的样式 strExcel.Append(@"<html><head><style>"); strExcel.Append(" table{mso-displayed-decimal-separator:'\\.\';mso-displayed-thousand-separator:'\\,\';}"); strExcel.Append(" .font56428{color:windowtext;font-size:9.0pt; font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;}"); strExcel.Append(" .xl156428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:black;font-size:11.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:general;vertical-align:middle;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl676428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:10.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:center;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl686428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:12.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:general;vertical-align:bottom;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl696428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:12.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:general;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl706428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:black;font-size:10.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:'Times New Roman', serif;mso-font-charset:0;mso-number-format:General;text-align:center;vertical-align:bottom;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl716428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:12.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:general;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl726428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:black;font-size:14.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:'Times New Roman', serif;mso-font-charset:0;mso-number-format:General;text-align:general;vertical-align:bottom;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl736428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:center;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl746428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:right;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl756428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:center;vertical-align:middle;border:.5pt solid windowtext;background:white;mso-pattern:black none;white-space:nowrap;}"); strExcel.Append(" .xl766428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:center;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl776428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:18.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:center;vertical-align:bottom;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl786428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:black;font-size:14.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:'Times New Roman', serif;mso-font-charset:0;mso-number-format:General;text-align:right;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl796428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:general;vertical-align:middle;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl806428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:general;vertical-align:middle;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl816428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:black;font-size:11.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:center;vertical-align:middle;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl826428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:left;vertical-align:bottom;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl836428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:left;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl846428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:14.0pt;font-weight:700;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:left;vertical-align:middle;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl856428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:black;font-size:11.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:left;vertical-align:middle;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append(" .xl866428{padding-top:1px;padding-right:1px;padding-left:1px;mso-ignore:padding;color:windowtext;font-size:12.0pt;font-weight:400;font-style:normal;text-decoration:none;font-family:宋体;mso-generic-font-family:auto;mso-font-charset:134;mso-number-format:General;text-align:left;vertical-align:middle;border:.5pt solid windowtext;mso-background-source:auto;mso-pattern:auto;white-space:nowrap;}"); strExcel.Append("</style>"); //Javascript的打印代码 strExcel.Append("<script type='text/javascript'>"); strExcel.Append("function doPrint() {"); strExcel.Append("var htmlcode=window.document.body.innerHTML;"); strExcel.Append("var sprnstr='<!--startprint-->';"); strExcel.Append("var eprnstr= '<!--endprint-->';"); strExcel.Append("var prnhtml=htmlcode.substr(htmlcode.indexOf(sprnstr)+17);"); strExcel.Append("prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));"); strExcel.Append("window.document.body.innerHTML=prnhtml;"); strExcel.Append("window.print();"); strExcel.Append("window.document.body.innerHTML=htmlcode;"); strExcel.Append("}"); strExcel.Append("</script>"); strExcel.Append("</head><body ><input id='Button1' type='button' value=http://www.mamicode.com/'打印' onclick='doPrint()' target='_self'/>");>4、打印出来的效果。
总结:
当我们遇到一个问题的时候应该从不同的角度去看待他,这样你就会有不同的解决方法选择一种最快,最容易,自己最擅长的方法去做这样就能提高工作效率。
C#巧用Excel模版变成把Table打印出来
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。