当前位置: 首页 > 工具软件 > Zebra_Form > 使用案例 >

C# 简单的ZEBRA标签打印程序

陶宜民
2023-12-01

Zebra打印机有专用的的语言ZPL,简单的使用C#编程打印方法:

1. 写ZPL脚本,定义好相关打印格式,数据名,等,此保存为txt格式即可

脚本可以用BARONE5.0程序生成,

2. 通过C#程序,操作脚本txt文件,将需要打印的数据替换进脚本

C#如何操作txt,简单的不要再说了

3. 将此脚本通过DOS copy的方法,直接copy到ZEBRA的LPT口即可

            //将脚本送到打印口
            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.Arguments = "/c copy script-go.txt LPT1 ";
            cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            cmd.Start();
            while (true)
            {
                if (cmd.HasExited)
                {
                    break;
                }
                System.Threading.Thread.Sleep(400);
            }

 

 

完整实例:

打印标签,替换4点为1点,通过code取得名称,etc

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Diagnostics;

namespace LabelPrint
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            //using skin
            Sunisoft.IrisSkin.SkinEngine MySkin;
            MySkin = new Sunisoft.IrisSkin.SkinEngine(this);
            MySkin.SkinFile = "MacOS.ssk";
        }     

        //产生脚本的函数,将script内代码读取,参数替换,并生成新的script-go.txt
        private string ScriptStr, F_ScriptStr;
        private void ScriptGen()
        {
            StreamReader SR = new StreamReader("script.txt");
            ScriptStr = SR.ReadToEnd();
            SR.Close();
            F_ScriptStr = ScriptStr.Replace("$sn$", F_SN);
            F_ScriptStr = F_ScriptStr.Replace("$itemcode$", F_IC);
            F_ScriptStr = F_ScriptStr.Replace("$itemname$", F_IN);
            //得到修改后的字符串F_ScriptStr

            //生成新的txt文件
            File.Delete("script-go.txt");
            StreamWriter SW = new StreamWriter("script-go.txt", true);
            SW.Write(F_ScriptStr);
            SW.Close();
        }

        //输入框内的SN,输入框内的ItemCode,最终SN,最终ItemCode,最终ItemName
        private string SN, IC, F_SN, F_IC, F_IN;
        private void buttonPrint_Click_1(object sender, EventArgs e)
        {
            SN = textBoxSN.Text;
            IC = textBoxCode.Text;

            int SN_Len, IC_Len;
            SN_Len = SN.Length;
            IC_Len = IC.Length;

            //判断输入框内是否输入数据,数据位数是否正确
            if (SN_Len != 11)
            {
                MessageBox.Show("! Wrong : Please check the SN");
                textBoxSN.Focus();
                textBoxSN.SelectAll();
                return;
            }
            if (IC_Len < 11)
            {
                MessageBox.Show("! Wrong : Please check the itemcode");
                textBoxCode.Focus();
                textBoxCode.SelectAll();
                return;
            }

            //取出Itemcode的“4点”
            string F_IC1, F_IC2;
            F_SN = SN;
            F_IC1 = IC.Substring(0, 8);//已经包含"."

            switch (IC_Len)
            {
                case 11:
                    F_IC2 = IC.Substring(8, 3);
                    F_IC = F_IC1 + F_IC2;
                    break;

                case 12:
                    F_IC2 = IC.Substring(8, 4);
                    F_IC = F_IC1 + F_IC2;
                    break;

                case 14:
                    F_IC2 = IC.Substring(11, 3);
                    F_IC = F_IC1 + F_IC2;
                    break;

                case 15:
                    F_IC2 = IC.Substring(11, 4);
                    F_IC = F_IC1 + F_IC2;
                    break;

                default:
                    MessageBox.Show("! Wrong : Please check the itemcode");
                    textBoxCode.Focus();
                    textBoxCode.SelectAll();
                    break;
            }
            //这里已经得到最终的 F_SN 和 F_IC

            //下面连接数据库,核对输入的F_IC,得到对应的ItemName, F_IN          
            string Connstr = "Provider = Microsoft.Jet.OLEDB.4.0; Jet OLEDB:Database Password=rctool;Data Source = convert.mdb";
            OleDbConnection Conn = new OleDbConnection(Connstr);
            Conn.Open();
            string CMDStr = "SELECT * FROM cvn WHERE code='" + F_IC + "'";
            OleDbCommand CMD = new OleDbCommand(CMDStr, Conn);
            OleDbDataReader dr = CMD.ExecuteReader();
            if (dr.HasRows == false)
            {
                MessageBox.Show("This code doesn't exist in database");
                Conn.Close();
                textBoxCode.Focus();
                textBoxCode.SelectAll();
                return;
            }
            if (dr.HasRows == true)
            {
                dr.Read();
                F_IN = dr["name"].ToString();
                Conn.Close();
            }
            //这里已经得到最终的 F_IN
            //至此F_SN,F_IC,F_IN已经全部得到

            //下面开始输出这些参数,生成新的打印脚本script-go.txt,脚本默认产生在程序目录下面
            ScriptGen();

            //下面将此脚本送到打印口
            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.Arguments = "/c copy script-go.txt LPT1 ";
            cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            cmd.Start();
            while (true)
            {
                if (cmd.HasExited)
                {
                    break;
                }
                System.Threading.Thread.Sleep(400);
            }
            textBoxCode.Clear();
            textBoxSN.Focus();
            textBoxSN.SelectAll();
        }


       
    }
}

 

 

 类似资料: