最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++讀寫word文檔(.docx)DuckX庫(kù)的使用詳解

 更新時(shí)間:2025年09月25日 10:41:30   作者:Q.V.Q  
DuckX是C++庫(kù),用于創(chuàng)建/編輯.docx文件,支持讀取文檔、添加段落/片段、編輯表格,解決中文亂碼需更改編碼方案,進(jìn)階功能含文本替換(支持表格)和文檔合并(僅限文本)

DuckX是一個(gè)用于創(chuàng)建和編輯 Microsoft Word (.docx) 文件的 C++ 庫(kù)。

一、基本用法

1. 讀取文檔

#include <iostream>
#include "duckx.hpp"

int main() {
	duckx::Document doc("foo.docx");
	doc.open();

	for (auto p = doc.paragraphs(); p.has_next(); p.next()) {
		for (auto r = p.runs(); r.has_next(); r.next()) {
			std::cout << r.get_text() << std::endl;
		}
	}

	return 0;
}

運(yùn)行結(jié)果如下:

中文亂碼的原因時(shí)由于將UTF-8字符串使用GBK編碼顯示了,更改編碼方案即可。

3. 添加段落

#include "duckx.hpp"
#include <iostream>

int main() {
    // 加載文檔
    duckx::Document doc("foo.docx");
    doc.open();

    // 遍歷段落
    duckx::Paragraph& paragraph = doc.paragraphs();
    while (paragraph.has_next()) {
        // 如果需要在某段之后插入段落
        if (paragraph.runs().get_text() == "AAA") {
            paragraph.insert_paragraph_after("This is a new paragraph.");
        }

        // 移動(dòng)到下一個(gè)段落
        paragraph.next();
    }

    // 保存修改后的文檔
    doc.save();

    return 0;
}

原始文件如下:

修改文件如下:

4. 添加片段

#include "duckx.hpp"
#include <iostream>

int main() {
    // 加載文檔
    duckx::Document doc("foo.docx");
    doc.open();

    // 遍歷段落
    duckx::Paragraph& paragraph = doc.paragraphs();
    while (paragraph.has_next()) {
        // 在某段中追加運(yùn)行文本
        if (paragraph.runs().get_text() == "AAA") {
            paragraph.add_run(" Added new text here.");
        }

        // 移動(dòng)到下一個(gè)段落
        paragraph.next();
    }

    // 保存修改后的文檔
    doc.save();

    return 0;
}

3. 編輯表格

#include "duckx.hpp"
#include <iostream>

int main() {
    // 加載文檔
    duckx::Document doc("table.docx");
    doc.open();

    // 遍歷表格
    duckx::Table& table = doc.tables();
    while (table.has_next()) {
        duckx::TableRow& row = table.rows();
        while (row.has_next()) {
            duckx::TableCell& cell = row.cells();
            while (cell.has_next()) {
                // 在單元格內(nèi)新增段落
                duckx::Paragraph& paragraph = cell.paragraphs();
                if (paragraph.runs().get_text() == "") {
                    paragraph.add_run("2024");
                }
                cell.next();
            }
            row.next();
        }
        table.next();
    }

    // 保存修改后的文檔
    doc.save();

    return 0;
}

原始文檔如下:

修改文檔如下:

二、進(jìn)階用法

1. 文本替換

#include "duckx.hpp"
#include <iostream>
#include <unordered_map>
#include <string>

void Replace(const std::string & path, const std::unordered_map<std::string, std::string>& replacements) {
    // 打開(kāi)文檔
    duckx::Document doc(path);
    doc.open();

    // 遍歷段落
    for (auto p = doc.paragraphs(); p.has_next(); p.next()) {
        // 遍歷運(yùn)行文本
        for (auto r = p.runs(); r.has_next(); r.next()) {
            // 獲取當(dāng)前運(yùn)行文本內(nèi)容
            std::string text = r.get_text();

            // 檢查鍵值對(duì)中的鍵是否存在于當(dāng)前文本中
            for (const auto& [key, value] : replacements) {
                // 如果找到匹配鍵,進(jìn)行替換
                size_t pos = text.find(key);
                if (pos != std::string::npos) {
                    text.replace(pos, key.length(), value);
                    r.set_text(text);
                }
            }
        }
    }

    // 保存修改后的文檔
    doc.save();
}

