页面设置和打印选项
Contents
[
Hide
]
有时,开发人员需要配置页面设置和打印设置来控制打印过程。页面设置和打印设置提供各种选项,并在 Aspose.Cells 中得到完全支持。
本文介绍如何使用 Aspose.Cells API 通过几行简单的代码创建控制台应用程序并将页面设置和打印选项应用于工作表。
使用页面和打印设置
对于此示例,我们在 Microsoft Excel 中创建了一个工作簿,并使用 Aspose.Cells 设置页面设置和打印选项。
设置页面设置选项
首先在 Microsoft Excel 中创建一个简单的工作表。然后对其应用页面设置选项。执行代码会更改页面设置选项,如下面的屏幕截图所示。
输出文件
-
在 Microsoft Excel 中创建包含一些数据的工作表: 1.在Microsoft Excel中打开一个新的工作簿。 1.添加一些数据。 下面是该文件的屏幕截图。
输入文件
-
设置页面设置选项: 将页面设置选项应用于文件。下面是默认选项的屏幕截图,在应用新选项之前。
默认页面设置选项
- 下载并安装 Aspose.Cells:
- 下载 Aspose.Cells for Java.
- 在您的开发计算机上解压它。 全部Aspose组件在安装后以评估模式工作。评估模式没有时间限制,它只在生成的文档中注入水印。
- 创建一个项目。 使用 Java 编辑器(例如 Eclipse)创建一个项目,或者使用文本编辑器创建一个简单的程序。
- 添加类路径。
- 从 Aspose.Cells.zip 中提取 Aspose.Cells.jar 和 dom4j_1.6.1.jar。 1、在Eclipse中设置项目的类路径: 1.在Eclipse中选择你的项目然后点击项目其次是特性. 1.选择Java 构建路径在对话框的左侧。
- 选择库选项卡,单击添加 JAR要么添加外部 JAR选择 Aspose.Cells.jar 和 dom4j_1.6.1.jar 并将它们添加到构建路径中。 或者您可以在运行时在 DOS 提示符下在 Windows 中设置它:
javac \-classpath %classpath%;e:\Aspose.Cells.jar; ClassName .javajava \-classpath %classpath%;e:\Aspose.Cells.jar; ClassName
- 编写调用 API 的应用程序: 以下是此示例中组件使用的代码。
This file contains 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(SettingPageSetupOptions.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "CustomerReport.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
PageSetup pageSetup = sheet.getPageSetup(); | |
// Setting the orientation to Portrait | |
pageSetup.setOrientation(PageOrientationType.PORTRAIT); | |
// Setting the scaling factor to 100 | |
// pageSetup.setZoom(100); | |
// OR Alternately you can use Fit to Page Options as under | |
// Setting the number of pages to which the length of the worksheet will be spanned | |
pageSetup.setFitToPagesTall(1); | |
// Setting the number of pages to which the width of the worksheet will be spanned | |
pageSetup.setFitToPagesWide(1); | |
// Setting the paper size to A4 | |
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4); | |
// Setting the print quality of the worksheet to 1200 dpi | |
pageSetup.setPrintQuality(1200); | |
// Setting the first page number of the worksheet pages | |
pageSetup.setFirstPageNumber(2); | |
// Save the workbook | |
workbook.save(dataDir + "PageSetup.xls"); |
设置打印选项
页面设置设置还提供了几个打印选项(也称为工作表选项),允许用户控制工作表页面的打印方式。它们允许用户:
- 选择工作表的特定打印区域。
- 打印标题。
- 打印网格线。
- 打印行/列标题。
- 达到草稿质量。
- 打印评论。
- 打印单元错误。
- 定义页面排序。
下面的示例将打印选项应用于在上例中创建的文件 (PageSetup.xls)。下面的屏幕截图显示了应用新选项之前的默认打印选项。 输入文档
执行代码会更改打印选项。 输出文件
This file contains 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(SettingPrintoptions.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "PageSetup.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
PageSetup pageSetup = sheet.getPageSetup(); | |
// Specifying the cells range (from A1 cell to E30 cell) of the print area | |
pageSetup.setPrintArea("A1:E30"); | |
// Defining column numbers A & E as title columns | |
pageSetup.setPrintTitleColumns("$A:$E"); | |
// Defining row numbers 1 & 2 as title rows | |
pageSetup.setPrintTitleRows("$1:$2"); | |
// Allowing to print gridlines | |
pageSetup.setPrintGridlines(true); | |
// Allowing to print row/column headings | |
pageSetup.setPrintHeadings(true); | |
// Allowing to print worksheet in black & white mode | |
pageSetup.setBlackAndWhite(true); | |
// Allowing to print comments as displayed on worksheet | |
pageSetup.setPrintComments(PrintCommentsType.PRINT_IN_PLACE); | |
// Allowing to print worksheet with draft quality | |
pageSetup.setPrintDraft(true); | |
// Allowing to print cell errors as N/A | |
pageSetup.setPrintErrors(PrintErrorsType.PRINT_ERRORS_NA); | |
// Setting the printing order of the pages to over then down | |
pageSetup.setOrder(PrintOrderType.OVER_THEN_DOWN); | |
// Save the workbook | |
workbook.save(dataDir + "PageSetup_Print.xls"); |
概括
本文介绍如何使用 Aspose.Cells 设置页面设置和工作表打印选项。希望它能给您一些见解,并且您可以在自己的场景中使用这些选项。
Aspose.Cells 得益于多年的研究、设计和精心调整。我们热忱欢迎您的查询、意见和建议Aspose.Cells论坛.我们保证及时回复。