C#+一般處理程式+Lodop列印跨境電商電子面單(比利時、荷蘭佛山小包)
阿新 • • 發佈:2019-01-26
效果圖:
Aspx頁面程式碼:
<table class="table table-bordered table-striped"> <tbody> <tr class="even gradeC"> <td >波次單號:</td> <td> <input type="text" runat="server" id="txtWaveNo" value="SO1512150540" /></td> <td >SO訂單號:</td> <td> <input type="text" runat="server" id="txtSoNo" /> </td> </tr> <tr class="odd gradeA"> <td colspan="4"><input type="button" class="btn btn-info" id="btnPrint" value="比利時小包列印" runat="server" /> <input type="button" class="btn btn-info" id="btnPrint" value="荷蘭小包列印" runat="server" /> <input type="button" class="btn btn-info" id="btnFoShanPrint" value="佛山小包列印" runat="server" /> <span style="color:red">友情提示:可選擇掃描波次或者SO單號進行列印。</span> </td> </tr> </tbody> </table><div id="main-content"> <div class="tab-content"> <!--發運訂單資訊--> <div id="live" class="tab-pane active"> <div class='row-fluid'> <div class='span12 widget'> <div class='widget-content summary-list'> <div class='row-fluid' > <div class='span12 section '> <table class='table table-bordered table-striped'> <tbody id="divErrorMessage" runat="server"> </tbody> </table> </div> </div> </div> </div> </div> </div> </div> </div>
JS程式碼:
荷蘭和佛山小包列印: <input type="hidden" id="hfExpressHtml"/> <!-- Core Scripts --> <script src="http://www.orderonline.cn/js/jquery-1.7.2.js"></script> <script src="assets/js/Lodop.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#btnPrint").click(function () { if ($("#txtWaveNo").val() == "" && $("#txtSoNo").val() =="") { alert('波次單號或SO訂單號不能全為空,請任意輸入一個進行列印。'); } else { var searchwhere = $("#txtWaveNo").val() + "_" + $("#txtSoNo").val(); $.ajax({ type: "get", url: "ExpressPrint.ashx", data: "getExpressInfo=" + encodeURIComponent(searchwhere), datatype: "json", async: false, success: function (returnData, textstatus, xmlhttprequest) { LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM')); LODOP.SET_SHOW_MODE("HIDE_PAPER_BOARD", 1); $("#hfExpressHtml").html(returnData); var html = $("#hfExpressHtml .main_all >div"); // var strStyleCSS = "<!DOCTYPE html> <head>"; var strStyleCSS = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns=\"http://www.w3.org/1999/xhtml\"><head>"; strStyleCSS += "<link href='http://www.orderonline.cn/css/A4.css' type='text/css' rel='stylesheet'></head><body>"; //HTML格式用 // LODOP.SET_PRINT_PAGESIZE(1, "760px", "650px", "A5"); var num = 0; var strFormHtml = ""; for (var i = 0; i < html.length; i++) { if (num % 2 == 0) { LODOP.NewPage(); strFormHtml = ""; strFormHtml += strStyleCSS + html[i].outerHTML; } else { strFormHtml += strStyleCSS + html[i].outerHTML; } LODOP.ADD_PRINT_HTM(15, 0, 800, 650, strFormHtml + "</body></html>"); // LODOP.ADD_PRINT_HTM(30, 0, 800, 650, strFormHtml + "</body></html>"); num++; } LODOP.PREVIEW(); strFormHtml = ""; }, error: function (errorinfo) { alert("快遞單列印失敗,請重新重新整理後列印。"); } }); } }) $("#btnFoShanPrint").click(function () { if ($("#txtWaveNo").val() == "" && $("#txtSoNo").val() == "") { alert('波次單號或SO訂單號不能全為空,請任意輸入一個進行列印。'); } else { var searchwhere = $("#txtWaveNo").val() + "_" + $("#txtSoNo").val(); $.ajax({ type: "get", url: "ExpressPrint.ashx", data: "getExpressInfo=" + encodeURIComponent(searchwhere), datatype: "json", async: false, success: function (returnData, textstatus, xmlhttprequest) { LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM')); LODOP.SET_SHOW_MODE("HIDE_PAPER_BOARD", 1); $("#hfExpressHtml").html(returnData); var html = $("#hfExpressHtml .main_all >div"); // var strStyleCSS = "<!DOCTYPE html> <head>"; var strStyleCSS = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns=\"http://www.w3.org/1999/xhtml\"><head>"; strStyleCSS += "<link href='http://www.orderonline.cn/css/A4.css' type='text/css' rel='stylesheet'></head><body>"; //HTML格式用 LODOP.SET_PRINT_PAGESIZE(2, "760px", "650px", "A5"); var num = 0; var strFormHtml = ""; for (var i = 0; i < html.length; i++) { if (num % 2 == 0) { LODOP.NewPage(); strFormHtml = ""; strFormHtml += strStyleCSS + html[i].outerHTML; } else { strFormHtml += strStyleCSS + html[i].outerHTML; } LODOP.ADD_PRINT_HTM(15, 0, 800, 650, strFormHtml + "</body></html>"); // LODOP.ADD_PRINT_HTM(30, 0, 800, 650, strFormHtml + "</body></html>"); num++; } LODOP.PREVIEW(); strFormHtml = ""; }, error: function (errorinfo) { alert("快遞單列印失敗,請重新重新整理後列印。"); } }); } }) //清除轉向頁面 $("#txtWaveNo").val(''); $("#txtSoNo").val(''); $("#hfExpressHtml").html(''); }); </script> 比利時小包列印: <script type="text/javascript"> $(document).ready(function () { $("#btnPrint").click(function () { if ($("#txtWaveNo").val() == "" && $("#txtSoNo").val() == "") { alert('波次單號或SO訂單號不能全為空,請任意輸入一個進行列印。'); } else { var searchwhere = $("#txtWaveNo").val() + "_" + $("#txtSoNo").val(); $.ajax({ type: "get", url: "ExpressPrint.ashx", data: "getBlsExpressInfo=" + encodeURIComponent(searchwhere), datatype: "json", async: false, success: function (returnData, textstatus, xmlhttprequest) { LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM')); LODOP.SET_SHOW_MODE("HIDE_PAPER_BOARD", 1); $("#hfExpressHtml").html(returnData); var html = $("#hfExpressHtml .main_all >div"); // var strStyleCSS = "<!DOCTYPE html> <head>"; var strStyleCSS = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns=\"http://www.w3.org/1999/xhtml\"><head>"; strStyleCSS += "<link href='http://www.orderonline.cn/css/A4.css' type='text/css' rel='stylesheet'></head><body>"; //HTML格式用 // LODOP.SET_PRINT_PAGESIZE(1, "760px", "650px", "A5"); var num = 0; var strFormHtml = ""; for (var i = 0; i < html.length; i++) { if (num % 2 == 0) { LODOP.NewPage(); strFormHtml = ""; strFormHtml += strStyleCSS + html[i].outerHTML; } else { strFormHtml += strStyleCSS + html[i].outerHTML; } LODOP.ADD_PRINT_HTM(15, 0, 800, 650, strFormHtml + "</body></html>"); // LODOP.ADD_PRINT_HTM(30, 0, 800, 650, strFormHtml + "</body></html>"); num++; } LODOP.PREVIEW(); strFormHtml = ""; }, error: function (errorinfo) { alert("快遞單列印失敗,請重新重新整理後列印。"); } }); } //清除轉向頁面 $("#txtWaveNo").val(''); $("#txtSoNo").val(''); $("#hfExpressHtml").html(''); }) }); </script>
ExpressPrint.ashx 程式碼:
public class ExpressPrint : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (!string.IsNullOrEmpty(context.Request["getExpressInfo"])) { string[] strParm = context.Request["getExpressInfo"].Split('_'); StringBuilder sbExpressHtml = new StringBuilder(); var returnHtml = string.Empty; using (ExpressPrintDataContext db = new ExpressPrintDataContext()) { var getExpressList = from p in db.DOC_Wave_Details join pp in db.DOC_Order_Header on p.OrderNo equals pp.OrderNo where p.WaveNo.Contains(strParm[0]) && p.OrderNo.Contains(strParm[1]) select new { p.WaveNo, p.OrderNo, pp.SOReference3 }; var expressNo = string.Empty; returnHtml = @"<link href=""http://www.orderonline.cn/A4.css"" rel=""stylesheet"" type=""text/css"" />"; returnHtml += "<div class=\"wapper\" id=\"A4_print\"><div class=\"main_all\">"; foreach (var item in getExpressList) { // expressNo += item.SOReference3 + ','; string strURL = "http://www.orderonline.cn/printNo.action?print=A4&remak=1&prType=W&hawbNos=" + item.SOReference3; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strURL); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); returnHtml += Regex.Match(retString, @"(?is)<div class=""main p_r"">((?<o><div[^<>]*>)|(?<-o></div>)|(?!</?div>).)+(?(o)(?!))</div>").Groups[0].Value.Trim(); Regex reg = new Regex("<td colspan=\"2\" class=\"bar-con tx-ce \">([\\s\\S]*?)<\\/td>", RegexOptions.IgnoreCase); MatchCollection mac = reg.Matches(returnHtml); var code="barbecue?fmt=png&height=40&type=code128&data="+item.SOReference3; foreach (Match m in mac) { string f1 = m.Groups[1].Value.Replace("/[\r\n]/g", ""); returnHtml = returnHtml.Replace(f1, "<div style=\"float:left\"><img style=\"width:260px;height:40px;\" src="+code+"> </div><div style=\"float:right;\"><img src=\"/images/print/th.png\" style=\"width:40px;height:40px;\" /></div>"); } } Regex regex = new Regex("/images", RegexOptions.IgnoreCase); returnHtml = regex.Replace(returnHtml, "http://www.orderonline.cn/images"); Regex regex1 = new Regex("barbecue", RegexOptions.IgnoreCase); returnHtml = regex1.Replace(returnHtml, "http://www.orderonline.cn/barbecue"); sbExpressHtml.Append(returnHtml+"</div></div>"); } context.Response.Write(sbExpressHtml.ToString()); } if (!string.IsNullOrEmpty(context.Request["getBlsExpressInfo"])) { string[] strParm = context.Request["getBlsExpressInfo"].Split('_'); StringBuilder sbExpressHtml = new StringBuilder(); var returnHtml = string.Empty; using (ExpressPrintDataContext db = new ExpressPrintDataContext()) { //var getExpressList = from p in db.DOC_Wave_Details // join pp in db.DOC_Order_Header // on p.OrderNo equals pp.OrderNo // where p.WaveNo.Contains(strParm[0]) && p.OrderNo.Contains(strParm[1]) // select new { p.WaveNo, p.OrderNo, pp.SOReference3 }; //資料放入ticket var apiToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", "mega", "bpost"))); string url = "http://42.121.252.25/api/lvsparcels?productBarcode=BLVS1592170000004048"; try { //配置請求header HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "GET"; request.ContentType = "text/json; charset=utf-8"; request.Headers.Add(HttpRequestHeader.Authorization, "basic " + apiToken); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); LvsParcelJson getExpressInfo = (LvsParcelJson)JsonConvert.DeserializeObject<LvsParcelJson>(retString); myStreamReader.Close(); myResponseStream.Close(); } catch (Exception e) { context.Response.Write(e.Message); } } } context.Response.End(); } public class LvsParcelJson { public int Id { get; set; } public string OrderNumber { get; set; } public string ProductBarcode { get; set; } public string RecipientName { get; set; } public string RecipientStreet { get; set; } public string RecipientHouseNumber { get; set; } public string RecipientBusnumber { get; set; } public string RecipientZipCode { get; set; } public string RecipientCity { get; set; } public string RecipientState { get; set; } public string RecipientCountry { get; set; } public string PhoneNumber { get; set; } public string SenderName { get; set; } public DateTime CreateTime { get; set; } public string SenderAddress { get; set; } public List<Customers> Customs { get; set; } } public class Customers { public string Sku { get; set; } public string ChineseContentDescription { get; set; } public string ItemContent { get; set; } public int ItemCount { get; set; } public decimal Value { get; set; } public string Currency { get; set; } public decimal Weight { get; set; } public string SkuInInvoice { get; set; } } public bool IsReusable { get { return false; } } }