int main() {
    std::unordered_map<std::string, std::string> replacements = {
        {"{name}", "John Doe"},
        {"{date}", "2024-11-29"},
        {"{city}", "New York"}
    };
    Replace("foo.docx", replacements);
    std::cout << "Replacements complete. Saved to foo.docx." << std::endl;
    return 0;
}

進(jìn)階版:可同時(shí)替換普通文本和表格中的文本

#include "duckx.hpp"
#include <iostream>
#include <unordered_map>
#include <string>

void Replace(const std::string& path, const std::unordered_map<std::string, std::string>& replacements) {
    // 打開(kāi)文檔
    duckx::Document doc(path);
    doc.open();

    // 遍歷段落
    for (auto p = doc.paragraphs(); p.has_next(); p.next()) {
        // 遍歷運(yùn)行文本
        for (auto r = p.runs(); r.has_next(); r.next()) {
            // 獲取當(dāng)前運(yùn)行文本內(nèi)容
            std::string text = r.get_text();

            // 檢查鍵值對(duì)中的鍵是否存在于當(dāng)前文本中
            for (const auto& [key, value] : replacements) {
                // 如果找到匹配鍵,進(jìn)行替換
                size_t pos = text.find(key);
                if (pos != std::string::npos) {
                    text.replace(pos, key.length(), value);
                    r.set_text(text);
                }
            }
        }
    }

    // 遍歷表格
    for (auto t = doc.tables(); t.has_next(); t.next()) {
        // 遍歷表格行
        for (auto r = t.rows(); r.has_next(); r.next()) {
            // 遍歷表格單元格
            for (auto c = r.cells(); c.has_next(); c.next()) {
                // 遍歷單元格中的段落
                for (auto p = c.paragraphs(); p.has_next(); p.next()) {
                    // 遍歷單元格段落中的運(yùn)行文本
                    for (auto r = p.runs(); r.has_next(); r.next()) {
                        // 獲取當(dāng)前運(yùn)行文本內(nèi)容
                        std::string text = r.get_text();

                        // 檢查鍵值對(duì)中的鍵是否存在于當(dāng)前文本中
                        for (const auto& [key, value] : replacements) {
                            // 如果找到匹配鍵,進(jìn)行替換
                            size_t pos = text.find(key);
                            if (pos != std::string::npos) {
                                text.replace(pos, key.length(), value);
                                r.set_text(text);
                            }
                        }
                    }
                }
            }
        }
    }

    // 保存修改后的文檔
    doc.save();
}

int main() {
    std::unordered_map<std::string, std::string> replacements = {
        {"{name}", "John Doe"},
        {"{date}", "2024-11-29"},
        {"{city}", "New York"}
    };
    Replace("foo.docx", replacements);
    std::cout << "Replacements complete. Saved to foo.docx." << std::endl;
    return 0;
}

2. 合并文檔

只能合并文本

#include "duckx.hpp"
#include <iostream>

