在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文章向大家介绍C# 调用打印机 打印 Excel,主要包括C# 调用打印机 打印 Excel使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。 打印 Excel 模板 大体思路,通过NPOI操作Excel文件,通过Spire将Excel转成图片,将图片传给系统打印。
Spire是收费工具,在微软库中下载Free版本。
#region 打印所用到的函数 private void btnPrint_Click(object sender, EventArgs e) { if (!pnlRes.Visible) { MessageBox.Show("请先测量数据!", "提示"); return; } //未测量无法打印 try { Settings.PrintDataRead(); if (File.Exists(Settings.path_printExcel)) { File.Copy(Settings.path_printExcel, Settings.path_printExcelCopy, true);//三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换 } writeExcel(); prtDoc.PrinterSettings.PrinterName = Settings.PrintData.PrinterName; //设置打印机名称 System.Drawing.Printing.PaperSize ps = new System.Drawing.Printing.PaperSize(); //ps.RawKind = 150; ps.Height = Settings.PrintData.PaperSizeHeight - 5; ps.Width = Settings.PrintData.PaperSizeWidth + 2; //ps.PaperName = "default"; prtDoc.DefaultPageSettings.PaperSize = ps; excelToImage(Settings.path_printExcelCopy); prtDoc.Print(); cleanAll(); File.Delete(Settings.path_printExcelCopy); } catch (Exception ex) { LogHelper.Error(ex.GetType(), ex); MessageBox.Show(ex.Message, "错误"); File.Delete(Settings.path_printExcelCopy); } } //写入信息到Excel private void writeExcel() { try { string tempPath = Settings.path_printExcelCopy; //Excel对象 XSSFWorkbook wk = null; using (FileStream fs = File.Open(tempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { wk = new XSSFWorkbook(fs); fs.Close(); } //Excel页面 设置->Sheet1 ISheet sheet = wk.GetSheet("Sheet1");
/* 写入操作 依据需求自己编写 for (int j = 0; j < chList.Length; ++j) { if ((string)chList[j].ckb.Tag != "0") { int cell = (j + 1) * 3 - 2; //写入通道 sheet.GetRow(1).GetCell(cell).SetCellValue("CH" + (j + 1));
//写入OK / NG Tag 1,OK 2NG if ((string)chList[j].ckb.Tag == "1") { sheet.GetRow(1).GetCell(cell + 1).SetCellValue("OK"); } else { sheet.GetRow(1).GetCell(cell + 1).SetCellValue("NG"); } //写入机型 时间 sheet.GetRow(2).GetCell(cell).SetCellValue("机型:" + Settings.Model.deviceName);
sheet.GetRow(3).GetCell(cell).SetCellValue(DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
for (int i = 0; i < chList[j].lblList.Length; ++i) { switch (i) { case 0: sheet.GetRow(4).GetCell(cell).SetCellValue("流量:" + chList[j].lblList[i].Text); break; case 1: sheet.GetRow(5).GetCell(cell).SetCellValue("出口压力:" + chList[j].lblList[i].Text); break; case 2: sheet.GetRow(6).GetCell(cell).SetCellValue("浓度:" + chList[j].lblList[i].Text); break; case 3: sheet.GetRow(7).GetCell(cell).SetCellValue("雾化压力:" + chList[j].lblList[i].Text); break; } } } else {
} } */ using (var fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write)) { wk.Write(fs); } } catch (Exception ex) { LogHelper.Error(ex.GetType(), ex); MessageBox.Show(ex.Message, "错误"); } } private void excelToImage(string filename) { try { Workbook workbook = new Workbook(); workbook.LoadFromFile(filename); Worksheet sheet = workbook.Worksheets[0]; sheet.SaveToImage(Settings.path_printImage); //图片后缀.bmp ,imagepath自己设置 } catch (Exception ex) { LogHelper.Error(ex.GetType(), ex); MessageBox.Show(ex.Message, "错误"); } }
private void prtDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { #region 如果不需要截取图片,可以不用写以下代码 //截取左上角宽度 int rectangleWidth = Settings.PrintData.MarginWidth + 72; int rectangleHight = Settings.PrintData.MarginHeight + 96; try { GC.Collect(); Graphics g = e.Graphics; //imagepath是指 excel转成的图片的路径 using (Bitmap bitmap = new Bitmap(Settings.path_printImage)) { Rectangle newarea = new Rectangle(); newarea.X = rectangleWidth; newarea.Y = rectangleHight; newarea.Width = bitmap.Width - rectangleWidth; newarea.Height = bitmap.Height - rectangleHight; using (Bitmap newbitmap = bitmap.Clone(newarea, bitmap.PixelFormat)) { //打印 g.DrawImage(newbitmap, 0, 0, newbitmap.Width, newbitmap.Height); } } } catch (Exception ex) { LogHelper.Error(ex.GetType(), ex); MessageBox.Show(ex.Message, "错误"); } #endregion }
#endregion http://www.manongjc.com/article/90422.html
|
请发表评论