C#打印小票(连续自动打印)
2021/4/12 14:26:20
本文主要是介绍C#打印小票(连续自动打印),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
注意前端需要引用(LodopFuncs.js,JsBarcode.all.min.js,jquery.js)当然还有安装打印机驱动还有Lodop的驱动
前端代码调用打印方法部分
json.printinfo是后端传向前端的生成好的打印代码。
//执行打印小票 var orderids = ""; if (json.printinfo != "") { var imgsrc = "\"../../../Content/img/wjywx.jpg\""; function print() { let jsCode = eval(json.printinfo) //打印小票完成根据订单id去修改订单在数据库中的状态 $.ajax({ type: 'POST', url: "../Home/UpdateOrderStatic?Orderid=" + orderids, data: "", dataType: 'json', async: false, cache: false, success: function (json) { if (json.responseText == "True") { alert("打印完成") } else { alert("修改订单状态失败") } } }) } print(); }
打印小票用到的实体
/// <summary> /// 打印小票实体 /// </summary> public class Printmodel { /// <summary> /// 小票顶部标题例如【含处方药】 /// </summary> private string title; /// <summary> /// 平台例如【京东到家】 /// </summary> private string platform; /// <summary> /// 订单序号例如【#4】顾客联 /// </summary> private string number; /// <summary> /// 门店名称【例如阳光小镇24H店】 /// </summary> private string storeName; /// <summary> /// 下单时间 /// </summary> private string orderCreateTime; /// <summary> /// 预计送达时间 /// </summary> private string deliveryTime; /// <summary> /// 订单id /// </summary> private string orderId; /// <summary> /// 客户名称 /// </summary> private string userName; /// <summary> /// 客户联系电话 /// </summary> private string userTel; /// <summary> /// 客户备注 /// </summary> private string userNote; /// <summary> /// 用户地址 /// </summary> private string userAddress; /// <summary> /// 商品信息 /// </summary> private List<GoodsModel> goods; /// <summary> /// 商品优惠金额 /// </summary> private decimal preferential; /// <summary> /// 配送全额 /// </summary> private decimal distribution; /// <summary> /// 包装费 /// </summary> private decimal packaging; /// <summary> /// 门店联系电话 /// </summary> private string storeTel; /// <summary> /// 投诉电话 /// </summary> private string complaintsTel; /// <summary> /// 温馨·提示内容 /// </summary> private string prompt; /// <summary> /// 微信二维码路径 /// </summary> private string wxImg; /// <summary> /// 图片下面的消息 /// </summary> private string message; /// <summary> /// 标题 /// </summary> public string Title { get => title; set => title = value; } /// <summary> /// 平台 /// </summary> public string Platform { get => platform; set => platform = value; } /// <summary> /// 订单序号例如【#4】 /// </summary> public string Number { get => number; set => number = value; } /// <summary> /// 门店名称 /// </summary> public string StoreName { get => storeName; set => storeName = value; } /// <summary> /// 下单时间 /// </summary> public string OrderCreateTime { get => orderCreateTime; set => orderCreateTime = value; } /// <summary> /// 预计到达时间 /// </summary> public string DeliveryTime { get => deliveryTime; set => deliveryTime = value; } /// <summary> /// 订单id /// </summary> public string OrderId { get => orderId; set => orderId = value; } /// <summary> /// 顾客姓名 /// </summary> public string UserName { get => userName; set => userName = value; } /// <summary> /// 顾客联系电话 /// </summary> public string UserTel { get => userTel; set => userTel = value; } /// <summary> /// 顾客地址 /// </summary> public string UserAddress { get => userAddress; set => userAddress = value; } /// <summary> /// 商品信息 /// </summary> public List<GoodsModel> Goods { get => goods; set => goods = value; } /// <summary> /// 计算商品总价 /// </summary> /// <param name="goods">商品集合</param> /// <returns></returns> private decimal Suninfull(List<GoodsModel> goods) { decimal sun = 0; if (goods.Count > 0 && goods != null) { foreach (GoodsModel item in goods) { sun += item.Money; }; }; return sun; } /// <summary> /// 商品全额 /// </summary> public decimal SumInFull { get => Suninfull(this.Goods); } /// <summary> /// 商品优惠金额 /// </summary> public decimal Preferential { get => preferential; set => preferential = value; } /// <summary> /// 配送费 /// </summary> public decimal Distribution { get => distribution; set => distribution = value; } /// <summary> /// 包装费 /// </summary> public decimal Packaging { get => packaging; set => packaging = value; } /// <summary> /// 获取商品总件数 /// </summary> /// <param name="goods">商品实体集合</param> /// <returns></returns> private int getGoodsNum(List<GoodsModel> goods) { int num = 0; foreach (GoodsModel item in goods) { num += item.Number; } return num; } /// <summary> /// 商品总件数 /// </summary> public int GoodsNum { get => getGoodsNum(this.goods); } /// <summary> /// 实付金额[总金额-优惠金额+配送费+包装费] /// </summary> public decimal ActualMoney { get => (SumInFull - Preferential + Distribution + Packaging); } /// <summary> /// 门店电话 /// </summary> public string StoreTel { get => storeTel; set => storeTel = value; } /// <summary> /// 投诉电话 /// </summary> public string ComplaintsTel { get => complaintsTel; set => complaintsTel = value; } /// <summary> /// 温馨提示内容 /// </summary> public string Prompt { get => prompt; set => prompt = value; } /// <summary> /// 微信二维码路径(相对路径) /// </summary> public string WxImg { get => wxImg; set => wxImg = value; } /// <summary> /// 二维码下部信息 /// </summary> public string Message { get => message; set => message = value; } /// <summary> /// 客户备注 /// </summary> public string UserNote { get => userNote; set => userNote = value; } }
打印小票的方法
M_Order_Info实体是订单相关信息。根据自己需求而定
/// <summary> /// 打印小票方法 /// </summary> /// <returns></returns> public string Print_Receipts(List<M_Order_Info> orderInfo) { #region 打印小票 int magin = 0; string orderids = string.Empty; string html = ""; html += " var LODOP;"; html += " function tprint(){"; html += " LODOP = getLodop();"; html += "LODOP.PRINT_INIT('');"; html += "LODOP.SET_PRINT_PAGESIZE('3', '58mm', '', '热敏');"; D_Order_Info doi = new D_Order_Info(); D_WX_StoreInfos StoreInfos = new D_WX_StoreInfos(); foreach (M_Order_Info item in orderInfo) { if (item.isprint == 0&&item.deliveryStationNoIsv!="19176") { Printmodel model = new Printmodel(); if(item.businessTag!=null&& item.businessTag.Contains("dj_prescription_order")) { model.Title = "含处方药"; } else { model.Title = ""; } model.Platform = GetEnumByValue(typeof(OrderSourceType), item.orderType.ToString()).ToString(); ; model.Number = item.orderNum.ToString(); model.StoreName = item.deliveryStationName; model.OrderCreateTime = item.orderStartTime.ToString(); model.DeliveryTime = item.orderPreEndDeliveryTime.ToString(); model.OrderId = item.orderId; orderids += "'" + model.OrderId + "',"; model.UserName = item.buyerFullName; model.UserTel = item.buyerMobile; model.UserNote = GetRealRemark(item.orderBuyerRemark).Trim(); model.UserAddress = Regex.Replace(item.buyerFullAddress,@"[^a-zA-Z0-9\u4e00-\u9fa5\s]", ""); List<GoodsModel> goodslist = new List<GoodsModel>(); foreach (Order_Products item2 in doi.get_Products(item.orderId)) { GoodsModel gmodel = new GoodsModel(); gmodel.GoodsName = item2.skuName; gmodel.Number = item2.skuCount; gmodel.Price = Convert.ToInt32(item2.skuStorePrice)/100M; gmodel.SKU = item2.skuId.ToString(); gmodel.UPC = item2.upcCode.ToString(); goodslist.Add(gmodel); } model.Goods = goodslist; model.Preferential = Convert.ToInt32(item.orderDiscountMoney)/100M; model.Packaging = Convert.ToInt32(item.packagingMoney)/100M; model.Distribution = Convert.ToInt32(item.orderFreightMoney)/100M; model.StoreTel = StoreInfos.GetStoreInfoByStoreID(item.deliveryStationNoIsv).telephone; model.ComplaintsTel = "4000239900"; model.Prompt = "此商品属于特殊商品,非质量问题概不退换"; model.WxImg ="\'+imgsrc+\'"; model.Message = "小票底部信息"; html += Print(model, ref magin); magin += magin; } } html += " LODOP.PREVIEW();"; //LODOP.PRINT(); html += "}tprint();orderids=" + orderids.Substring(0, orderids.Length - 1)+";"; return html; #endregion }
生成打印小票的前端代码
/// <summary> /// 生成前端打印小票代码 /// </summary> /// <param name="per"></param> /// <returns></returns> public string Print(Printmodel per,ref int magin) { string html = ""; int topmagin = 5+ magin; //html += " var LODOP;"; //html += " function tprint(){"; //html += " LODOP = getLodop();"; //html += "LODOP.PRINT_INIT('');"; //html += "LODOP.SET_PRINT_PAGESIZE('3', '58mm', '', '热敏');"; if (!string.IsNullOrWhiteSpace(per.Title)) { html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', 0,183, 100, '※" + per.Title + "※');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 13);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2); "; } else { topmagin=topmagin - 5; } html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 7) + "mm', 0, 183, 100, '" + per.Platform + "|#" + per.Number + "顾客联');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 11);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 6) + "mm', 0, 183, 100, '" + per.StoreName + "');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 6) + "mm', 5, 183, 100, '下单时间:" + per.OrderCreateTime + "');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '预计送达:" + per.DeliveryTime + "');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '订单编号:" + per.OrderId + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_BARCODE('" + (topmagin += 4) + "mm','5','25%', 90, '128Auto', '" + per.OrderId + "');"; html += "LODOP.SET_PRINT_STYLEA(0, 'AlignJustify', 2);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 34) + "mm', 0, 183, 100, '***顾客信息***');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '客户:" + per.UserName + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '联系电话:" + per.UserTel + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; if (!string.IsNullOrWhiteSpace(per.UserNote)) { html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 8) + "mm', 5, 183, 100, '备注:" + per.UserNote + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 2);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; } html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 8) + "mm', 5, 183, 100, '地址:" + per.UserAddress + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 11) + "mm', 5, 183, 100, '* ');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '* ');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 8) + "mm', 5, 183, 100, '裁剪线:✂↘️ ');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_LINE('" + (topmagin += 11) + "mm', 0, '" + topmagin + "mm', 183, 1, 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '* ');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '* ');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 12) + "mm', 0, 183, 100, '***商品信息***');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '商品名称');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '35%', 183, 100, '数量');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '55%', 183, 100, '单价');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '75%', 183, 100, '金额');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_LINE('" + (topmagin += 3) + "mm', 0, '" + topmagin + "mm', 183, 2, 1);"; foreach (GoodsModel item in per.Goods) { html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 1) + "mm', 5, 183, 100,'" + item.GoodsName + "');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 8) + "mm', '35%', 183, 100, 'X" + item.Number + "');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '55%', 183, 100, '" + item.Price + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + topmagin + "mm', '75%', 183, 100, '" + item.Money + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, 'SKU码:" + item.SKU + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, 'UCP码:" + item.UPC + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; topmagin += 4; } html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 0, 183, 100, '***结算信息***');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 10);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 2);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 0, 183, 100, '商品金额:¥" + per.SumInFull + " 商品优惠:¥" + per.Preferential + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 7.5);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 3);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('"+ (topmagin += 4) + "mm', 0, 183, 100, '配送金额:¥" + per.Distribution + " 包装费:¥" + per.Packaging + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 7.5);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 3);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_LINE('" + (topmagin += 5) + "mm', 0, '" + topmagin + "mm', 183, 2, 1);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 1) + "mm', 0, 183, 100, '总件数:" + per.GoodsNum + "');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 12);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += "LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 0, 183, 100, '实付:" + per.ActualMoney + "');"; html += "LODOP.SET_PRINT_STYLEA(0, 'FontSize', 12);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Bold', 1);"; html += "LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 6) + "mm', 5, 183, 100, '门店电话:" + per.StoreTel + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '投诉电话:" + per.ComplaintsTel + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; if (!string.IsNullOrWhiteSpace(per.Prompt)) { html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 4) + "mm', 5, 183, 100, '温馨提示:" + per.Prompt + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; } html += "LODOP.ADD_PRINT_IMAGE('" + (topmagin += 14) + "mm', 0, 183, '60mm', '<img src=" + per.WxImg + " />');"; html += "LODOP.SET_PRINT_STYLEA(0, 'Stretch', 2);"; html += " LODOP.ADD_PRINT_TEXT('" + (topmagin += 70) + "mm', 5, 183, 100, '" + per.Message + "');"; html += " LODOP.SET_PRINT_STYLEA(0, 'FontSize', 8);"; html += " LODOP.SET_PRINT_STYLEA(0, 'Alignment', 1);"; LODOP.PRINT(); //html += "}"; //html += " tprint()"; magin=topmagin; return html; }
这篇关于C#打印小票(连续自动打印)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具