• 按照已有的模板输出<一>(如发票)


                                               按照已有的模板输出<一>

     普通的发票基本上都是固定模式,所以我们一般写好固定的模板,把其中需要变动的地方,以特定符号来代替。每次打印发票的时候,只需将其中的特定符号转换成我们需要显示的数据。。。

    1)  新建一个txt格式的发票模板,如图6.1 发票模板:

     

             图6.1 发票模板

     

    1)我现在模拟相同情况,编写一个对象实体并赋值

    Public class  FaPiao{

     //合同号

      public string Contract { get; set; }

    //姓名

     public string Name { get; set; }

     //站点

    public string Site{ get; set; }

     

    //级别 金额等的集合

    public List<LevelAndPrice> LevelandPrice{ get; set; }

     

    //总净重

    public Decimal Net{ get; set; }

    //总皮重

    public Decimal Tare{ get; set; }

     

    //总毛重

    public Decimal Gross{ get; set; }

     

    //总金额

    public Decimal Money { get; set; }

     

    //流水号

    public int  Serial{ get; set; }

     

    //当前页数

    public int  Page{ get; set; }

     

    //总页数

    public int  Pages{ get; set; }

     

    //磅码员

    public string   Weighter{ get; set; }

     

    //定级员

    public string   Levelter{ get; set; }

     

    //终端

    public string   Line{ get; set; }

     

    }

     

      Public  class  LevelandPrice {

       //合同号

    public string   Contract{ get; set; }

       

       //级别

    public string   Level{ get; set; }

     

       //净重

    public  Decimal Weight{ get; set; }

     

       //金额

    public  Decima  Price{ get; set; }

     

    }

     

      给实体赋值,这里我就不赋值了 。根据实际情况赋值就好。

    2)给模板变量赋值

           /// <summary>

            /// 烟叶收购过磅单模板

            /// </summary>

            /// <param name="templeteFilePath">模板文件路径</param>

            /// <param name=" fapiao ">发票对象</param>

    /// <param name=" currentPage ">当前页</param>

    /// <param name=" totalPage ">总页数</param>

        /// <returns> </returns>

    Public  static  string[] GetGdb(string templeteFilePath, FaPiao fapiao, int currentPage, int totalPage)

    {

    //获得模板

      StreamReader objReader = new StreamReader(templeteFilePath, Encoding.UTF8);

     //将模板的按照每一行存入到一个string集合中。

           string line;

            List<string> contents = new List<string>();

            while ((line = objReader.ReadLine()) != null)

                {

                    contents.Add(line + " ");

              }

    //现在我们需要考虑一下Level,weight,price。因为这个地方一般来说不会只显示一行值,可能有多行值,相当于我们去超市买商品所打的发票,我们买很多商品就会列出很多条目。这是一个动态的。现在我们的考虑如何给这部分动态的赋值。

    //查找到level在模板中是第几行,转换来就是查找出在contents集合中的位置,因为上一步代码我们已经按照行数将模板内容存在了contents集合中了。

     var index = contents.FindIndex(p => p.Contains("{level}"));

    //查找出这一行的模板内容,就是已经找到level所在的行的内容,就是我们需要查找的{level}    {weight}    {price}

      var template = contents[index];

    //去掉模板中的这一行内容

      contents.RemoveAt(index);

    //循环给{level}    {weight}    {price}循环赋值,将这动态的几行添加到模板中去。

    List<LevelAndPrices > levelandPrices= fapiao.LevelandPrice;

      for (int i = 0; i < levelandPrices.Count; i++)

                {//级别

                    var tmp = template.Replace("{level}", levelandPrices[i].Level);

                    //净重

                    tmp = tmp.Replace("{weight}", levelandPrices[i].Weight.ToString("0.00"));

                    //金额

                    tmp = tmp.Replace("{price}", levelandPrices[i].Price.ToString("0.00"));

                    contents.Insert(index + i, tmp);

                }

    //循环部分已经添加完成了,现在给模板剩余部分添加数据

        string allLine = string.Empty;

         for (int i = 0; i < contents.Count - 2; i++)

                {

                    allLine += contents[i];

                }

             //合同()

                allLine = allLine.Replace("{contract}", fapiao.Contract);

                //姓名

                allLine = allLine.Replace("{name}", fapiao.Name);

                //站点

                allLine = allLine.Replace("{site}", fapiao.Site);

                //打印时间

                allLine = allLine.Replace("{date}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                //总净重

                allLine = allLine.Replace("{net}", fapiao.Net.ToString("0.00"));

                //总皮重

                allLine = allLine.Replace("{tare}", fapiao.Tare.ToString("0.00"));

                //毛重

                allLine = allLine.Replace("{gross}", fapiao.Gross.ToString("0.00"));

                //金额

                allLine = allLine.Replace("{money}", fapiao.Money.ToString("0.00"));

                //流水号

                allLine = allLine.Replace("{serial}", fapiao.Serial);

                //磅码员

                allLine = allLine.Replace("{weighter}", fapiao.Weihter);

                //定级员

                allLine = allLine.Replace("{levelter}", fapiao.Levelter);

                //终端

      allLine = allLine.Replace("{line}", fapiao.Line);

               //当前页

                allLine = allLine.Replace("{page}", currentPage.ToString());

                //总页

                allLine = allLine.Replace("{pages}", totalPage.ToString());

                //最后一页加几个换行符,多打印几行

              return new[] { allLine, contents[contents.Count - 1]+" " };

    }

    GetGdb方法已经给模板中的所有的变量赋值,如果要看打印的效果,直接循环引用GetGdb()方法,循环输出每一行查看效果。。。如果要将这个模板发送给打印机,在和打印机约定好协议后,只需要将string[]转化成为二进制流或者GB2312发送给打印机即可,这要看打印机方需要那种格式。

  • 相关阅读:
    C++多态的实现原理
    C++编程之面向对象的三个基本特征
    C/C++中static关键字详解
    C/C++中static关键字作用总结
    Linux防CC攻击脚本
    linux下防火墙iptables原理及使用
    linux下使用 TC 对服务器进行流量控制
    awr报告与statspack报告
    awr报告
    statspack报告
  • 原文地址:https://www.cnblogs.com/yaoxiaodan/p/6279171.html
Copyright © 2020-2023  润新知