表格数据导入 条码打印 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
{ }
}
}
}
评论