spire.xls组件支持打印excel 格式文档。本文将详细介绍下面三种打印方式,使用c# 打印excel文档。
- 使用默认打印机打印excel文档
- 指定打印机及设置打印选项
- 静默打印excel文档
使用默认打印机打印excel 文档,直接调用workbook.printdocument.print()方法完成打印
c#
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
workbook.printdocument.print();
vb.net
dim workbook as new workbook()
workbook.loadfromfile("sample.xlsx")
workbook.printdocument.print()
指定打印机及设置打印选项
microsoft中,用户可以通过页面设置,对打印方向,纸张大小,打印区域进行设置:
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
//获取第一个excel工作表
worksheet sheet = workbook.worksheets[0];
//设置打印纸张大小
sheet.pagesetup.papersize = papersizetype.papera4;
//设置打印方向
sheet.pagesetup.orientation = pageorientationtype.portrait;
//设置打印区域
sheet.pagesetup.printarea = "b2:f8";
//打印标题
sheet.pagesetup.printtitlecolumns = "$a:$b";
sheet.pagesetup.printtitlerows = "$1:$2";
//打印顺序
sheet.pagesetup.order = ordertype.downthenover;
sheet.pagesetup.order = ordertype.overthendown;
//设置打印对话框属性
printdialog dialog = new printdialog();
dialog.allowprinttofile = true;
dialog.allowcurrentpage = true;
dialog.allowsomepages = true;
//设置单面打印
dialog.printersettings.duplex = duplex.simplex;
//设置打印页面范围
dialog.printersettings.frompage = 0;
dialog.printersettings.topage = 8;
dialog.printersettings.printrange = printrange.somepages;
//设置打印份数
dialog.printersettings.copies = 5;
//设置打印机名称
dialog.printersettings.printername = "hp lasterjet p1007";
//打印文档
workbook.printdialog = dialog;
printdocument pd = workbook.printdocument;
if
(dialog.showdialog() == dialogresult.ok)
{
pd.print();
}
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("sample.xlsx")
'获取第一个excel工作表
dim sheet as worksheet = workbook.worksheets(0)
'设置打印纸张大小
sheet.pagesetup.papersize = papersizetype.papera4
'设置打印方向
sheet.pagesetup.orientation = pageorientationtype.portrait
'设置打印区域
sheet.pagesetup.printarea = "b2:f8"
'打印标题
sheet.pagesetup.printtitlecolumns = "$a:$b"
sheet.pagesetup.printtitlerows = "$1:$2"
'打印顺序
sheet.pagesetup.order = ordertype.downthenover
sheet.pagesetup.order = ordertype.overthendown
'设置打印对话框属性
dim dialog as new printdialog()
dialog.allowprinttofile = true
dialog.allowcurrentpage = true
dialog.allowsomepages = true
'设置单面打印
dialog.printersettings.duplex = duplex.simplex
'设置打印页面范围
dialog.printersettings.frompage = 0
dialog.printersettings.topage = 8
dialog.printersettings.printrange = printrange.somepages
'设置打印份数
dialog.printersettings.copies = 5
'设置打印机名称
dialog.printersettings.printername = "hp lasterjet p1007"
'打印文档
workbook.printdialog = dialog
dim pd as printdocument = workbook.printdocument
if dialog.showdialog() = dialogresult.ok then
pd.print()
end if
静默打印
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
//静默打印excel文档
workbook.printdocument.printcontroller = new standardprintcontroller();
workbook.printdocument.print();
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("sample.xlsx")
'静默打印excel文档
workbook.printdocument.printcontroller = new standardprintcontroller()
workbook.printdocument.print()