打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在MVC2.0使用Lodop为WEB打印提出完美解决方案

通过好友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:

  1. public JsonResult Print() 
  2.     //构造打印数据 
  3.     List<CustomerTest> list = new List<CustomerTest>(); 
  4.     for (int i = 0; i < 100; i++) 
  5.     { 
  6.         list.Add(new CustomerTest { CustomerName = "candy" + i, CustomerAddress = "思明区" + i, CustomerPhone = "13148484855" + i }); 
  7.         list.Add(new CustomerTest { CustomerName = "linda" + i, CustomerAddress = "湖里区" + i, CustomerPhone = "13847487545" + i }); 
  8.         list.Add(new CustomerTest { CustomerName = "ellie" + i, CustomerAddress = "海昌区" + i, CustomerPhone = "1359984665" + i }); 
  9.     } 
  10.  
  11.     //StringTemplate 打印模板文件,实际项目中为提高程序效率,应将打印模板文件缓存。 
  12.     string serverPath = System.Web.HttpContext.Current.Server.MapPath("~"); 
  13.     string path = Path.Combine(serverPath, @"PrintTemplate\"); 
  14.  
  15.     StringTemplateGroup group = new StringTemplateGroup("myGroup", path, typeof(TemplateLexer)); 
  16.     StringTemplate st = group.GetInstanceOf("template"); 
  17.     st.SetAttribute("customer", list); 
  18.  
  19.     //为打印提供html相关超文本内容。 
  20.     StringBuilder sb = new StringBuilder(); 
  21.     sb.Append(@"<html xmlns='http://www.w3.org/1999/xhtml' lang='zh-CN'>"); 
  22.     sb.Append("<head>"); 
  23.     sb.Append(@"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"); 
  24.     sb.Append(@"<meta http-equiv='Content-Language' content='zh-CN' />"); 
  25.     string cssContent = System.IO.File.ReadAllText(Path.Combine(serverPath, @"Content\CSS\CSSForPrint.css")); 
  26.     sb.Append(@"<style type='text/css'>"); 
  27.     sb.Append(cssContent); 
  28.     sb.Append(@"</style>"); 
  29.     sb.Append("</head>"); 
  30.     sb.Append("<body>"); 
  31.     sb.Append(st.ToString()); 
  32.     sb.Append(" "); 
  33.     sb.Append("</body>"); 
  34.     sb.Append("</html>"); 
  35.  
  36.     return Json(new { success = true, data = sb.ToString() }, JsonRequestBehavior.AllowGet); 

其中CustomerTest是自定义数据类,已经给出详细的注释了。仔细阅读不难理解。

      4.MVC2.0 view html head 内加入js 代码:

  1. <asp:Content ID="Content3" ContentPlaceHolderID="Head" runat="server"> 
  2.     <script language="javascript" src="CheckActivX.js"></script> 
  3.     <object id="LODOP" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width="0" 
  4.         height="0"> 
  5.     </object> 
  6.     <script type="text/javascript"> 
  7.    
  8.         function prn1_preview(data) { 
  9.             LODOP.PRINT_INIT("打印控件功能演示_Lodop功能_打印表格"); 
  10.             //报表标题 
  11.             LODOP.ADD_PRINT_HTM(50, 300, 330, 300, 
  12.                       "<font color ='black' size ='6'>客户列表</font><font color ='blue' size ='2'>(制表人:张三)</font>"); 
  13.             //报表内容打印。 
  14.             LODOP.ADD_PRINT_TABLE(100, 150, 760, 900, data); 
  15.             LODOP.PREVIEW(); 
  16.         }; 
  17.    
  18.         $(function () { 
  19.             $("#btnPrint").click(function () { 
  20.                 var url = '<%=Url.Action("Print","Home") %>'
  21.                 $.ajax({ 
  22.                     type: "POST", 
  23.                     url: url, 
  24.                     cache: false, 
  25.                     dataType: 'json', 
  26.                     success: function (result) { 
  27.                         if (result.success) { 
  28.                             prn1_preview(result.data); 
  29.                         } 
  30.                     } 
  31.                 }); 
  32.             }); 
  33.         }) 
  34.     </script> 
  35. </asp:Content> 

三、运行截图

最后一页打印预览:

打印机横向打印:

 四、注意事项
     本文给出的web打印方案需要读者对MVC2.0 、jQuery 、StringTemplate 有一定的了解。另外本例只是在IE下实现了WEB打印

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
从零开始学习 ASP.NET MVC 1.0 (五) ViewEngine 深入解析与应用实例
基于StringTemplate的视图 - Fight ! - 博客园
ASP.NET MVC3 系列教程
ASP.NET MVC的JavaScriptResult
How ASP.NET MVC Works?
ASP.NET MVC 3 (一) 新的視圖引擎Razor
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服