Spire.xls+excel文件实现单据打印

报表和单据打印,通常都是使用fastreport之类的,因为有了现成的xls模板样式,如果转成fastreport那还需要花时间,是用spire.xls这个玩意简单,超好用。

一.引用

using Spire.Xls;

二.基本的操作

// 创建工作簿,读写,保存,保存

private void save_Click(object sender, EventArgs e)

{

Workbook workbook = new Workbook();

workbook.LoadFromFile(Application.StartupPath + "\\sample.xlsx");

// 获取第一个工作表

Worksheet sheet = workbook.Worksheets0;

// 读取数据

string value1 = sheet.Range"A1".Value.ToString();

string value2 = sheet.Range"B1".Value.ToString();

// 写入数据

sheet.Range"A1".Value = "Hello"; //方式一

sheet.Range2,2.Value = "World"; //方式二

//保存文件

workbook.SaveToFile("c:\\example.xlsx", ExcelVersion.Version2013);

}
三.打印

使用对话框模式输出,不知道原因是什么,怎么选择打印机都是从默认打印机输出。

myxls.LoadFromFile(Application.StartupPath + "\\sample.xlsx");

sheet = myxls.Worksheets0;

sheet.PageSetup.PrintArea = "A1:H20";

PrintDialog dialog = new PrintDialog();

dialog.PrinterSettings.PrintRange = System.Drawing.Printing.PrintRange.SomePages;

dialog.PrinterSettings.FromPage = 0;

dialog.AllowPrintToFile = true;

dialog.AllowCurrentPage = true;

dialog.AllowSomePages = true;

myxls.PrintDialog = dialog;

myxls.PrintDocument.PrinterSettings.Copies = 1;

if (dialog.ShowDialog() == DialogResult.OK)

{

myxls.PrintDocument.Print();

}

找不出原因,归结为可能是免费版本限制功能的原因。也不深究,换一个方式,在页面上增加了一个下拉框,窗体加载时将打印机列出来供选择。

using System.Drawing.Printing;

foreach (var item in System.Drawing.Printing.PrinterSettings.InstalledPrinters)

{

this.con_printer.Items.Add(item);

}

this.con_printer.Text= new System.Drawing.Printing.PrintDocument().PrinterSettings.PrinterName;//默认打印机

调用打印的时候,直接指定打印机名称,曲线救国成功。

myxls.PrintDocument.PrintController = new StandardPrintController();

PrinterSettings settings = myxls.PrintDocument.PrinterSettings;

settings.PrinterName = this.con_printer.Text;

settings.Duplex = Duplex.Simplex;

settings.FromPage = 1;

settings.ToPage = 1;

myxls.PrintDocument.Print();

四.插入图片

需要在打印页面插入二维码, 这个先用zxing产生图片,在指定位置插入图片

ExcelPicture mypic = sheet.Pictures.Add(1, 12, Generate3(barcode, 300, 300)); //在第1行12列插入

mypic.Width = 90;

mypic.Height = 90;

mypic.LeftColumnOffset = 75;

mypic.TopRowOffset = 20;

丝滑。

相关推荐
思麟呀14 小时前
在C++基础上理解CSharp-5
开发语言·c++·c#
z落落17 小时前
C#ToolStrip+StatusStrip 状态栏实时显示系统时间+NotifyIcon系统托盘
开发语言·c#
ctrl_v助手18 小时前
VisionPro (R) QuickBuild相机的连接
服务器·笔记·数码相机·c#
北域码匠20 小时前
奇偶归并排序:并行计算的排序利器
数据结构·算法·c#·排序算法
zhangfeng113320 小时前
国家超算中心 昆山站 异构加速卡1 显存16GB详细配置, 海光 Z100SM HCU
linux·网络·深度学习·c#
z落落20 小时前
C# WinForm TreeView 树形控件+ListView控件+菜单栏
开发语言·c#
ABprogramming21 小时前
Aspire入门指南
c#·.net
加号321 小时前
【C#】VS2022 传统 ASP.NET Web 服务(.asmx)接口实现指南
前端·c#·asp.net
加号31 天前
【C#】 文件与目录管理:创建、删除操作的技术解析
开发语言·c#
用户395240998802 天前
SqlSugar 连接 PostgreSQL 报错 42P01: relation does not exist 的排查与修复
c#