打印工作簿
Contents
[
Hide
]
本文档旨在让开发人员了解(以紧凑的方式)如何打印电子表格。
使用场景
完成电子表格创建后,您可能需要根据需要打印一份工作表的硬拷贝。打印时,MS Excel 假定您要打印整个工作表区域,除非您指定了您的选择。以下屏幕截图显示了使用 Excel 打印工作簿的对话框。
**数字:**打印对话框
使用 Aspose.Cells 打印工作簿
Aspose.Cells for Java 提供了一个[到打印机](https://reference.aspose.com/cells/java/com.aspose.cells/sheetrender#toPrinter(java.lang.String) 的方法图纸渲染班级。通过使用SheetRender.toPrinter 方法,您可以提供打印机名称以及打印作业名称。
示例代码
打印选定的工作表
下面的代码片段演示了使用[SheetRender.toPrinter](https://reference.aspose.com/cells/java/com.aspose.cells/sheetrender#toPrinter(java.lang.String)方法来打印您选择的工作表。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
打印整个工作簿
您还可以使用[工作簿渲染到打印机](https://reference.aspose.com/cells/java/com.aspose.cells/workbookrender#toPrinter(java.lang.String) 方法来打印整个工作簿。下面的代码片段演示了使用[工作簿渲染到打印机](https://reference.aspose.com/cells/java/com.aspose.cells/workbookrender#toPrinter(java.lang.String)方法来打印整个工作簿。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |