#include "CWordOperation.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <thread>
CWordOperation::CWordOperation(QObject *parent)
: QObject(parent)
, m_pWord(nullptr)
, m_pWordDoc(nullptr)
, m_bOpen(false)
, m_bFristParagraph(true)
{
}
CWordOperation::~CWordOperation()
{
}
bool CWordOperation::openAsCreateFileMethod(const QString &strFilePath, bool bVisable)
{
if (m_bOpen == true) {
return true;
}
clearValue();
m_pWord = new QAxObject();
bool bFlag = m_pWord->setControl(u8"Word.Application"); // 连接office的word控件
if (bFlag == false) {
// 如果连接失败,则连接wps
bFlag = m_pWord->setControl(u8"WPS.Application"); // 连接wps控件
if (bFlag == false) {
return false; // 连接word控件操作失败
}
}
// 设置word界面不现实
m_pWord->setProperty("Visible", true); // true表示显示word界面,false表示不现实word界面
// 获取所有的工作文档
QAxObject *document = m_pWord->querySubObject("Documents");
if (document == nullptr) {
return false; // 获取工作文档失败
}
do {
if (strFilePath.isEmpty() == false) {
QFile file(strFilePath);
if (file.exists()) {
document->dynamicCall("Add(const QString&)", strFilePath); // 以strFilePath为模板创建一个word文档
break;
}
}
document->dynamicCall("Add(void)"); // 创建一个空的word文档
} while (0);
// 获取当前激活的文档
m_pWordDoc = m_pWord->querySubObject(u8"ActiveDocument");
if (m_pWordDoc == nullptr) {
m_bOpen = false;
}
else {
m_bOpen = true;
}
return m_bOpen;
}
bool CWordOperation::openFile(const QString & strFilePath, bool bVisable)
{
if (m_bOpen == true) {
return true;
}
clearValue();
m_pWord = new QAxObject();
bool bFlag = m_pWord->setControl(u8"Word.Application"); // 连接office的word控件
if (bFlag == false) {
// 如果连接失败,则连接wps
bFlag = m_pWord->setControl(u8"WPS.Application"); // 连接wps控件
if (bFlag == false) {
return false; // 连接word控件操作失败
}
}
// 设置word界面不现实
m_pWord->setProperty("Visible", bVisable); // true表示显示word界面,false表示不现实word界面
// 获取所有的工作文档
QAxObject *document = m_pWord->querySubObject("Documents");
if (document == nullptr) {
return false; // 获取工作文档失败
}
QFile file(strFilePath);
if (file.exists() == false) {
clearValue();
qDebug() << u8"openFile is error: the file is not exist";
return false;
}
m_pWordDoc = document->querySubObject(u8"Open(QString)", strFilePath);
if (m_pWordDoc == nullptr) {
clearValue();
qDebug() << u8"openFile is error: Word Doc is null";
return false;
}
m_bOpen = true;
return true;
}
void CWordOperation::close()
{
if (m_pWord != nullptr) {
m_pWord->setProperty("DisplayAlerts", 0); // 不显示警告或者错误
}
if (m_pWordDoc != nullptr) {
m_pWordDoc->dynamicCall("Close(bool)", true); // 关闭文档窗口
delete m_pWordDoc;
m_pWordDoc = nullptr;
}
if (m_pWord != nullptr) {
m_pWord->dynamicCall("Quit()");
delete m_pWord;
m_pWord = nullptr;
}
m_bOpen = false;
}
void CWordOperation::closeFalse()
{
if (m_pWord != nullptr) {
m_pWord->setProperty("DisplayAlerts", 0); // 不显示警告或者错误
}
if (m_pWordDoc != nullptr) {
m_pWordDoc->dynamicCall("Close(bool)", false); // 关闭文档窗口
delete m_pWordDoc;
m_pWordDoc = nullptr;
}
if (m_pWord != nullptr) {
m_pWord->dynamicCall("Quit()");
delete m_pWord;
m_pWord = nullptr;
}
m_bOpen = false;
}
void CWordOperation::saveAs(const QString & strFilePath)
{
m_pWordDoc->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(strFilePath));
}
void CWordOperation::save()
{
m_pWordDoc->dynamicCall("Save()", true); // 这里最好使用true,会有提示框。否则Visible设置为false时不会弹出保存位置,就会导致卡顿。
}
bool CWordOperation::insertText(const QString & strText)
{
// 获取光标所在位置
QAxObject *selection = m_pWord->querySubObject("Selection");
if (selection == nullptr) {
return false;
}
selection->dynamicCall("TypeText(const QString&)", strText);
return true;
}
int CWordOperation::insertParagraphsAndTable(const QString & filePath)
{
// 创建Word应用程序实例
QAxObject* word = new QAxObject("Word.Application");
if (!word) {
qDebug() << "无法创建Word应用程序实例";
return -1;
}
// 添加一个新文档
QAxObject* documents = word->querySubObject("Documents");
QAxObject* document = documents->querySubObject("Add()");
if (!document) {
qDebug() << "无法添加新文档";
return -1;
}
// 获取文档的末尾 Range 对象
QAxObject* range = document->querySubObject("Range()");
if (!range) {
qDebug() << "无法获取文档的末尾 Range 对象";
return -1;
}
// 插入一个新段落并设置首行缩进
QAxObject* paragraphFormat = range->querySubObject("ParagraphFormat");
if (paragraphFormat) {
paragraphFormat->dynamicCall("SetFirstLineIndent(double)", 21);
}
// 在新段落中插入文本
range->dynamicCall("InsertAfter(const QString&)", u8"这是新段落的文本");
QAxObject* paragraphs = document->querySubObject("Paragraphs");
if (!paragraphs) {
qDebug() << "无法获取文档的段落集合";
return -1;
}
// 现在再次插入一个新的段落并设置格式
QAxObject* newParagraph = paragraphs->querySubObject("Add()");
if (newParagraph) {
QAxObject* newParagraphFormat = newParagraph->querySubObject("Format");
if (newParagraphFormat) {
newParagraphFormat->dynamicCall("SetFirstLineIndent(double)", 21);
}
QAxObject* newRange = newParagraph->querySubObject("Range");
if (newRange) {
newRange->dynamicCall("InsertAfter(const QString&)", u8"这是第1个段落"); // 插入文本并添加回车
}
delete newParagraphFormat;
delete newRange;
}
QAxObject* paragraphs2 = document->querySubObject("Paragraphs");
if (!paragraphs2) {
qDebug() << "无法获取文档的段落集合";
return -1;
}
// 现在再次插入一个新的段落并设置格式
QAxObject* newParagraph2 = paragraphs2->querySubObject("Add()");
if (newParagraph2) {
QAxObject* newParagraphFormat = newParagraph->querySubObject("Format");
if (newParagraphFormat) {
newParagraphFormat->dynamicCall("SetFirstLineIndent(double)", 21);
}
QAxObject* newRange = newParagraph->querySubObject("Range");
if (newRange) {
newRange->dynamicCall("InsertAfter(const QString&)", u8"这是第2个段落"); // 插入文本并添加回车
}
delete newParagraphFormat;
delete newRange;
}
// 保存并关闭文档
document->dynamicCall("SaveAs2(const QString&)", "path/to/your/document.docx");
document->dynamicCall("Close()");
// 退出Word应用程序
word->dynamicCall("Quit()");
// 清理资
测控系统集成
- 粉丝: 691
- 资源: 60
最新资源
- js-leetcode题解之158-read-n-characters-given-read4-ii-call
- js-leetcode题解之157-read-n-characters-given-read4.js
- js-leetcode题解之156-binary-tree-upside-down.js
- js-leetcode题解之155-min-stack.js
- js-leetcode题解之154-find-minimum-in-rotated-sorted-array-ii.js
- js-leetcode题解之153-find-minimum-in-rotated-sorted-array.js
- js-leetcode题解之152-maximum-product-subarray.js
- js-leetcode题解之151-reverse-words-in-a-string.js
- js-leetcode题解之150-evaluate-reverse-polish-notation.js
- js-leetcode题解之149-max-points-on-a-line.js
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