Qt WORD/PDF(四)使用 QAxObject 对 Word 替换(QWidget)

打印 上一主题 下一主题

主题 1000|帖子 1000|积分 3010


关于QT Widget 其它文章请点击这里:     QT Widget
国际站点 GitHub:     https://github.com/chenchuhan
国内站点 Gitee :      https://gitee.com/chuck_chee
姊妹篇:     
Qt WORD/PDF(一)使用 QtPdfium库实现 PDF 操作
Qt WORD/PDF(二)使用 QtPdfium库实现 PDF 预览、打印等
Qt WORD/PDF(三)使用 QAxObject 对 Word 替换(QML)
Qt WORD/PDF(四)使用 QAxObject 对 Word 替换(QWidget)

一、QAxObject 简介

QAxObject 是 Qt 提供的一个类,它用于与 COM(Component Object Model)对象举行交互。COM 是一种微软的技术,广泛用于各种应用程序之间的通讯,尤其在 Windows 平台上,很多软件和系统组件都是基于 COM 构建的。QAxObject 类提供了一个 Qt 风格的接口,简化了与这些 COM 对象的交互。
本文紧张使用 QAxObject 操作 word 文档,使用键值对,对模板文件举行替换操作,导出相应的文档,特殊适合输出陈诉。
本文采用 Qt Widget 纯代码的方式
环境:
QT5.15.2 + MSVC2019 + Widget
二、演示


实现功能:


  • 用户可以选择一个模板文件,并举行占位符的批量替换。
  • 用户可以设置替换后文档的保存路径。
  • 支持通过界面交互实现选择文件、体现信息以及实行替换操作。
  • 使用 QAxObject 实现了与 Word 的 COM 接口的交互,允许直接操作 Word 文档中的内容。
三、代码

完整代码

