Spara specificerade arbetsblad till PDF med C++

Som standard sparar Aspose.Cells alla synliga arbetsblad i en arbetsbok till en PDF-fil. Med PdfSaveOptions.GetSheetSet()-alternativet kan du spara angivna arbetsblad till en PDF-fil. till exempel kan du spara det aktiva arbetsbladet till PDF, spara alla arbetsblad (både synliga och dolda arbetsblad) till PDF, eller spara flera anpassade arbetsblad till PDF.

Spara aktivt arkpapp till PDF

Om du bara vill exportera det aktiva bladet till PDF kan du uppnå detta genom att ange SheetSet.GetActive() till PdfSaveOptions.GetSheetSet()-alternativet.

Arbetsbladet Sheet2 är det aktiva bladet i källfilen 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();
}

Spara alla arbetsblad till PDF

SheetSet.GetVisible() visar synliga blad i en arbetsbok, och SheetSet.GetAll() visar alla blad inklusive både synliga och dolda/blindade blad i en arbetsbok. Om du vill exportera alla blad till PDF kan du bara ange SheetSet.GetAll till PdfSaveOptions.GetSheetSet()-alternativet.

Källfilen sheetset-example.xlsx innehåller alla fyra ark med dolt ark Ark3.

#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();
}

Spara angivna arbetsblad som PDF

Om du vill exportera önskat/anpassat flera blad till PDF kan du göra detta genom att ange flera bladindextal till PdfSaveOptions.GetSheetSet()-alternativet.

#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;
}

Omordna arbetsblad till PDF

Om du vill omordna blad (t.ex. i omvänd ordning) till PDF utan att ändra källfilen kan du göra detta genom att ange omordnade bladindextal till PdfSaveOptions.GetSheetSet()-alternativet.

#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();
}