حفظ أوراق عمل محددة كملف PDF باستخدام C++

بشكل افتراضي، يحفظ Aspose.Cells جميع أوراق العمل المرئية في دفتر العمل كملف PDF. مع خيار PdfSaveOptions.GetSheetSet()، يمكنك حفظ أوراق عمل محددة إلى ملف PDF. على سبيل المثال، يمكنك حفظ ورقة العمل النشطة إلى PDF، أو حفظ جميع الأوراق (المرئية وغير المرئية) إلى PDF، أو حفظ عدة أوراق مخصصة إلى PDF.

حفظ الورقة العمل النشطة إلى PDF

إذا كنت تريد تصدير الورقة النشطة فقط إلى PDF، يمكنك تحقيق ذلك عن طريق تمرير SheetSet.GetActive() إلى خيار PdfSaveOptions.GetSheetSet().

ورقة Sheet2 هي الورقة النشطة في ملف المصدر 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();
}

حفظ جميع أوراق العمل إلى PDF

يمثل SheetSet.GetVisible() الأوراق المرئية في دفتر العمل، ويمثل SheetSet.GetAll() جميع الأوراق بما في ذلك الأوراق المرئية وغير المرئية في دفتر العمل. إذا كنت تريد تصدير جميع الأوراق إلى PDF، يمكنك فقط تمرير SheetSet.GetAll إلى خيار PdfSaveOptions.GetSheetSet().

يحتوي ملف المصدر sheetset-example.xlsx على جميع الأوراق الأربع مع الورقة المخفية 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();
}

حفظ ورقات العمل المحددة في ملف PDF

إذا كنت تريد تصدير أوراق متعددة مرغوبة/مخصصة إلى PDF، يمكنك تحقيق ذلك بتمرير فهارس أوراق متعددة إلى خيار 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;
}

إعادة ترتيب أوراق العمل إلى PDF

إذا كنت تريد إعادة ترتيب الأوراق (مثل الترتيب العكسي) إلى PDF دون تعديل ملف المصدر، يمكنك تحقيق ذلك بتمرير فهارس أوراق مرتبة سابقًا إلى خيار 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();
}