mainwindow.cpp:
  1. #include "mainwindow.h"
  2. // #include "ui_mainwindow.h"
  3. #include <QFileDialog>
  4. #include <QDebug>
  5. #include <QMessageBox>
  6. MainWindow::MainWindow(QWidget *parent)
  7.     : QMainWindow(parent)
  8.     // , ui(new Ui::MainWindow)
  9. {
  10.     // ui->setupUi(this);
  11.     // 初始化 UI
  12.     auto *centralWidget = new QWidget(this);
  13.     auto *mainLayout = new QVBoxLayout;
  14.     mainLayout->setContentsMargins(10, 10, 10, 10); // 设置布局边距
  15.     mainLayout->setSpacing(10); // 设置控件之间的间距
  16.     // setMinimumSize(600, 520); // 设置窗口最小宽度为600,高度为400
  17.     // 模板文件选择
  18.     auto *templateLayout = new QHBoxLayout;
  19.     auto *templateLabel = new QLabel("打开模板:", this);
  20.     templatePathEdit = new QLineEdit(this);
  21.     auto *browseTemplateButton = new QPushButton("浏览", this);
  22.     templateLayout->addWidget(templateLabel);
  23.     templateLayout->addWidget(templatePathEdit);
  24.     templateLayout->addWidget(browseTemplateButton);
  25.     connect(browseTemplateButton, &QPushButton::clicked, this, &MainWindow::browseTemplateFile);
  26.     // 输出文件选择
  27.     auto *outputLayout = new QHBoxLayout;
  28.     auto *outputLabel = new QLabel("输出路径:", this);
  29.     outputPathEdit = new QLineEdit(this);
  30.     auto *browseOutputButton = new QPushButton("浏览", this);
  31.     outputLayout->addWidget(outputLabel);
  32.     outputLayout->addWidget(outputPathEdit);
  33.     outputLayout->addWidget(browseOutputButton);
  34.     connect(browseOutputButton, &QPushButton::clicked, this, &MainWindow::browseOutputFile);
  35.     // 键值对表格
  36.     auto *placeholdersLabel = new QLabel("键值对替换:", this);
  37.     placeholdersTable = new QTableWidget(this);
  38.     placeholdersTable->setColumnCount(2);
  39.     placeholdersTable->setHorizontalHeaderLabels({"占位符", "替换值"});
  40.     placeholdersTable->setRowCount(5); // 默认三行
  41.     // 设置默认值
  42.     placeholdersTable->setItem(0, 0, new QTableWidgetItem("[A]"));
  43.     placeholdersTable->setItem(0, 1, new QTableWidgetItem("柯布"));
  44.     placeholdersTable->setItem(1, 0, new QTableWidgetItem("[B]"));
  45.     placeholdersTable->setItem(1, 1, new QTableWidgetItem("阿瑟"));
  46.     placeholdersTable->setItem(2, 0, new QTableWidgetItem("[C]"));
  47.     placeholdersTable->setItem(2, 1, new QTableWidgetItem("杜拉"));
  48.     placeholdersTable->setItem(3, 0, new QTableWidgetItem("[D]"));
  49.     placeholdersTable->setItem(3, 1, new QTableWidgetItem("伊姆斯"));
  50.     // 替换按钮
  51.     replaceButton = new QPushButton("执行替换", this);
  52.     connect(replaceButton, &QPushButton::clicked, this, &MainWindow::replaceInWord);
  53.     // 布局整合
  54.     mainLayout->addLayout(templateLayout);
  55.     mainLayout->addLayout(outputLayout);
  56.     mainLayout->addWidget(placeholdersLabel);
  57.     mainLayout->addWidget(placeholdersTable);
  58.     mainLayout->addWidget(replaceButton);
  59.     centralWidget->setLayout(mainLayout);
  60.     setCentralWidget(centralWidget);
  61.     setWindowTitle("Word 替换工具");
  62.     resize(1000, 600); // 初始窗口大小
  63. }
  64. MainWindow::~MainWindow()
  65. {
  66.     // delete ui;
  67. }
  68. void MainWindow::browseTemplateFile() {
  69.     QString filePath = QFileDialog::getOpenFileName(this, "选择模板文件", QString(), "Word 文件 (*.docx *.doc)");
  70.     if (!filePath.isEmpty()) {
  71.         templatePathEdit->setText(filePath);
  72.          wordApp = new QAxObject("Word.Application");
  73.         if (wordApp->isNull()) {
  74.             qDebug() << "Failed to initialize Word.Application.";
  75.             delete wordApp;
  76.             return ;
  77.         }
  78.         // 隐藏 Word 窗口
  79.         wordApp->setProperty("Visible", true);
  80.         //打开指定文档
  81.         QAxObject *documents = wordApp->querySubObject("Documents");
  82.         QAxObject *document = documents->querySubObject("Open(const QString&)", filePath);
  83.         if (document == nullptr) {
  84.             QMessageBox::critical(this, "错误", "无法打开 Word 文件!");
  85.             return;
  86.         }
  87.     }
  88. }
  89. void MainWindow::browseOutputFile() {
  90.     QString filePath = QFileDialog::getSaveFileName(this, "选择输出文件", QString(), "Word 文件 (*.docx *.doc)");
  91.     if (!filePath.isEmpty()) {
  92.         outputPathEdit->setText(filePath);
  93.     }
  94. }
  95. void MainWindow::replaceInWord() {
  96.     QString templatePath = templatePathEdit->text();
  97.     QString outputPath = outputPathEdit->text();
  98.     if (templatePath.isEmpty() || outputPath.isEmpty()) {
  99.         QMessageBox::warning(this, "错误", "请填写模板路径和输出路径!");
  100.         return;
  101.     }
  102.     QMap<QString, QString> placeholders;
  103.     for (int row = 0; row < placeholdersTable->rowCount(); ++row) {
  104.         QString key = placeholdersTable->item(row, 0) ? placeholdersTable->item(row, 0)->text() : QString();
  105.         QString value = placeholdersTable->item(row, 1) ? placeholdersTable->item(row, 1)->text() : QString();
  106.         if (!key.isEmpty()) {
  107.             placeholders.insert(key, value);
  108.         }
  109.     }
  110.     if (placeholders.isEmpty()) {
  111.         QMessageBox::warning(this, "错误", "请填写至少一个占位符和替换值!");
  112.         return;
  113.     }
  114.     if (replaceMultiple(templatePath, outputPath, placeholders)) {
  115.         QMessageBox::information(this, "成功", "替换完成!");
  116.     } else {
  117.         QMessageBox::critical(this, "失败", "替换失败!");
  118.     }
  119. }
  120. bool MainWindow::replaceMultiple(const QString &templatePath, const QString &outputPath, const QMap<QString, QString> &placeholders) {
  121.     qDebug() << "Received data:" << placeholders;
  122.     qDebug() << "Template Path:" << templatePath;
  123.     qDebug() << "Output Path:" << outputPath;
  124.     if (!QFile::exists(templatePath)) {
  125.         qDebug() << "Template file does not exist:" << templatePath;
  126.         return false;
  127.     }
  128.     qDebug() << "QFile::exists ok" ;
  129.    
  130.     // 打开模板文件
  131.     QAxObject *documents = wordApp->querySubObject("Documents");
  132.     QAxObject *document = documents->querySubObject("Open(const QString&)", templatePath);
  133.     // 查找占位符并替换
  134.     //使用 Find.Execute 查找占位符,使用 TypeText 方法替换为新内容
  135.     QAxObject *selection = wordApp->querySubObject("Selection");
  136.     // 获取 Find 对象
  137.     QAxObject *find = selection->querySubObject("Find");
  138.     qDebug() << "start placeholde";
  139.     // 遍历占位符键值对, 替换未成功,则有问题
  140.     for (auto it = placeholders.begin(); it != placeholders.end(); ++it) {
  141.         QString placeholder = it.key();
  142.         QString newContent = it.value();
  143.         bool isFound = true;
  144.         //可替换多个,且重复的
  145.         while (isFound) {
  146.             // 查找目标文本并替换
  147.             //            isFound = find->dynamicCall("Execute(const QString&)", placeholder).toBool();
  148.             isFound = find->dynamicCall("Execute(QString, bool, bool, bool, bool, bool, bool, int)",
  149.                                         placeholder,  // 要查找的字符串
  150.                                         false,        // 区分大小写
  151.                                         false,        // 完整单词
  152.                                         false,        // 使用通配符
  153.                                         false,        // 忽略标点符号
  154.                                         false,        // 忽略空格
  155.                                         true,         // 向前查找
  156.                                         1).toBool();   // 查找范围:整个文档
  157.             if (isFound) {
  158.                 // 替换文本
  159.                 selection->dynamicCall("TypeText(const QString&)", newContent);
  160.             }
  161.         }
  162.     }
  163.     qDebug() << "All Find operation succeed!";
  164.     document->dynamicCall("SaveAs(const QString&)", outputPath);
  165.     // 关闭文档
  166.     document->dynamicCall("Close()");
  167.     wordApp->dynamicCall("Quit()");
  168.     delete wordApp;
  169.     return true;
  170. }
