Guardar hojas de cálculo específicas en PDF con C++
Por defecto, Aspose.Cells guarda todas las hojas de cálculo visibles en 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, o guardar múltiples 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.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Open the template excel file
Workbook workbook(u"sheetset-example.xlsx");
// Set active sheet to output
PdfSaveOptions pdfSaveOptions;
pdfSaveOptions.SetSheetSet(SheetSet::GetActive());
// Save the pdf file with PdfSaveOptions
workbook.Save(u"output.pdf", pdfSaveOptions);
std::cout << "PDF file saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
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 y ocultas. Si deseas exportar todas las hojas a PDF, simplemente pasa SheetSet.GetAll a la opción PdfSaveOptions.GetSheetSet().
El archivo fuente ejemplo-de-conjunto-de-hojas.xlsx contiene las cuatro hojas con la hoja oculta ‘Sheet3’.
#include <iostream>
#include "Aspose.Cells.h"
#include "Aspose.Cells/PdfSaveOptions.h"
#include "Aspose.Cells/SheetSet.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Open the template Excel file
Workbook workbook(u"sheetset-example.xlsx");
// Set all sheets to output
PdfSaveOptions pdfSaveOptions;
pdfSaveOptions.SetSheetSet(SheetSet::GetAll());
// Save the PDF file with PdfSaveOptions
workbook.Save(u"output.pdf", pdfSaveOptions);
std::cout << "PDF file generated successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
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().
#include <iostream>
#include "Aspose.Cells.h"
#include "Aspose.Cells/PdfSaveOptions.h"
#include "Aspose.Cells/SheetSet.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Open the template Excel file
U16String inputFilePath(u"sheetset-example.xlsx");
Workbook workbook(inputFilePath);
// Set custom multiple sheets (Sheet1, Sheet3) to output
Vector<int32_t> sheetIndexes = {0, 2};
SheetSet sheetSet(sheetIndexes);
// Initialize PDF save options
PdfSaveOptions pdfSaveOptions;
pdfSaveOptions.SetSheetSet(sheetSet);
// Save the PDF file with PdfSaveOptions
U16String outputFilePath(u"output.pdf");
workbook.Save(outputFilePath, pdfSaveOptions);
std::cout << "Excel file saved as PDF successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
Reordenar hojas de cálculo a PDF
Si deseas reordenar las hojas (por ejemplo, en orden inverso) a PDF sin modificar el archivo fuente, puedes lograrlo pasando los índices de hojas reordenados a la opción PdfSaveOptions.GetSheetSet().
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Open the template excel file
Workbook workbook(u"sheetset-example.xlsx");
// Reorder sheets (Sheet1, Sheet2, Sheet3, Sheet4) to (Sheet4, Sheet3, Sheet2, Sheet1)
Vector<int32_t> sheetIndexes = { 3, 2, 1, 0 };
SheetSet sheetSet(sheetIndexes);
// Create PdfSaveOptions and assign the sheet set
PdfSaveOptions pdfSaveOptions;
pdfSaveOptions.SetSheetSet(sheetSet);
// Save the pdf file with PdfSaveOptions
workbook.Save(u"output.pdf", pdfSaveOptions);
std::cout << "PDF saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}