int main() {
    // 加載第一個(gè)文檔
    duckx::Document doc1("document1.docx");
    doc1.open();

    // 加載第二個(gè)文檔
    duckx::Document doc2("document2.docx");
    doc2.open();

    // 將第二個(gè)文檔的段落添加到第一個(gè)文檔
    duckx::Paragraph &paragraph2 = doc2.paragraphs();
    while (paragraph2.has_next()) {
        // 獲取第二個(gè)文檔中的段落
        std::string text = paragraph2.runs().get_text();

        // 在第一個(gè)文檔中插入段落
        doc1.paragraphs().insert_paragraph_after(text);

        paragraph2.next();
    }

    // 將第二個(gè)文檔的表格添加到第一個(gè)文檔
    duckx::Table &table2 = doc2.tables();
    while (table2.has_next()) {
        duckx::TableRow &row2 = table2.rows();
        while (row2.has_next()) {
            duckx::TableCell &cell2 = row2.cells();
            while (cell2.has_next()) {
                // 獲取第二個(gè)文檔中的單元格
                std::string cellText = cell2.paragraphs().runs().get_text();

                // 在第一個(gè)文檔中插入單元格
                doc1.tables().rows().cells().add_run(cellText);

                cell2.next();
            }
            row2.next();
        }
        table2.next();
    }

    // 保存合并后的文檔
    doc1.save();

    std::cout << "Documents merged and saved to document1.docx." << std::endl;
    return 0;
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • JS調(diào)用C++函數(shù)拋出異常及捕捉異常詳解

    JS調(diào)用C++函數(shù)拋出異常及捕捉異常詳解

    這篇文章主要介紹了js調(diào)用C++函數(shù)的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-08-08
  • C++vector自定義大小方式

    C++vector自定義大小方式

    這篇文章主要介紹了C++vector自定義大小方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • COLORREF,COLOR,RGB,CString的轉(zhuǎn)化總結(jié)分析

    COLORREF,COLOR,RGB,CString的轉(zhuǎn)化總結(jié)分析

    實(shí)際的軟件開(kāi)發(fā)過(guò)程中,常需要用到非.net平臺(tái)的代碼。這時(shí)候就可能碰到ColorRef(也就是以int類型代表的顏色值或是以DWORD值表示的顏色)。這跟.net平臺(tái)下的顏色的相互轉(zhuǎn)換MS并沒(méi)有直接實(shí)現(xiàn)
    2013-09-09
  • C語(yǔ)言實(shí)現(xiàn)大數(shù)據(jù)文件的內(nèi)存映射機(jī)制

    C語(yǔ)言實(shí)現(xiàn)大數(shù)據(jù)文件的內(nèi)存映射機(jī)制

    這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)大數(shù)據(jù)文件的內(nèi)存映射機(jī)制的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • C++?OpenCV紅綠燈檢測(cè)Demo實(shí)現(xiàn)詳解

    C++?OpenCV紅綠燈檢測(cè)Demo實(shí)現(xiàn)詳解

    OpenCV(Open Source Computer Vision Library)是開(kāi)源的計(jì)算機(jī)視覺(jué)和機(jī)器學(xué)習(xí)庫(kù),提供了C++、 C、 Python、 Java接口,并支持Windows、 Linux、 Android、 Mac OS平臺(tái),下面這篇文章主要給大家介紹了關(guān)于C++?OpenCV紅綠燈檢測(cè)Demo實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)程序設(shè)計(jì)

    C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)程序設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • QT的QWebEngineView類知識(shí)點(diǎn)詳細(xì)介紹

    QT的QWebEngineView類知識(shí)點(diǎn)詳細(xì)介紹

    QWebEngineView是Qt框架中的組件,基于Chromium內(nèi)核,支持HTML5、CSS3、JavaScript等Web技術(shù),適用于嵌入網(wǎng)頁(yè)內(nèi)容到Qt應(yīng)用程序,它提供了豐富的接口如加載、導(dǎo)航、與JavaScript交互等,并支持信號(hào)槽機(jī)制處理各種網(wǎng)頁(yè)事件,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-10-10
  • C到C++的升級(jí)關(guān)系及區(qū)別實(shí)例探究

    C到C++的升級(jí)關(guān)系及區(qū)別實(shí)例探究

    這篇文章主要為大家介紹了C到C++的升級(jí)關(guān)系及區(qū)別實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • C++超詳細(xì)講解邏輯操作符

    C++超詳細(xì)講解邏輯操作符

    在C語(yǔ)言中,邏輯運(yùn)算符有&&、||、!;&&表示“與”的意思,需要兩端的表達(dá)式的值都為true,該式的值才為true。||表示“或”的意思,兩端的表達(dá)式的值只要有一端為true,該式的值就為true。!表示“非”的意思,將該式的真值換成相反的真值,即false和true互換
    2022-06-06
  • VS中scanf為何會(huì)報(bào)錯(cuò)詳解

    VS中scanf為何會(huì)報(bào)錯(cuò)詳解

    在我們剛使用vs時(shí),在使用scanf函數(shù)時(shí)常會(huì)遇到報(bào)錯(cuò)提醒,下面這篇文章主要給大家介紹了關(guān)于VS中scanf為何會(huì)報(bào)錯(cuò)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02

最新評(píng)論

泽州县| 驻马店市| 栾城县| 友谊县| 六安市| 林州市| 万荣县| 泊头市| 河源市| 周口市| 山西省| 台山市| 大城县| 苍溪县| 东阳市| 民勤县| 光山县| 新营市| 保康县| 长海县| 南平市| 南郑县| 威海市| 习水县| 正定县| 鹿邑县| 澄迈县| 崇礼县| 通化县| 阜阳市| 大渡口区| 龙里县| 民乐县| 略阳县| 汉阴县| 安徽省| 明星| 毕节市| 津市市| 江山市| 共和县|