一、库编译
1.下载地址:https://github.com/amiremohamadi/DuckX
2. 使用git 下载: git clone https://github.com/amiremohamadi/DuckX
3.编译
1.打开代码所在目录
2.创建生成目录build
3.依次点击 config generate
4. 打开项目,选择编译库的种类
生成库位置
二、使用
C++读写word文档(.docx) DuckX库的使用-CSDN博客
1.需求文件和第三方库
a.头文件
b.第三方依赖
c.库文件
2、配置库文件以及头文件包含
3.代码
#include <iostream> #include"include/duckx.hpp" #include"duckXWordHelper.h" using namespace std;int main() {duckXWordHelper duckHelper("c:\\word\\my_test.docx","c:\\word\\newmy_test.docx");duckHelper.addTable();duckHelper.addNewLineText("okay","this is a new line");duckHelper.save(); }
#pragma once #include <fstream> #include <iostream> #include<unordered_map> #include"duckx.hpp" /// <summary> /// duckX word操作帮助类 /// 项目常见的word操作封装 /// 需求c++17 /// </summary> class duckXWordHelper { public:duckXWordHelper();duckXWordHelper(const std::string& docFileName);duckXWordHelper(const std::string& docFileName, const std::string& docCopyFileName);~duckXWordHelper();void open(const std::string& strFileName);void save();bool copyFile(const std::string& sourceFilePath, const std::string& destinationFilePath);bool addNewLineText(const std::string& strFindeText, const std::string& strNewLine);void addTable();bool replace(const std::unordered_map<std::string, std::string>& replacements); private:duckx::Document m_doc; };
#include "duckXWordHelper.h"duckXWordHelper::duckXWordHelper() { }duckXWordHelper::duckXWordHelper(const std::string& docFileName) {m_doc.file(docFileName);m_doc.open(); }duckXWordHelper::duckXWordHelper(const std::string& docFileName, const std::string& docCopyFileName) {copyFile(docFileName, docCopyFileName);m_doc.file(docCopyFileName);m_doc.open(); }duckXWordHelper::~duckXWordHelper() { }void duckXWordHelper::open(const std::string& strFileName) {m_doc.file(strFileName);m_doc.open(); }void duckXWordHelper::save() {m_doc.save(); }bool duckXWordHelper::copyFile(const std::string& sourceFilePath, const std::string& destinationFilePath) {std::ifstream source(sourceFilePath, std::ios::binary);if (!source) {std::cerr << "Error opening source file: " << sourceFilePath << std::endl;return false;}std::ofstream destination(destinationFilePath, std::ios::binary);if (!destination) {std::cerr << "Error opening destination file: " << destinationFilePath << std::endl;return false;}destination << source.rdbuf();if (!destination) {std::cerr << "Error writing to destination file: " << destinationFilePath << std::endl;return false;}return true; }bool duckXWordHelper::addNewLineText(const std::string& strFindeText, const std::string& strNewLine) {bool bFind = false;duckx::Paragraph& paragraph = m_doc.paragraphs();while (paragraph.has_next()) {// 在某段中追加运行文本if (paragraph.runs().get_text() == strFindeText) {paragraph.add_run(strNewLine);bFind = true;}// 移动到下一个段落 paragraph.next();}return bFind; }void duckXWordHelper::addTable() {//表头 内容// 遍历表格duckx::Table& table = m_doc.tables();//table.rows().cells().add_run(cellText);std::string cellText = "111";m_doc.tables().rows().cells().paragraphs().add_run(cellText);m_doc.tables().rows().cells().paragraphs().add_run(cellText);m_doc.tables().rows().cells().paragraphs().add_run(cellText);m_doc.tables().rows().cells().paragraphs().add_run(cellText);//m_doc.tables().//while (table.has_next()) {// duckx::TableRow& row = table.rows();// while (row.has_next()) {// duckx::TableCell& cell = row.cells();// while (cell.has_next()) {// // 在单元格内新增段落// duckx::Paragraph& paragraph = cell.paragraphs();// if (paragraph.runs().get_text() == "") {// paragraph.add_run("2024");// }// cell.next();// }// row.next();// }// table.next();//} }bool duckXWordHelper::replace(const std::unordered_map<std::string, std::string>& replacements) {// 遍历段落for (auto p = m_doc.paragraphs(); p.has_next(); p.next()) {// 遍历运行文本for (auto r = p.runs(); r.has_next(); r.next()) {// 获取当前运行文本内容std::string text = r.get_text();// 检查键值对中的键是否存在于当前文本中 c++17for (const auto& [key,value] : replacements) {// 如果找到匹配键,进行替换size_t pos = text.find(key);if (pos != std::string::npos) {text.replace(pos, key.length(), value);r.set_text(text);}}}}// 遍历表格for (auto t = m_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()) {// 遍历单元格段落中的运行文本for (auto r = p.runs(); r.has_next(); r.next()) {// 获取当前运行文本内容std::string text = r.get_text();// 检查键值对中的键是否存在于当前文本中for (const auto& [key, value] : replacements) {// 如果找到匹配键,进行替换size_t pos = text.find(key);if (pos != std::string::npos) {text.replace(pos, key.length(), value);r.set_text(text);}}}}}}}return true; }