本条经验主要介绍以下三种使用Spire.XLS组件打印Excel文档的方式:1.使用默认打印机打印Excel文档2.指定打印机及设置打印选项3.静默打印Excel文档
工具/原料
Spire.XLS for .Net
Visual Studio
dll包导入:
1、下载并安装后,注意在程序中添加引用Spire.XLS.dll(dll可在安装路径下的bin文件夹中获取),如下图:
使用默认打印机 及代码示例1:
1、使用默认打印机打印Excel 文档,直接调用workbook.PrintDocument.Print()方法完成打印:
2、Workbook workbook = new Workbook();workbook.LoadFromFile("Sample.xlsx");workbook.PrintDocument.Print();
指定打印机及设置打印选项及代码示例2::
1、Microsoft中,用户可以通过页面设置,对打印方向,纸张大小,打印区域进行设置:
2、//加载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();}
静默打印代码示例3:
1、//加载Excel文档Workbook workbook = new Workbook();workbook.LoadFromFile烫喇霰嘴("Sample.xlsx");//静默打印Excel文档workbook.PrintDocument.PrintController = new StandardPrintController();workbook.PrintDocument.Print();