复制代码
mainwindow.h:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QTableWidget>
  5. #include <QLineEdit>
  6. #include <QPushButton>
  7. #include <QVBoxLayout>
  8. #include <QHBoxLayout>
  9. #include <QLabel>
  10. #include <QMap>
  11. #include <QAxObject>
  12. #include <QAxWidget>
  13. QT_BEGIN_NAMESPACE
  14. namespace Ui {
  15. class MainWindow;
  16. }
  17. QT_END_NAMESPACE
  18. class MainWindow : public QMainWindow
  19. {
  20.     Q_OBJECT
  21. public:
  22.     MainWindow(QWidget *parent = nullptr);
  23.     ~MainWindow();
  24. private slots:
  25.     void browseTemplateFile();
  26.     void browseOutputFile();
  27.     void replaceInWord();
  28. private:
  29.     QLineEdit *templatePathEdit;
  30.     QLineEdit *outputPathEdit;
  31.     QTableWidget *placeholdersTable;
  32.     QPushButton *replaceButton;
  33.     bool replaceMultiple(const QString &templatePath, const QString &outputPath, const QMap<QString, QString> &placeholders);
  34.     QAxObject *wordApp = nullptr;
  35.     QAxWidget *wordPreview = nullptr;
  36. private:
  37.     Ui::MainWindow *ui;
  38. };
  39. #endif // MAINWINDOW_H
