minidocx使用
使用理由:
1.duckX不支持添加表格
2.openoffice使用略困难,
3.qt自带QAxContainer 需求安装wps/office 且执行效率低
一、下载编译
1.地址:minidocx: 用于创建 Microsoft Word 文档的 C++ 类库,支持分页、换行、分节,支持插入页码、表格、图文框,支持设置字体格式、段落格式、页面格式、表格样式等。
2.工具需求:Cmake+VS (我用的VS2022)
3.编译
a.选择路径
b.config>generate> openproject
c.选择生成模式debug/release
d.lib生成位置
二、使用
1.头文件需求
2.库
3.vs 配置
库目录
头文件目录
4.库添加 这里我用的debug所以添加对应的debug库
5.代码例子 来自官网
#include <iostream> #include "minidocx.hpp" using namespace docx; int main() { Document doc; auto p1 = doc.AppendParagraph("Hello, World!", 12, "Times New Roman"); auto p2 = doc.AppendParagraph(u8"你好,世界!", 14, u8"宋体"); auto p3 = doc.AppendParagraph(u8"Hello, 世界!", 16, "Times New Roman", u8"宋体"); auto p4 = doc.AppendParagraph(); p4.SetAlignment(Paragraph::Alignment::Centered); auto p4r1 = p4.AppendRun("Sample text here...", 12, "Arial"); p4r1.AppendLineBreak(); p4r1.SetCharacterSpacing(Pt2Twip(2)); auto p4r2 = p4.AppendRun("And another line of sample text here..."); p4r2.SetFontSize(14); p4r2.SetFont("Times New Roman"); p4r2.SetFontColor("FF0000"); p4r2.SetFontStyle(Run::Bold | Run::Italic); doc.Save("c:\\word\\basic.docx"); return 0; }
6.根据自己的需求简单封装一下
#pragma once #include<unordered_map> #include "minidocx.hpp" using namespace docx; /// <summary> /// minidcx 使用的方法封装 /// /// </summary> class miniDocxHelper { public: miniDocxHelper(); miniDocxHelper(const std::string& strFileName); ~miniDocxHelper(); bool open(const std::string& strFileName); void save(); void saveAs(const std::string& strFileName); bool replace(const std::string& strFind, const std::string& strReplace); bool replace(const std::unordered_map<std::string, std::string>& replacements); void addTable(unsigned int row, unsigned int column); bool addLineAfterTextOfLine(const std::string& strFindeText, const std::string& strNewLine); protected: Document m_doc; std::string m_strOpenFile; };
#include "miniDocxHelper.h" miniDocxHelper::miniDocxHelper() { m_strOpenFile = ""; } miniDocxHelper::miniDocxHelper(const std::string& strFileName) { m_doc.Open(strFileName); } miniDocxHelper::~miniDocxHelper() { } bool miniDocxHelper::open(const std::string& strFileName) { m_strOpenFile = strFileName; return m_doc.Open(strFileName); } void miniDocxHelper::save() { m_doc.Save(m_strOpenFile); } void miniDocxHelper::saveAs(const std::string& strFileName) { m_doc.Save(strFileName); } std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) { size_t start_pos = 0; while ((start_pos = str.find(from, start_pos)) != std::string::npos) { str.replace(start_pos, from.length(), to); start_pos += to.length(); // 处理完当前匹配后,跳过已替换的部分 } return str; } bool miniDocxHelper::replace(const std::string& strFind, const std::string& strReplace) { bool bFind = false; for (auto p = m_doc.FirstParagraph(); p; p = p.Next()) { // 遍历段落中的所有富文本 for (auto r = p.FirstRun(); r; r = r.Next()) { std::string runText = r.GetText(); size_t pos = runText.find(strFind); if (pos != std::string::npos) { std::string newText = ReplaceAll(runText, strFind, strReplace); // 移除旧文本 r.ClearText(); // 插入新文本 r.AppendText(newText); // 更新位置,继续查找下一个匹配项 bFind = true; } } } return bFind; } bool miniDocxHelper::replace(const std::unordered_map<std::string, std::string>& replacements) { bool bFind = false; for (auto p = m_doc.FirstParagraph(); p; p = p.Next()) { // 遍历段落中的所有富文本 for (auto r = p.FirstRun(); r; r = r.Next()) { std::string runText = r.GetText(); // 检查键值对中的键是否存在于当前文本中 c++17 for (const auto& [key, value] : replacements) { // 如果找到匹配键,进行替换 size_t pos = runText.find(key); if (pos != std::string::npos) { std::string newText = ReplaceAll(runText, key, value); // 移除旧文本 r.ClearText(); // 插入新文本 r.AppendText(newText); // 更新位置,继续查找下一个匹配项 bFind = true; } } } } return bFind; } void miniDocxHelper::addTable(unsigned int row, unsigned int column) { //auto tbl = m_doc.AppendTable(row, column); //tbl.GetCell(0, 0).FirstParagraph().AppendRun("AAA"); //tbl.GetCell(0, 1).FirstParagraph().AppendRun("BBB"); //tbl.GetCell(0, 2).FirstParagraph().AppendRun("CCC"); //tbl.GetCell(1, 0).FirstParagraph().AppendRun("DDD"); //tbl.GetCell(1, 1).FirstParagraph().AppendRun("EEE"); } bool miniDocxHelper::addLineAfterTextOfLine(const std::string& strFindeText, const std::string& strNewLine) { bool bFind = false; docx::Paragraph paragraph = m_doc.FirstParagraph(); while (!paragraph) { // 在某段中追加运行文本 if (paragraph.GetText().find(strFindeText) != std::string::npos) { auto addParagraph = m_doc.InsertParagraphAfter(paragraph); addParagraph.AppendRun(strNewLine); bFind = true; } // 移动到下一个段落 paragraph = paragraph.Next(); } return bFind; }
#include <iostream> #include"miniDocxHelper.h" using namespace docx; int main() { miniDocxHelper minidoc; minidoc.open("c:\\word\\basic.docx"); minidoc.addTable(3,3); //minidoc.addLineAfterTextOfLine("Hello", "123"); //minidoc.replace("Hello", "123"); std::unordered_map<std::string, std::string> replaceMap; replaceMap["Hello"] = "123"; replaceMap[u8"世界"] = u8"大树"; replaceMap["line"] = u8"好好好"; minidoc.replace(replaceMap); minidoc.saveAs("c:\\word\\basic_New.docx"); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!