打印工作簿

使用情景

完成创建电子表格后,您可能希望打印工作表的硬拷贝以备份。在打印时,MS Excel假定您要打印整个工作表区域,除非您指定所需的选择。以下截图显示了在Excel中打印工作簿的对话框。

todo:image_alt_text

图表: 打印对话框

使用 Aspose.Cells 打印工作簿

Aspose.Cells for Java提供了SheetRender类的toPrinter方法。通过使用SheetRender.toPrinter方法,您可以提供打印机名称以及打印作业名称。

示例代码

打印所选工作表

以下代码片段演示了使用SheetRender.toPrinter方法打印所选工作表的方法。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(PrintingSelectedWorksheet.class);
// Instantiate a new workbook
Workbook book = new Workbook(dataDir + "Book1.xls");
// Create an object for ImageOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Create a SheetRender object with respect to your desired sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
// Print the worksheet
sr.toPrinter(strPrinterName);

打印整个工作簿

您还可以使用WorkbookRender.toPrinter方法打印整个工作簿。以下代码片段演示了使用WorkbookRender.toPrinter方法打印整个工作簿的方法。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(PrintingWholeWorkbook.class);
// Instantiate a new workbook
Workbook book = new Workbook(dataDir + "Book1.xls");
// Create an object for ImageOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// WorkbookRender only support TIFF file format
imgOptions.setImageFormat(ImageFormat.getTiff());
// Create a WorkbookRender object with respect to your workbook
WorkbookRender wr = new WorkbookRender(book, imgOptions);
// Print the workbook
wr.toPrinter(strPrinterName);

相关文章