使用C++将XLSX文件转换为PDF格式

将Excel转换为PDF

这个示例使用Excel文件(SampleInput.xlsx)作为模板。工作簿包含带有图表和图片的工作表。每个工作表使用不同类型的格式,包括字体、属性、颜色、阴影效果和边框。第一个工作表有柱状图,最后一个工作表有图片。

模板Excel文件

模板文件包含三个工作表,包括图表和图片作为媒体。第一个工作表有图表,最后一个工作表有一张图片,如截图所示。

todo:image_alt_text todo:image_alt_text
第一个工作表 (销售预测) 第二个工作表 (销售报告)
todo:image_alt_text todo:image_alt_text
第三个工作表 (数据录入) 最后一个工作表 (图像)

转换过程

#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

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    try
    {
        // Get the template excel file path
        U16String designerFile = srcDir + u"SampleInput.xlsx";

        // Specify the pdf file path
        U16String pdfFile = outDir + u"Output.out.pdf";

        // Open the template excel file
        Workbook wb(designerFile);

        // Save the pdf file
        wb.Save(pdfFile, SaveFormat::Pdf);

        std::cout << "PDF file saved successfully!" << std::endl;
    }
    catch (const std::exception& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    Aspose::Cells::Cleanup();
    return 0;
}

结果

当上述代码运行后,在应用程序目录的Files文件夹中创建了一个PDF文件。 以下屏幕截图显示了PDF页面。 请注意,页眉和页脚也在输出PDF文件中保留。

todo:image_alt_text todo:image_alt_text
第一个工作表 (销售预测) 第二个工作表 (销售报告)
todo:image_alt_text todo:image_alt_text
第三个工作表 (数据录入) 最后一个工作表 (图像)