c#中如何用codesoft来打印标签开发分享

以前使用过delphi和java开发过使用codesoft,最近因需要用c#开发了一个用codesoft来打印标签的程序。源代码分享给需要的人。

业务背景:

    把datagridview中的数据打印 标签(需要对coesoft软件有一定的了解)。

操作方法:

1  使用codesoft制做一个打印模板(这里就不再讲了)

2  在c#项目中引用codesoft插件。

         找到项目中的[引用],右击添加引用,找到浏览,选中c:\program files\cs6下的Lppx2.tlb,这样组件就安装完成。


     

安装完后,在引用下面会出现:


3  源代码及说明如下,下面是一个打印的画面和程序:



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

namespace OPurchase
{
    public partial class frmJP_CODESOFT_PT : Form
    {
   
        public int ilableLx;//打印模板类型,目前这里仅一个模板
        public DataGridView dv;//定义公共变量,调用这个窗口时把datagridview传进来,
        public frmJP_CODESOFT_PT()
        {
            InitializeComponent();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                Close();
            }
            else if (keyData == Keys.Enter)
            {

                SendKeys.Send("{tab}");
                return true;

            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnSet_Click(object sender, EventArgs e)
        {

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (ilableLx == 0)//模板一
            {
                LabelManager2.ApplicationClass labApp = null;
                LabelManager2.Document doc = null;
                string labFileName=System.Windows.Forms.Application.StartupPath + @"\barJapLab.Lab";              
                try
                {

                    if (!File.Exists(labFileName))
                    {
                        MessageBox.Show("沒有找到標簽模板文件:barJapLab.Lab,請聯系系統管理員", "溫馨提示");
                        return;
                    }
                    labApp = new LabelManager2.ApplicationClass();
                    labApp.Documents.Open(labFileName, false);// 调用设计好的label文件
                    doc = labApp.ActiveDocument;
                    if (dv.SelectedRows.Count > 0)
                    {
                        progressBar1.Maximum = dv.SelectedRows.Count;
                        progressBar1.Value = 0;
                        
                        for (int i = 0; i < dv.SelectedRows.Count; i++)  //选中多行打印
                        {  
                            //下面这些是给模板的变量传值进去                         
                            doc.Variables.FormVariables.Item("barcode_no").Value = dv.SelectedRows[i].Cells["barcode_no"].Value.ToString();
                            doc.Variables.FormVariables.Item("style_no").Value = dv.SelectedRows[i].Cells["style_no"].Value.ToString();
                            doc.Variables.FormVariables.Item("upp_jp").Value = dv.SelectedRows[i].Cells["upp_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("outsole_jp").Value = dv.SelectedRows[i].Cells["outsole_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("col_jp").Value = dv.SelectedRows[i].Cells["col_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("size_width").Value = dv.SelectedRows[i].Cells["size_width"].Value.ToString();
                            doc.Variables.FormVariables.Item("price").Value = dv.SelectedRows[i].Cells["price"].Value.ToString();
                            doc.Variables.FormVariables.Item("comp_jp").Value = dv.SelectedRows[i].Cells["comp_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("address_jp").Value = dv.SelectedRows[i].Cells["address_jp"].Value.ToString();
                            doc.Variables.FormVariables.Item("url").Value = dv.SelectedRows[i].Cells["url"].Value.ToString();
                            //下面这行是打印份数的定义
                            doc.PrintDocument(Convert.ToInt16(dv.SelectedRows[i].Cells["qty"].Value) + Convert.ToInt16(dv.SelectedRows[i].Cells["att_qty"].Value));
                            progressBar1.Value++;
                            progressBar1.Refresh();
                            lbStatus.Text = "總共" + dv.SelectedRows.Count.ToString() + "行需要列印,已送出"+Convert.ToString (i+1)+"行";
                            lbStatus.Refresh();
                            this.Refresh();

                        }
                    }
                    else  //光标所在行打印
                    {
                        doc.Variables.FormVariables.Item("barcode_no").Value = dv.CurrentRow.Cells["barcode_no"].Value.ToString();
                        doc.Variables.FormVariables.Item("style_no").Value = dv.CurrentRow.Cells["style_no"].Value.ToString();
                        doc.Variables.FormVariables.Item("upp_jp").Value = dv.CurrentRow.Cells["upp_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("outsole_jp").Value = dv.CurrentRow.Cells["outsole_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("col_jp").Value = dv.CurrentRow.Cells["col_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("size_width").Value = dv.CurrentRow.Cells["size_width"].Value.ToString();
                        doc.Variables.FormVariables.Item("price").Value = dv.CurrentRow.Cells["price"].Value.ToString();
                        doc.Variables.FormVariables.Item("comp_jp").Value = dv.CurrentRow.Cells["comp_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("address_jp").Value = dv.CurrentRow.Cells["address_jp"].Value.ToString();
                        doc.Variables.FormVariables.Item("url").Value = dv.CurrentRow.Cells["url"].Value.ToString();                        
                        doc.PrintDocument(Convert.ToInt16(dv.CurrentRow.Cells["qty"].Value));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("出錯啦,原因如下:\n\r"+ex.Message,"出錯啦");
                }
                finally
                {
                    labApp.Documents.CloseAll(true);
                    labApp.Quit();//退出
                    labApp = null;
                    doc = null;
                    GC.Collect(0);
                }
                

            }
        }

        private void frmJP_CODESOFT_PT_Load(object sender, EventArgs e)
        {
            if (dv.SelectedRows.Count >0)
             lbStatus.Text = "總共"+dv.SelectedRows .Count .ToString ()+"行需要列印";
            else
             lbStatus.Text = "總共1行需要列印";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}




评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值