将每个工作表保存为不同的PDF文件
Contents
[
Hide
]
Aspose.Cells支持将包含图像、图表等的电子表格文件转换为PDF文档。 Aspose.Cells for Java 可独立工作以将电子表格转换为PDF文档,您不需要再使用Aspose.PDF for Java进行转换,转换不需要创建/使用任何临时文件,因为整个过程可以在内存中完成。
如果您需要保存模板 Excel 文件中的每个工作表以生成不同的 PDF 文件,这可以很容易地实现。您可以尝试一次将一个工作表索引设置为 PdfSaveOptions.SheetSet 选项,以生成 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); | |
} |
如果电子表格中包含公式,最好在将电子表格渲染为 PDF 之前调用 Workbook.calculateFormula() 方法。这将确保重新计算公式依赖的值,并在 PDF 中呈现正确的值。