異なるPDFファイルにそれぞれのワークシートを保存する
Contents
[
Hide
]
Aspose.Cellsは、(画像、グラフなどを含む)スプレッドシートファイルをPDFドキュメントに変換することをサポートしています。Aspose.Cells for JavaはスプレッドシートをPDFドキュメントに変換するためにAspose.PDF for Javaを使用する必要はなく、独立して機能します。変換には一時ファイルを作成したり使用したりする必要もありません。全体のプロセスはメモリ内で行うことができます。
テンプレートのExcelファイル内の各ワークシートを異なるPDFファイルに保存する必要がある場合、これは簡単に実現できます。PdfSaveOptions.SheetSetオプションを使用して、1つのシートインデックスを1回に設定して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に正しい値がレンダリングされます。