Guardar las hojas específicas en PDF con Node.js vía C++

Por defecto, Aspose.Cells guarda todas las hojas visibles de un libro en un archivo PDF. Con la opción PdfSaveOptions.getSheetSet(), puedes guardar hojas específicas en un archivo PDF. Por ejemplo, puedes guardar la hoja activa en PDF, guardar todas las hojas (tanto visibles como ocultas) en PDF, guardar varias hojas personalizadas en PDF.

Guardar Hoja de Cálculo Activa en PDF

Si solo quieres exportar la hoja activa a PDF, puedes lograrlo pasando SheetSet.getActive() a la opción PdfSaveOptions.getSheetSet().

La hoja Sheet2 es la hoja activa del archivo fuente 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);

Guardar todas las hojas en PDF

SheetSet.getVisible() indica hojas visibles en un libro, y SheetSet.getAll() indica todas las hojas, incluyendo las hojas visibles e invisibles en un libro. Si desea exportar todas las hojas a PDF, simplemente pase la opción de SheetSet.getAll() a PdfSaveOptions.getSheetSet().

El archivo fuente ejemplo-de-conjunto-de-hojas.xlsx contiene las cuatro hojas con la hoja oculta ‘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);

Guardar hojas de cálculo especificadas en PDF

Si deseas exportar varias hojas deseadas/personalizadas a PDF, puedes lograrlo pasando múltiples índices de hoja a la opción 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);

Reordenar hojas de cálculo a PDF

Si desea reordenar las hojas (por ejemplo, en orden inverso) a PDF sin modificar el archivo fuente, puede lograrlo pasando los índices de hoja reordenados a la opción 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);