复制代码
pro 中需要增加对 QAxObject 的支持
  1. QT       += core gui axcontainer
复制代码
四、分析:

这段Qt C++代码实现了一个简朴的“Word 文件替换工具”,允许用户通过一个图形界面选择Word模板文件、设置输出路径,并在Word文档中举行占位符的替换操作。
1. 类的构造函数 (MainWindow::MainWindow)



  • UI设置:

    • 使用 QWidget 创建中央窗口,QVBoxLayout 为主布局,内部包含多个控件(如标签、输入框、按钮等)。
    • 通过 QHBoxLayout 设置了模板文件选择地区(输入框和浏览按钮)、输出路径选择地区(输入框和浏览按钮)、以及键值对表格用于占位符替换。
    • 还创建了一个 QTableWidget 来管理占位符和替换值的键值对。初始化了5行默认数据。

  • 控件毗连:

    • 点击“浏览”按钮会触发文件选择对话框,并通过信号槽机制毗连相应的函数(browseTemplateFile 和 browseOutputFile)。
    • 替换按钮 (replaceButton) 毗连到 replaceInWord 函数。

  • Word预览:

    • wordPreview 是一个 QAxWidget 控件,允许通过 ActiveX 技术与 Word 应用举行交互。它在 browseTemplateFile 中初始化并用于打开 Word 文件。

2. 浏览模板文件 (​browseTemplateFile​)



  • 打开文件对话框 (QFileDialog::getOpenFileName) 选择模板文件,文件路径体现在 templatePathEdit 中。
  • 使用 QAxWidget 来查询 Word 的应用对象 wordApp,并打开用户选择的文件。
  • 通过 Word.Application 创建 ActiveX 对象,加载模板文件并将其体现在打印预览模式。
  • 如果没有成功加载Word文件,会弹出错误提示。
3. 浏览输出文件 (​browseOutputFile​)



  • 打开保存文件对话框 (QFileDialog::getSaveFileName) 选择输出文件路径,并将路径体现在 outputPathEdit 中。
4. 替换操作 (​replaceInWord​)



  • 从 UI 中获取模板文件路径和输出文件路径,如果路径为空,弹出警告。
  • 获取表格中的占位符及其对应替换值,并构建一个 QMap 来存储这些键值对。
  • 调用 replaceMultiple 函数举行批量替换操作。
5. 替换逻辑 (​replaceMultiple​)



  • 文件存在性检查: 使用 QFile::exists 检查模板文件是否存在。
  • 打开Word文档: 使用 QAxObject 打开模板文件,获取 Selection 对象和 Find 对象来查找占位符。
  • 查找和替换: 遍历占位符的键值对,使用 Find.Execute 查找占位符,并通过 TypeText 替换为新内容。替换过程中使用了 while 循环,确保文档中全部的占位符都能被替换(即使它们重复出现)。
  • 保存文件: 替换完成后,使用 SaveAs 保存文件到指定的输出路径。
  • 关闭和退出: 完成替换后,关闭文档并退出Word应用。
总结

该程序使用 Qt 的 QAxObject 来与 Microsoft Word 举行交互,实现了以下功能:


  • 用户可以选择一个模板文件,并举行占位符的批量替换。
  • 用户可以设置替换后文档的保存路径。
  • 支持通过界面交互实现选择文件、体现信息以及实行替换操作。
  • 使用 QAxObject 实现了与 Word 的 COM 接口的交互,允许直接操作 Word 文档中的内容。

关于QGC地面站其它文章请点击这里:     QT Widget

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

泉缘泉

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表