在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 表格数据导入 条码打印

表格数据导入 条码打印

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:8.16M
  • 下载次数:50
  • 浏览次数:384
  • 发布时间:2020-04-02
  • 实例类别:C#语言基础
  • 发 布 人:shangguan1209
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 数据导入 表格 数据 导入 打印

实例介绍

【实例简介】

表格数据导入 条码打印 

【实例截图】

from clipboardfrom clipboard

【核心代码】

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.Drawing.Printing;
using Sunisoft.IrisSkin;
using xreb_owner_appls;
using System.Collections;

namespace BarCodePrint
{
    public partial class BarCodePrint : Form
    {
        public BarCodePrint()
        {
            InitializeComponent();
            btnPrint.Enabled = false;
            btnPrintCurrentData.Enabled = false;
        }


        LoadData ld = new LoadData();

        DataTable dt;
        
        private PrintDocument m_printDoc = null;//打印文档
        private PrintPreviewDialog m_printPreview = null;//打印预览UI
        private PrinterSettings psetting = new PrinterSettings();//实例打印设置对象

        private float m_pageWidth = 60F;//纸张宽度 mm单位
        private float m_pageHeight = 22F;

        System.Drawing.Font font = new System.Drawing.Font("宋体", 7, FontStyle.Bold);


        bool printCurrentPage = true;

        private BarCodeGenerate bcc = new BarCodeGenerate();

        private void BarCodePrint_Load(object sender, EventArgs e)
        {
            // LoadDll();
            skinEngine1.SkinFile = System.Environment.CurrentDirectory "\\skins\\WaveColor1.ssk";

            m_printDoc = new PrintDocument();//实例打印文档对象
            m_printPreview = new PrintPreviewDialog();

            m_printPreview.PrintPreviewControl.Zoom = 1;
            m_printPreview.Width = Convert.ToInt32(m_pageWidth) * 2;// Screen.PrimaryScreen.Bounds.Width;
            m_printPreview.Height = Convert.ToInt32(m_pageHeight) * 2;// Screen.PrimaryScreen.Bounds.Height;

            //自定义纸张大小
            m_printDoc.DefaultPageSettings.PaperSize = new PaperSize("newPage60X22", (int)(m_pageWidth / 25.4 * 100)
           , (int)(m_pageHeight / 25.4 * 100));
            //自定义图片内容整体上间距/左间距
            m_printDoc.OriginAtMargins = true;
            m_printDoc.DefaultPageSettings.Margins.Top = 0;// (int)(2 / 25.4 * 100);
            m_printDoc.DefaultPageSettings.Margins.Left = 0;// (int)(2 / 25.4 * 100);
            //打印事件
            m_printDoc.PrintPage = new PrintPageEventHandler(m_printDoc_PrintPage);
        }

