日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

一起谈.NET技术,在MVC2.0使用Lodop为WEB打印提出完美解决方案

發(fā)布時(shí)間:2023/12/19 asp.net 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一起谈.NET技术,在MVC2.0使用Lodop为WEB打印提出完美解决方案 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  通過(guò)好友CallHot介紹Lodopweb打印控件。由于是國(guó)人開(kāi)發(fā)的,故這兩天認(rèn)真了研究下,打算在未來(lái)的項(xiàng)目中使用。現(xiàn)將學(xué)習(xí)成果與園友分享。如果存在不足的地方,希望您指出。

  具體的實(shí)現(xiàn)步驟如下:

  一、準(zhǔn)備工作

?  1.MVC2.0 + jQuery1.4.1 開(kāi)發(fā)環(huán)境。

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

  3.StringTemplate,C#開(kāi)源模板引擎。官方地址: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)目開(kāi)發(fā)中將繁雜的報(bào)表打印工作內(nèi)容,部分分配給美工來(lái)處理。而開(kāi)發(fā)人員只需提供數(shù)據(jù)源接口。使用st可以減輕開(kāi)發(fā)人員的工作量。并將報(bào)表開(kāi)發(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);
}

  其中CustomerTest是自定義數(shù)據(jù)類,已經(jīng)給出詳細(xì)的注釋了。仔細(xì)閱讀不難理解。

  4.MVC2.0 view html head 內(nèi)加入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功能_打印表格");
//報(bào)表標(biāo)題
LODOP.ADD_PRINT_HTM(50, 300, 330, 300,
"<font color ='black' size ='6'>客戶列表</font><font color ='blue' size ='2'>(制表人:張三)</font>");
//報(bào)表內(nèi)容打印。
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>

  三、運(yùn)行截圖

  最后一頁(yè)打印預(yù)覽:

  打印機(jī)橫向打印:

  四、注意事項(xiàng)

?  本文給出的web打印方案需要讀者對(duì)MVC2.0 、jQuery 、StringTemplate 有一定的了解。另外本例只是在IE下實(shí)現(xiàn)了WEB打印,如果需要Firefox或其他瀏覽器下支持web打印請(qǐng)聯(lián)系Lodop作者。

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

總結(jié)

以上是生活随笔為你收集整理的一起谈.NET技术,在MVC2.0使用Lodop为WEB打印提出完美解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。