|
通過(guò)好友CallHot介紹Lodopweb打印控件。由于是國(guó)人開發(fā)的,故這兩天認(rèn)真了研究下,打算在未來(lái)的項(xiàng)目中使用。現(xiàn)將學(xué)習(xí)成果與園友分享。如果存在不足的地方,希望您指出。
具體的實(shí)現(xiàn)步驟如下:
一、準(zhǔn)備工作
1.MVC2.0 + jQuery1.4.1 開發(fā)環(huán)境。
2.Lodop web 打印控件,官方地址:http://mtsoftware.v053.gokao.NET/download.html (注:國(guó)人開發(fā),免費(fèi)軟件)。
3.StringTemplate,C#開源模板引擎。官方地址:http://www.stringtemplate.org。
本文主要給出WEB下打印步驟實(shí)現(xiàn)方案,具體的技術(shù)實(shí)現(xiàn)細(xì)節(jié),請(qǐng)查看官方API。lodop,stringtemplate 官方已給出了詳盡的文檔說(shuō)明。
二、MVC2.0使用StringTemplate構(gòu)造打印模板
StringTemplate 文中簡(jiǎn)稱st。網(wǎng)絡(luò)上有相關(guān)文檔介紹st效率還不錯(cuò)。本文將st作為報(bào)表打印模板。在實(shí)際項(xiàng)目開發(fā)中將繁雜的報(bào)表打印工作內(nèi)容,部分分配給美工來(lái)處理。而開發(fā)人員只需提供數(shù)據(jù)源接口。使用st可以減輕開發(fā)人員的工作量。并將報(bào)表開發(fā)任務(wù)分工更細(xì)致。給項(xiàng)目帶來(lái)的好處就不多論了。具體實(shí)現(xiàn)如下:
1.在MVC2.0項(xiàng)目中引用st核心dll:
2.建立st的模板文件,template.st(st模板專用文件):
也可以認(rèn)為st文件就是一個(gè)普通的html文件。該部分主要由美工負(fù)責(zé)處理,比如CSS。
3.在MVC2.0 controller 內(nèi)建立提供數(shù)據(jù)源的 JsonResult:
public JsonResult Print()
{
//構(gòu)造打印數(shù)據(jù)
List<CustomerTest> list = new List<CustomerTest>();
for (int i = 0; i < 100; i++)
{
list.Add(new CustomerTest { CustomerName = "candy" + i, CustomerAddress = "思明區(qū)" + i, CustomerPhone = "13148484855" + i });
list.Add(new CustomerTest { CustomerName = "linda" + i, CustomerAddress = "湖里區(qū)" + i, CustomerPhone = "13847487545" + i });
list.Add(new CustomerTest { CustomerName = "ellie" + i, CustomerAddress = "海昌區(qū)" + i, CustomerPhone = "1359984665" + i });
}
//StringTemplate 打印模板文件,實(shí)際項(xiàng)目中為提高程序效率,應(yīng)將打印模板文件緩存。
string serverPath = System.Web.HttpContext.Current.Server.MapPath("~");
string path = Path.Combine(serverPath, @"PrintTemplate/");
StringTemplateGroup group = new StringTemplateGroup("myGroup", path, typeof(TemplateLexer));
StringTemplate st = group.GetInstanceOf("template");
st.SetAttribute("customer", list);
//為打印提供html相關(guān)超文本內(nèi)容。
StringBuilder sb = new StringBuilder();
sb.Append(@"<html xmlns='http://www.w3.org/1999/xhtml' lang='zh-CN'>");
sb.Append("<head>");
sb.Append(@"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />");
sb.Append(@"<meta http-equiv='Content-Language' content='zh-CN' />");
string cssContent = System.IO.File.ReadAllText(Path.Combine(serverPath, @"Content/CSS/CSSForPrint.css"));
sb.Append(@"<style type='text/css'>");
sb.Append(cssContent);
sb.Append(@"</style>");
sb.Append("</head>");
sb.Append("<body>");
sb.Append(st.ToString());
sb.Append(" ");
sb.Append("</body>");
sb.Append("</html>");
return Json(new { success = true, data = sb.ToString() }, JsonRequestBehavior.AllowGet);
}
NET技術(shù):在MVC2.0使用Lodop為WEB打印提出完美解決方案,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。