Salva fogli specifici in PDF con Node.js tramite C++

Per impostazione predefinita, Aspose.Cells salva tutti i fogli visibili di un workbook in un file PDF. Con l’opzione PdfSaveOptions.getSheetSet(), puoi salvare fogli specifici in un file PDF. Ad esempio, puoi salvare il foglio attivo in PDF, salvare tutti i fogli (sia visibili che nascosti) in PDF, o salvare più fogli personalizzati in PDF.

Salva il foglio di lavoro attivo in PDF

Se vuoi esportare solo il foglio attivo in PDF, puoi farlo passando SheetSet.getActive() all’opzione PdfSaveOptions.getSheetSet().

Il foglio Sheet2 è il foglio attivo del file di origine sheetset-example.xlsx.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sheetset-example.xlsx");

// Open the template excel file
const workbook = new AsposeCells.Workbook(filePath);

// Set active sheet to output
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();
pdfSaveOptions.setSheetSet(AsposeCells.SheetSet.getActive());

// Save the pdf file with PdfSaveOptions
workbook.save("output.pdf", pdfSaveOptions);

Salva tutti i fogli in PDF

SheetSet.getVisible() indica i fogli visibili in un workbook, e SheetSet.getAll() indica tutti i fogli, inclusi quelli visibili e nascosti/invisibili. Se vuoi esportare tutti i fogli in PDF, puoi semplicemente passare SheetSet.getAll() all’opzione PdfSaveOptions.getSheetSet().

Il file di origine sheetset-example.xlsx contiene tutti e quattro i fogli con il foglio nascosto Sheet3.

const AsposeCells = require("aspose.cells.node");
const path = require("path");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sheetset-example.xlsx");

// Open the template excel file
const workbook = new AsposeCells.Workbook(filePath);

// Set all sheets to output
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();
pdfSaveOptions.setSheetSet(AsposeCells.SheetSet.getAll());

// Save the pdf file with PdfSaveOptions
workbook.save("output.pdf", pdfSaveOptions);

Salva fogli specificati in PDF

Se vuoi esportare più fogli desiderati/personalizzati in PDF, puoi farlo passando più indici di fogli all’opzione PdfSaveOptions.getSheetSet().

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sheetset-example.xlsx");

// Open the template excel file
const workbook = new AsposeCells.Workbook(filePath);

// Set custom multiple sheets(Sheet1, Sheet3) to output
const sheetSet = new AsposeCells.SheetSet([0, 2]);
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();
pdfSaveOptions.setSheetSet(sheetSet);

// Save the pdf file with PdfSaveOptions
workbook.save("output.pdf", pdfSaveOptions);

Riordina i fogli di lavoro in PDF

Se desideri riordinare i fogli (es. in ordine inverso) in PDF senza modificare il file di origine, puoi farlo passando gli indici di fogli riordinati all’opzione PdfSaveOptions.getSheetSet().

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sheetset-example.xlsx");
// Open the template excel file
const wb = new AsposeCells.Workbook(filePath);

// Reorder sheets(Sheet1, Sheet2, Sheet3, Sheet4) to sheets(Sheet4, Sheet3, Sheet2, Sheet1)
const sheetSet = new AsposeCells.SheetSet([3, 2, 1, 0]);
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();
pdfSaveOptions.setSheetSet(sheetSet);

// Save the pdf file with PdfSaveOptions
wb.save("output.pdf", pdfSaveOptions);