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.Worksheets[0];

// 读取数据

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

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

// 写入数据

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

sheet.Range[2,2].Value = "World"; //方式二

//保存文件

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

}
三.打印

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

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

sheet = myxls.Worksheets[0];

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;

丝滑。

相关推荐
极小狐11 小时前
极狐GitLab 通用软件包存储库功能介绍
java·数据库·c#·gitlab·maven
钢铁男儿11 小时前
C# 方法(可选参数)
数据库·mysql·c#
yuanpan12 小时前
.net/C#进程间通信技术方案总结
开发语言·c#·.net
IM1GENIUS15 小时前
.NET高频技术点(持续更新中)
c#·.net
red-fly15 小时前
c#修改ComboBox当前选中项的文本
c#·combobox
bicijinlian19 小时前
.Net HttpClient 概述
c#·.net·httpclient·.net httpclient
码观天工19 小时前
.NET 原生驾驭 AI 新基建实战系列(七):Weaviate ── 语义搜索的智能引擎创新者
ai·c#·.net·向量数据库·weaviate
Zhen (Evan) Wang20 小时前
.NET 8 + Angular WebSocket 高并发性能优化
c#·.net·angular
chenyuhao202420 小时前
链表面试题7之相交链表
数据结构·算法·链表·面试·c#
菜鸟分享录21 小时前
MCP 入门实战:用 C# 开启 AI 新篇章
ai·c#·semantic kernel·mcp