将每个工作表保存到不同的 PDF 文件
Contents
[
Hide
]
Aspose.Cells 支持将电子表格文件(包含图像、图表等)转换为 PDF 文档。 Aspose.Cells for Java 可以独立工作,将电子表格转换为 PDF 文档,您不再需要使用 Aspose.PDF for Java 进行转换。转换也不需要创建/使用任何临时文件,因为整个过程可以在内存中完成。
如果您需要将每个工作表保存在您的模板 Excel 文件中以生成不同的 PDF 文件。这很容易实现。您可以尝试隐藏文件中的工作表,并根据渲染 PDF 的方式一次显示一张工作表。
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(SaveEachWorksheettoDifferentPDF.class); | |
// Instantiate a new workbook and open the Excel | |
// File from its location | |
Workbook workbook = new Workbook(dataDir + "input.xlsx"); | |
// Get the count of the worksheets in the workbook | |
int sheetCount = workbook.getWorksheets().getCount(); | |
// Define PdfSaveOptions | |
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
// Take Pdfs of each sheet | |
for (int j = 0; j < sheetCount; j++) { | |
Worksheet ws = workbook.getWorksheets().get(j); | |
//set worksheet to output | |
SheetSet sheetSet = new SheetSet(new int[] { ws.getIndex() }); | |
pdfSaveOptions.setSheetSet(sheetSet); | |
workbook.save(dataDir + "_" + ws.getName() + ".pdf", pdfSaveOptions); | |
} |
如果电子表格包含公式,最好调用[工作簿.计算公式](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#calculateFormula()方法,就在将电子表格呈现为 PDF 之前。这可确保重新计算公式相关值,并在 PDF 中呈现正确的值。