在MVC2.0使用Lodop為WEB打印提出完美解決方案

作者: ryanding  來源: 博客園  發布時間: 2011-01-03 22:10  閱讀: 4100 次  推薦: 0   原文鏈接   [收藏]  

  通過好友CallHot介紹Lodopweb打印控件。由于是國人開發的,故這兩天認真了研究下,打算在未來的項目中使用。現將學習成果與園友分享。如果存在不足的地方,希望您指出。

  具體的實現步驟如下:

  一、準備工作

   1.MVC2.0 + jQuery1.4.1 開發環境。

  2.Lodop web 打印控件,官方地址:http://mtsoftware.v053.gokao.net/download.html  (注:國人開發,免費軟件)。

  3.StringTemplate,C#開源模板引擎。官方地址:http://www.stringtemplate.org。

  本文主要給出WEB下打印步驟實現方案,具體的技術實現細節,請查看官方API。lodop,stringtemplate 官方已給出了詳盡的文檔說明。

  二、MVC2.0使用StringTemplate構造打印模板

  StringTemplate 文中簡稱st。網絡上有相關文檔介紹st效率還不錯。本文將st作為報表打印模板。在實際項目開發中將繁雜的報表打印工作內容,部分分配給美工來處理。而開發人員只需提供數據源接口。使用st可以減輕開發人員的工作量。并將報表開發任務分工更細致。給項目帶來的好處就不多論了。具體實現如下:

  1.在MVC2.0項目中引用st核心dll:

  2.建立st的模板文件,template.st(st模板專用文件):

  也可以認為st文件就是一個普通的html文件。該部分主要由美工負責處理,比如CSS。

  3.在MVC2.0 controller 內建立提供數據源的 JsonResult:

 
public JsonResult Print()
{

//構造打印數據
List<CustomerTest> list = new List<CustomerTest>();
for (int i = 0; i < 100; i++)
{
list.Add(
new CustomerTest { CustomerName = "candy" + i, CustomerAddress = "思明區" + i, CustomerPhone = "13148484855" + i });
list.Add(
new CustomerTest { CustomerName = "linda" + i, CustomerAddress = "湖里區" + i, CustomerPhone = "13847487545" + i });
list.Add(
new CustomerTest { CustomerName = "ellie" + i, CustomerAddress = "海昌區" + i, CustomerPhone = "1359984665" + i });
}


//StringTemplate 打印模板文件,實際項目中為提高程序效率,應將打印模板文件緩存。
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相關超文本內容。
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);
}

  其中CustomerTest是自定義數據類,已經給出詳細的注釋了。仔細閱讀不難理解。

  4.MVC2.0 view html head 內加入js 代碼:

 
<asp:Content ID="Content3" ContentPlaceHolderID="Head" runat="server">
<script language="javascript" src="CheckActivX.js"></script>
<object id="LODOP" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width="0"
height="0">
</object>
<script type="text/javascript">

function prn1_preview(data) {
LODOP.PRINT_INIT(
"打印控件功能演示_Lodop功能_打印表格");
//報表標題
LODOP.ADD_PRINT_HTM(50, 300, 330, 300,
"<font color ='black' size ='6'>客戶列表</font><font color ='blue' size ='2'>(制表人:張三)</font>");
//報表內容打印。
LODOP.ADD_PRINT_TABLE(100, 150, 760, 900, data);
LODOP.PREVIEW();
};

$(
function () {
$(
"#btnPrint").click(function () {
var url = '<%=Url.Action("Print","Home") %>';
$.ajax({
type:
"POST",
url: url,
cache:
false,
dataType:
'json',
success:
function (result) {
if (result.success) {
prn1_preview(result.data);
}
}
});
});
})

</script>
</asp:Content>

  三、運行截圖

  最后一頁打印預覽:

  打印機橫向打印:

  四、注意事項

   本文給出的web打印方案需要讀者對MVC2.0 、jQuery 、StringTemplate 有一定的了解。另外本例只是在IE下實現了WEB打印,如果需要Firefox或其他瀏覽器下支持web打印請聯系Lodop作者

  希望本篇文章可以給您帶來幫助,如有不足之處歡迎指出,謝謝!

0
0
 
標簽:MVC2.0 WEB打印
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()