使用C++中的Mail merge創建docx

用於對來自自定義數據源的docx模板和數據執行Mail merge操作的高速C++庫

我們的解決方案提供了強大的平台無關API 。使用我們的Mail merge文檔API為C++平台開發高級軟件。這是一個強大的軟件解決方案,用於合併Word和PDF格式的模板以及來自自定義數據源的數據,並使用C++進一步將結果導出為DOCX, PDF, HTML和其他流行格式。

查看代碼片段

在 C++ 中 Mail merge Word 文件和 Excel 數據

快速 C++ 庫,用於執行郵件與 Excel 數據的合併。

我們的 Mail Merge SDK 允許您使用 Excel 工作表中的數據填充 Word 模板。基於我們的 Mail Merge API ,您可以使用 C++ 創建您自己的軟件解決方案,它允許您自動從 Excel 發送電子郵件並生成包含 Excel 數據的報告。

要將 Mail merge 到 Word,只需在 C++ 中執行 Mail merge 操作並將結果導出為任何支持的 Word 格式。

使用 C++ Mail merge 到 Word

借助我們強大的 Mail merge API ,您可以僅使用 Word 合併模板和 Excel 中的數據在 C++ 中創建個性化文檔。

如何從 Excel 使用 Mail merge :

  • 創建一個帶有特殊合併字段的 Word 模板,您的 Excel 數據將插入其中。
  • 將 Excel 文檔轉換為 CSV 文件,然後轉換為適合我們的 API 解析的 JSON 文件。
  • 執行郵件合併 Word 模板和從 Excel 轉換為 CSV 再到 JSON 的數據的操作。

Mail merge

通過我們的高保真 Mail merge API ,您還可以使用 Google 文檔執行 Mail merge 操作。只需下載Word和Excel格式的必要數據,確保Word模板包含合併字段,並在 C++ 中進行郵件合併操作,類似於上一節描述的算法。

要試用這個強大的 Mail merge web 工具,請加載一個帶有特殊合併字段的 Word 模板,以及從 Excel 轉換的數據,如上所述。運行代碼後,使用 C++ 將結果導出為任何格式。

以下示例顯示如何執行 Mail merge 操作以在 C++ 中生成 Word 文檔:

使用 C++中的docx模板執行Mail merge操作
上傳Mail merge模板
運行代碼
上傳Mail merge數據
從列表中選擇目標格式
using namespace Aspose::Words;
using namespace Aspose::Cells;

auto doc = MakeObject<Document>(u"Input1.docx");        
auto workbook = Factory::CreateIWorkbook(new String("Input2.xlsx"));
auto worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
auto dataRange = worksheet->GetICells()->GetMaxDisplayIRange();

auto dataTable = worksheet->GetICells()->ExportArray(
   0, 0, dataRange->GetRowCount(), dataRange->GetColumnCount());
auto customersDataSource = MakeObject<BaseOperations::CustomerMailMergeDataSource>(dataTable);

doc->get_MailMerge()->Execute(customersDataSource);
doc->Save(u"Output.docx");

class ExcelDataSource : public IMailMergeDataSource
{
public:
    System::String convertCellsString(String cellsValue)
    {
        auto valCh = cellsValue.charValue();
        auto convertedString = System::String(valCh);
        String::deleteCharValue(valCh);
        return convertedString;
    }

    System::String get_TableName() override
    {
        return nullptr;
    }

    CustomerMailMergeDataSource(
     intrusive_ptr<Aspose::Cells::Systems::Array2D<Aspose::Cells::Systems::Object*>> data)
    {
        dataIn = data;

        for (int i = 0; i < data->At(0)->GetLength(); i++) {
            auto fieldName = data->GetValue(0, i)->ToString();
            m.insert({ convertCellsString(fieldName), i });
            i++;
        }

        mRecordIndex = 0;
    }        

    bool GetValue(System::String fieldName, SharedPtr<System::Object>& fieldValue) override
    {
        auto value = dataIn->GetValue(mRecordIndex, m.at(fieldName))->ToString();
        if (value == new String(""))
            return false;

        fieldValue = System::ObjectExt::Box<System::String>(convertCellsString(value));
        return true;        
    }

    bool MoveNext() override
    {
        mRecordIndex++;
        return mRecordIndex < dataIn->GetLength();            
    }

    SharedPtr<IMailMergeDataSource> GetChildDataSource(System::String tableName) override
    {
        return nullptr;            
    }

    intrusive_ptr<Aspose::Cells::Systems::Array2D<Aspose::Cells::Systems::Object*>> dataIn;
    std::unordered_map<System::String, int> m;
    int mRecordIndex;
};
運行代碼

如何在docx文件中使用Mail merge

  1. 安裝Aspose.Words for C++ 。
  2. 將庫引用(導入庫)添加到您的C++項目。
  3. 使用合併字段創建docx合併模板。
  4. 打開C++中的模板文件。
  5. 調用Execute()方法,在那里傳遞合併字段及其值。
  6. 調用Save()方法,傳遞帶有所需擴展名的輸出文件名。
  7. 獲取Mail merge操作的結果作為Word文件。

C++庫以在docx中使用Mail merge

有三個選項可以將 Aspose.Words for C++ 安裝到您的開發人員環境中。請選擇符合您需求的一種,並按照分步說明進行操作:

系統要求

您可以使用這個 C++ 庫在 Microsoft Windows 、Linux 和 macOS 操作系統上開發軟件:

  • Linux 需要 GCC >= 6.3.0 和 Clang >= 3.9.1
  • Xcode >= 12.5.1 ,macOS 需要 Clang 和 libc++

如果您為 Linux 或 macOS 開發軟件,請查看產品文檔中 fontconfig 和 mesa-glu 開源包)的信息。

其他支持的文件格式

您可以對其他文件格式執行Mail merge操作:

5%

訂閱 Aspose 產品更新

獲取直接發送到您的郵箱的每月通訊和優惠。

© Aspose Pty Ltd 2001-2024. 版權所有。