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()