        private void lblPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx";//过滤一下,只要表格格式的
            ofd.InitialDirectory = "C:\\";
            ofd.RestoreDirectory = true;
            ofd.FilterIndex = 1;
            ofd.AddExtension = true;
            ofd.CheckFileExists = true;
            ofd.CheckPathExists = true;
            ofd.ShowHelp = true;//是否显示帮助按钮

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = ofd.FileName;
            }
        }

        private void btnReadData_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            dataGridView1.Columns.Clear();

            //MessageBox.Show(ld.GetSth());

            try
            {
                dt = ld.LoadExcelByPath(txtPath.Text.ToString());
                //Workbook workbook = new Workbook(txtPath.Text.ToString());
                //Cells cells = workbook.Worksheets[workbook.Worksheets.ActiveSheetIndex].Cells;
                //dt = cells.ExportDataTable(0, 0, cells.MaxRow 1, cells.MaxColumn 1, true);//showTitle
            }
            catch
            {
                MessageBox.Show("未能加载数据,请检查Excel文件(地址、格式)或者联系IT部门", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //Cells cells = workbook.Worksheets[0].Cells;




            //在最前列添加一列CheckBox
            DataGridViewColumn myCol = new DataGridViewCheckBoxColumn();
            myCol.Name = "选择";
            myCol.ReadOnly = false;
            dataGridView1.Columns.Add(myCol);

            if (dt.Rows.Count > 0)
            {
                dataGridView1.DataSource = dt;
                dataGridView1.Rows[0].Selected = true;
                dataGridView1.Columns[0].Width = 50;
                dataGridView1.Columns[1].Width = 60;
                dataGridView1.Columns[1].ReadOnly = true;
                dataGridView1.Columns[2].ReadOnly = true;
                dataGridView1.Columns[3].ReadOnly = true;
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.RowHeadersVisible = false;
                //dataGridView1.ReadOnly = true;
                //dataGridView1.ColumnHeadersVisible = false;
                //barcodeControl1.ShowCode39StartStop = false;
                //barcodeControl1.CopyRight = "";
                // barcodeControl1.Data = dt.Rows[0][0].ToString() "-" dt.Rows[0][2].ToString();

                bcc.CreateBarCode(pictureBox1, dt.Rows[0][0].ToString() "-" dt.Rows[0][2].ToString());
                btnPrint.Enabled = true;
                btnPrintCurrentData.Enabled = true;
            }
            else
            {
                MessageBox.Show("无数据导入,请重新加载数据...", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                printCurrentPage = false;
                //m_printPreview.Document = m_printDoc;//把打印文档显示到预览对话框中
                //m_printPreview.ShowDialog();
                m_printDoc.Print();



                //if (DialogResult == DialogResult.OK)
                //{
                MessageBox.Show("打印成功", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
                //else
                //{
                //    MessageBox.Show("打印取消", "恭喜提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}               

            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" ex.Message ";请检查后重新打印。", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //throw ex;
            }
            finally
            {
                currentRowIndex = 0;
            }
        }


        int currentRowIndex = 0;
        /// <summary>
        /// 绘制需要打印的内容
        /// </summary>
        void m_printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {


            if (!printCurrentPage)
            {
                Bitmap bp = null;
                e.Graphics.DrawString("物料描述:" dt.Rows[currentRowIndex][1].ToString(), font, Brushes.Black, 2, 55);
                e.Graphics.DrawString("物料编码:" dt.Rows[currentRowIndex][0].ToString(), font, Brushes.Black, 2, 70);
                e.Graphics.DrawString("批次:" dt.Rows[currentRowIndex][2].ToString(), font, Brushes.Black, 110, 70);

                //Image newImage = Image.FromFile("logo.png");
                //e.Graphics.DrawImage(newImage, 250, 0,100,80);

                //Graphics g = e.Graphics;
                //barcodeControl1.Data = dt.Rows[currentRowIndex][0].ToString() "$" dt.Rows[currentRowIndex][2].ToString();
                //barcodeControl1.Draw(g, barcodeControl1.ClientRectangle, GraphicsUnit.Inch, 0.01f, 0, null);

                Rectangle rec1 = new Rectangle(2, 2, 200, 50);
                bp = bcc.CreateBarCodeBitMap(dt.Rows[currentRowIndex][0].ToString() "-" dt.Rows[currentRowIndex][2].ToString(), rec1.Width, rec1.Height);
                e.Graphics.DrawImage(bp, rec1);

                if (currentRowIndex == dt.Rows.Count - 1)
                {
                    e.HasMorePages = false;
                }
                else
                {
                    e.HasMorePages = true;
                    currentRowIndex ;
                }
            }
            else
            {
                e.Graphics.DrawString("物料描述:" dtNew.Rows[currentRowIndex][1].ToString(), font, Brushes.Black, 2, 55);
                e.Graphics.DrawString("物料编码:" dtNew.Rows[currentRowIndex][0].ToString(), font, Brushes.Black, 2, 70);
                e.Graphics.DrawString("批次:" dtNew.Rows[currentRowIndex][2].ToString(), font, Brushes.Black, 110, 70);

                //Image newImage = Image.FromFile("logo.png");                   
                //e.Graphics.DrawImage(newImage, 250, 0,100,80);

                //barcodeControl1.Data = dtNew.Rows[currentRowIndex][0].ToString() "$" dtNew.Rows[currentRowIndex][2].ToString();
                //Graphics g = e.Graphics;
                //barcodeControl1.Draw(g, barcodeControl1.ClientRectangle, GraphicsUnit.Inch, 0.01f, 0, null);
                //g.Dispose();
                Bitmap bp = null;
                Rectangle rec1 = new Rectangle(2, 2, 200, 50);
                bp = bcc.CreateBarCodeBitMap(dtNew.Rows[currentRowIndex][0].ToString() "-" dtNew.Rows[currentRowIndex][2].ToString(), rec1.Width, rec1.Height);//this.splitContainer1.Panel2.Width, this.splitContainer1.Panel2.Height
                e.Graphics.DrawImage(bp, rec1);

                if (currentRowIndex == dtNew.Rows.Count - 1)
                {
                    e.HasMorePages = false;
                }
                else
                {
                    e.HasMorePages = true;
                    currentRowIndex ;
                }
            }

        }


        DataTable dtNew;

        private void btnPrintCurrentData_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();


            int count = dataGridView1.Rows.Count;
            dataGridView1.MultiSelect = true;

            for (int i = 0; i < count; i )
            {
                dataGridView1.EndEdit();
                DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                bool flag = Convert.ToBoolean(checkCell.Value);

                if (flag == true)
                {
                    list.Add(i);
                    dataGridView1.Rows[i].Selected = true;
                }
            }

            if (list.Count < 1)
            {
                MessageBox.Show("未选择数据,请重新选择.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            dtNew = dt.Clone();

            DataRow dr = null;

            foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
            {
                dr = dtNew.NewRow();
                dr[0] = item.Cells[1].Value;
                dr[1] = item.Cells[2].Value;
                dr[2] = item.Cells[3].Value;
                dtNew.Rows.Add(dr);
            }
            //foreach (int i in list)
            //{
            //    dtNew.Rows.Add(dt.Rows[i].ItemArray);  //添加数据行
            //}


            try
            {

                printCurrentPage = true;
                //m_printPreview.Document = m_printDoc;
                //m_printPreview.ShowDialog();
                //m_printDoc.PrintPage = new PrintPageEventHandler(pd_PrintPage_singleData);
                m_printDoc.Print();
                //if (DialogResult == DialogResult.OK)
                //{
                MessageBox.Show("打印成功", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
                //else
                //{
                //    MessageBox.Show("打印取消", "恭喜提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                currentRowIndex = 0;
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                int index = dataGridView1.CurrentRow.Index;
                bcc.CreateBarCode(pictureBox1, dt.Rows[index][0].ToString() "-" dt.Rows[index][2].ToString());
                //barcodeControl1.ShowCode39StartStop = false;
                ////barcodeControl1.CopyRight = "XREB";
                //barcodeControl1.Data = dt.Rows[index][0].ToString() "-" dt.Rows[index][2].ToString();
                lblMatDesc.Text = "物料描述:" dt.Rows[index][1].ToString();
                lblMatNOBatch.Text = "批次:" dt.Rows[index][2].ToString();
            }
            catch
            { }
        }

    }
}


实例下载地址

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警