C++でXLSXファイルをPDF形式に変換する方法を学ぶ。

ExcelをPDFに変換する

この例では、テンプレートとしてExcelファイル(SampleInput.xlsx)を使用しています。ワークブックにはチャートと画像が含まれたシートがあります。各シートにはフォント、属性、色、シェーディング効果、境界線を使用したさまざまな書式があります。最初のシートには縦列チャート、最後には画像があります。

テンプレートの Excel ファイル

テンプレートファイルには、チャートと画像を含む3つのシートがあります。最初のシートはチャートを持ち、最後のシートには画像があります。スクリーンショットは以下の通りです。

todo:image_alt_text todo:image_alt_text
最初のワークシート**(売上予測)** 2番目のワークシート**(売上報告)**
todo:image_alt_text todo:image_alt_text
3番目のワークシート**(データ入力)** 最後のワークシート**(画像)**

変換プロセス

#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
最初のワークシート**(売上予測)** 2番目のワークシート**(売上報告)**
todo:image_alt_text todo:image_alt_text
3番目のワークシート**(データ入力)** 最後のワークシート**(画像)**