实例介绍
【实例简介】
c#写的多页打印源码
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using RTQPrint.Code; using System.IO; using ThoughtWorks.QRCode.Codec; using System.Drawing.Printing; namespace RTQPrint { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private string asc_code = string.Empty;//ASC代码 private string asc_name = string.Empty;//ASC名称 private string temp_Path = string.Empty; private void MainForm_Load(object sender, EventArgs e) { cmb_state.SelectedIndex =0; Bind(); SetState(); GetAsc(); if (string.IsNullOrEmpty(asc_code)) { SetASCForm fasc = new SetASCForm(); fasc.Flag = true; fasc.Owner = this; fasc.ShowDialog(); } } public void GetAsc() { asc_code = AppSettings.GetAppSettingValue("ASC"); asc_name = AppSettings.GetAppSettingValue("Name"); txt_asc_code.Text = asc_code; txt_asc_name.Text = asc_name; } private void SetState() { for (int i = 0; i < dataGridView1.RowCount; i ) { string flag=dataGridView1.Rows[i].Cells["Column17"].Value.ToString(); if (string.IsNullOrEmpty(flag)) { dataGridView1.Rows[i].Cells["Column17"].Value = "未打印"; } else { dataGridView1.Rows[i].Cells["Column17"].Value = "已打印"; dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.RosyBrown; } string week_id = dataGridView1.Rows[i].Cells["week_id"].EditedFormattedValue.ToString();//索赔周次 string work_id = dataGridView1.Rows[i].Cells["work_id"].EditedFormattedValue.ToString();//索赔工单号 string bank_id = dataGridView1.Rows[i].Cells["bank_id"].EditedFormattedValue.ToString();//行号 string vin = dataGridView1.Rows[i].Cells["vin"].EditedFormattedValue.ToString();//VIN码 string exception_fit_id = dataGridView1.Rows[i].Cells["exception_fit_id"].EditedFormattedValue.ToString();//配件代码 if (string.IsNullOrEmpty(week_id)) { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; } else if (string.IsNullOrEmpty(exception_fit_id)) { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; } else if (string.IsNullOrEmpty(work_id)) { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; } else if (string.IsNullOrEmpty(bank_id)) { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; } else if (string.IsNullOrEmpty(vin)) { dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red; } } dataGridView1.ClearSelection(); } private void btn_Excel_Click(object sender, EventArgs e) { try { openFileDialog1.Filter = "Excel File(*.xls)|*.xls"; if (openFileDialog1.ShowDialog()==DialogResult.OK) { InsertPictureToExcel ipt = new InsertPictureToExcel(); ipt.Open(openFileDialog1.FileName); ipt.Insert("标记"); ipt.SaveFile(openFileDialog1.FileName); ipt.Close(); ipt.Dispose(); DataTable dt = WareHouse.SelectAll(openFileDialog1.FileName); QueryBind(dt); AppSettings.SetConnectionStringsValue("Path", openFileDialog1.FileName); temp_Path = openFileDialog1.FileName; SetState(); } } catch (Exception ex) { MessageBox.Show("导入失败!请检查Excel格式!" ex.Message); } } private void Bind() { try { string path = AppSettings.GetConnectionStringsValue("Path"); if (!string.IsNullOrEmpty(path)) { if (File.Exists(path)) { temp_Path = path; DataTable dt = WareHouse.SelectAll(path); QueryBind(dt); } else { } } } catch (Exception ex) { AppSettings.SetConnectionStringsValue("Path",""); MessageBox.Show("程序出错!" ex.Message); } } private void QueryBind(DataTable dt) { for (int i = 0; i< dt.Rows.Count; i ) { string flag=dt.Rows[i]["标记"].ToString(); if (!string.IsNullOrEmpty(flag)) { dt.Rows[i].Delete(); } } dataGridView1.DataSource = dt; } private void btn_Query_Click(object sender, EventArgs e) { try { //if (dataGridView1.Rows.Count > 0) //{ string week_id = txt_week_id.Text.Trim(); string work_id = txt_work_id.Text.Trim(); string bank_id = txt_bank_id.Text.Trim(); string vin = txt_win.Text.Trim(); string fitting_code = txt_operate_code.Text.Trim(); string flag = cmb_state.Text.Trim(); DataTable dt = WareHouse.Select(week_id, work_id, bank_id, vin, fitting_code, flag, temp_Path); if (flag == "未打印") { QueryBind(dt); } else { dataGridView1.DataSource = dt.DefaultView; } SetState(); // } } catch (Exception ex) { MessageBox.Show("查询失败!请检查Excel格式!" ex.Message); } } private bool IsHasPrint() { bool flag = false; foreach (DataGridViewRow dgdr in dataGridView1.Rows) { if (dgdr.Cells["SelectColumn12"].EditedFormattedValue.ToString() == "True") { flag = true; break; } } return flag; } private void btn_Print_Click(object sender, EventArgs e) { try { string path = AppSettings.GetConnectionStringsValue("Path"); if (dataGridView1.Rows.Count > 0) { if (IsHasPrint()) { printDialog1.Document = printDocument1; DialogResult dr = printDialog1.ShowDialog(); if (dr == DialogResult.OK) { printDocument1.Print(); } } else { MessageBox.Show("请选择要打印的标签!"); } } else { MessageBox.Show("请先导入数据!然后选择打印!"); } } catch (InvalidPrinterException ex) { MessageBox.Show("打印机异常!请检查打印机!" ex.Message); } catch (Exception ex) { MessageBox.Show("删除失败!" ex.Message); } } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex != -1) { txt_operate_miaoshu.Text = dataGridView1.Rows[e.RowIndex].Cells["Column12"].Value.ToString(); txt_operate_deal.Text = dataGridView1.Rows[e.RowIndex].Cells["Column14"].Value.ToString(); txt_operate_reason.Text = dataGridView1.Rows[e.RowIndex].Cells["exception_reason"].Value.ToString(); } } private void btn_Save_Click(object sender, EventArgs e) { try { saveFileDialog1.Filter = "Excel File(*.xls)|*.xls"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { string path = AppSettings.GetConnectionStringsValue("Path"); File.Copy(path, saveFileDialog1.FileName); AppSettings.SetConnectionStringsValue("Path",""); dataGridView1.DataSource = null; temp_Path = ""; MessageBox.Show("备份成功!"); } } catch (Exception ex) { MessageBox.Show("备份失败!" ex.Message); } } private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { if (e.ColumnIndex!= 0) { e.Cancel = true; } } private void btn_setAsc_Click(object sender, EventArgs e) { SetASCForm fasc = new SetASCForm(); fasc.Flag = false; fasc.Owner = this; fasc.ShowDialog(); } private Image GetImage(string temp_asc_code,string week_id,string work_id,string bank_id,string vin,string exception_fit_id) { //生成二维码 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeScale = 2; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; qrCodeEncoder.QRCodeVersion = 4; string data = temp_asc_code week_id work_id bank_id vin exception_fit_id; return qrCodeEncoder.Encode(data); } //打印列表 List<TRQInfo> printlists = new List<TRQInfo>(); private void PrintDescriptionInfo(System.Drawing.Printing.PrintPageEventArgs e,int row_number) { switch (row_number) { case 2: case 3: case 4: case 5: e.Graphics.DrawString("请及时将索赔标签系在更换下的相应的故障件", new Font("楷体", 8, FontStyle.Regular), Brushes.Black, 10, 390); //390 e.Graphics.DrawString("上。并详细描述故障原因及位置,如描述原因", new Font("楷体", 8, FontStyle.Regular), Brushes.Black, 10, 410); e.Graphics.DrawString("与实际故障不一致,上海汽车将对该索赔执行", new Font("楷体", 8, FontStyle.Regular), Brushes.Black, 10, 430); e.Graphics.DrawString("抵扣,敬请注意。", new Font("楷体", 8, FontStyle.Regular), Brushes.Black, 10, 450); break; default: e.Graphics.DrawString("请及时将索赔标签系在更换下的相应的故障件", new Font("楷体", 8, FontStyle.Regular), Brushes.Black, 10, 390); //390 e.Graphics.DrawString("上。并详细描述故障原因及位置,如描述原因", new Font("楷体", 8, FontStyle.Regular), Brushes.Black, 10, 410); e.Graphics.DrawString("与实际故障不一致,上海汽车将对该索赔执行", new Font("楷体", 8, FontStyle.Regular), Brushes.Black, 10, 430); e.Graphics.DrawString("抵扣,敬请注意。", new Font("楷体", 8, FontStyle.Regular), Brushes.Black, 10, 450); break; } } /// <summary> /// 字符 字节 截取字符串 /// </summary> /// <param name="str">要截取的字符串</param> /// <param name="start">开始截取位置</param> /// <param name="length">截取的字符串长度</param> /// <param name="end">要截取长度的字节个数-1</param> /// <param name="nextend">要截取长度的字节个数</param> /// <param name="total_length">字符串总长度</param> /// <returns></returns> public static string Substring(string str, int start, int length, int end, int nextend, int total_length) { string temp = string.Empty; int temp_length = 0; int number = 0; if ((start length) <= total_length) { do { temp = str.Substring(start, length); number = System.Text.Encoding.GetEncoding("GB2312").GetByteCount(temp); length ; temp_length = start length; } while (number != nextend && number != end && temp_length <= total_length); } else { temp = str.Substring(start, total_length - start); } return temp; } private int page_number = 0; private int i = 0; //打印 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { page_number = printlists.Count-1;//打印的页数 //ASC名称 第一行 e.Graphics.DrawString("ASC名称: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 15); e.Graphics.DrawString(printlists[i].Asc_name, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 90, 15); //ASC代码 第二行 e.Graphics.DrawString("ASC代码: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 35); e.Graphics.DrawString(printlists[i].Asc_id, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 90, 35); //二维条码 e.Graphics.DrawImage(printlists[i].ImgRTQ, 170, 32, 80, 80); //索赔工单号 第三行 e.Graphics.DrawString("索赔工单号: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 55); e.Graphics.DrawString(printlists[i].Work_id, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 100, 55); //索赔周次 第四行 e.Graphics.DrawString("索赔周次: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 75); e.Graphics.DrawString(printlists[i].Week_id, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 90, 75); //操作代码 第五行 e.Graphics.DrawString("操作代码: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 95); e.Graphics.DrawString(printlists[i].Operater_code, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 90, 95); //车型 行号 第六行 e.Graphics.DrawString("车型: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 120); e.Graphics.DrawString(printlists[i].Car_model, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 60, 120); //里程数 第七行 e.Graphics.DrawString("里程数: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 140); e.Graphics.DrawString(printlists[i].Card_km, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 80, 140); e.Graphics.DrawString("故障件数量: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 170, 140); e.Graphics.DrawString(printlists[i].Exception_number, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 250, 140); //车辆识别号 第八行 e.Graphics.DrawString("车辆识别号: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 160); e.Graphics.DrawString(printlists[i].Vin, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 110, 160); //故障零件号 行号 第九行 e.Graphics.DrawString("故障零件号: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 180); e.Graphics.DrawString(printlists[i].Exception_fit_number, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 105, 180); e.Graphics.DrawString("行号: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 210, 180); e.Graphics.DrawString(printlists[i].Bank_id, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 245, 180); //故障代码 主要配件 第十行 e.Graphics.DrawString("故障代码: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 200); e.Graphics.DrawString(printlists[i].Exception_code, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 100, 200); e.Graphics.DrawString("主要配件: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 150, 200); e.Graphics.DrawString(printlists[i].Zhuyao_fit, new Font("Arial", 20, FontStyle.Regular), Brushes.Black, 220, 195); //故障名称 第十一行 e.Graphics.DrawString("故障名称: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 230); string temp_exception_name = printlists[i].Exception_name; int exception_name_length = temp_exception_name.Length; string excepton_info1 = Substring(temp_exception_name, 0, 11, 21, 22, exception_name_length); e.Graphics.DrawString(excepton_info1, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 95, 230); string excepton_info2 = Substring(temp_exception_name, excepton_info1.Length, 17, 33, 34, exception_name_length); e.Graphics.DrawString(excepton_info2, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 25, 250); string temp_exception_reason = printlists[i].Exception_reason; int exception_reason_length = temp_exception_reason.Length; e.Graphics.DrawString("故障原因及位置: ", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, 25, 280); string reason_one = Substring(temp_exception_reason, 0, 9, 17, 18, exception_reason_length); e.Graphics.DrawString(reason_one, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 140, 280); string reason_two = Substring(temp_exception_reason, reason_one.Length, 17, 33, 34, exception_reason_length); e.Graphics.DrawString(reason_two, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 25, 300); string reason_three = Substring(temp_exception_reason, reason_one.Length reason_two.Length, 17, 33, 34, exception_reason_length); e.Graphics.DrawString(reason_three, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 25, 320); string reason_four = Substring(temp_exception_reason, reason_one.Length reason_two.Length reason_three.Length, 17, 33, 34, exception_reason_length); e.Graphics.DrawString(reason_four, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 25, 340); PrintDescriptionInfo(e, 2); if (i != page_number) { e.HasMorePages = true; i ; return; } i = 0; page_number = 0; e.HasMorePages = false; } //全选 private void btn_SelectAll_Click(object sender, EventArgs e) { if (dataGridView1.RowCount > 0) { foreach (DataGridViewRow dgdr in dataGridView1.Rows) { dgdr.Cells["SelectColumn12"].Value = true; } } } private void btn_Cancel_Click(object sender, EventArgs e) { foreach (DataGridViewRow dgdr in dataGridView1.Rows) { dgdr.Cells["SelectColumn12"].Value =false; } } private void btn_Exit_Click(object sender, EventArgs e) { Application.Exit(); } private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { try { string path = AppSettings.GetConnectionStringsValue("Path"); if (dataGridView1.Rows.Count > 0) { if (IsHasPrint()) { foreach (DataGridViewRow dgdr in dataGridView1.Rows) { if (dgdr.Cells["SelectColumn12"].EditedFormattedValue.ToString() == "True") { TRQInfo trqinfo = new TRQInfo(); trqinfo.Asc_name = asc_name; trqinfo.Asc_id = asc_code; trqinfo.Week_id = dgdr.Cells["week_id"].EditedFormattedValue.ToString();//索赔周次 trqinfo.Work_id = dgdr.Cells["work_id"].EditedFormattedValue.ToString();//索赔工单号 trqinfo.Bank_id = dgdr.Cells["bank_id"].EditedFormattedValue.ToString();//行号 trqinfo.Vin = dgdr.Cells["vin"].EditedFormattedValue.ToString();//VIN码 trqinfo.Exception_fit_number = dgdr.Cells["exception_fit_id"].EditedFormattedValue.ToString();//故障零件号 trqinfo.Operater_code = dgdr.Cells["operate_code"].EditedFormattedValue.ToString();//操作代码 trqinfo.Card_km = dgdr.Cells["card_km"].EditedFormattedValue.ToString();//里程数 trqinfo.Car_model = dgdr.Cells["card_mode"].EditedFormattedValue.ToString();//车型 trqinfo.Exception_code= dgdr.Cells["exception_code"].EditedFormattedValue.ToString();//故障代码 trqinfo.Exception_number = dgdr.Cells["exception_fit_number"].EditedFormattedValue.ToString();//故障件数量 trqinfo.Exception_name = dgdr.Cells["exception_name"].EditedFormattedValue.ToString();//故障名称 trqinfo.Exception_reason = dgdr.Cells["exception_reason"].EditedFormattedValue.ToString();//故障原因 trqinfo.Zhuyao_fit = dgdr.Cells["zhuyaopeijian"].EditedFormattedValue.ToString();//主要配件 trqinfo.ImgRTQ = GetImage(trqinfo.Asc_id, trqinfo.Week_id, trqinfo.Work_id, trqinfo.Bank_id, trqinfo.Vin, trqinfo.Exception_fit_number); string temp_exception_fit_number = dgdr.Cells["exception_fit_number"].EditedFormattedValue.ToString(); int temp_number = Convert.ToInt32(temp_exception_fit_number); for (int i = 0; i < temp_number; i ) { printlists.Add(trqinfo); } WareHouse.Update(trqinfo.Week_id,trqinfo.Work_id,trqinfo.Bank_id,trqinfo.Vin,trqinfo.Exception_fit_number, "Y", temp_Path); dgdr.Cells["Column17"].Value = "已打印"; dgdr.DefaultCellStyle.BackColor = Color.RosyBrown; dgdr.Cells["SelectColumn12"].Value = false; } } } else { MessageBox.Show("请选择要打印的标签!"); } } else { MessageBox.Show("请先导入数据!然后选择打印!"); } } catch (Exception ex) { MessageBox.Show("删除失败!" ex.Message); } } private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { printlists.Clear(); } } }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论