-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavethread.cpp
More file actions
135 lines (123 loc) · 4.03 KB
/
savethread.cpp
File metadata and controls
135 lines (123 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "savethread.h"
#include <QFile>
#include <QDir>
#include <QDate>
#include <QDebug>
SaveThread::SaveThread(QObject *parent) : QThread(parent)
{
}
void SaveThread::SetInfo(const QString &name,
const QString &adminName,
const QString &adminId,
const QString &corpName,
const QString &corpId,
const QString &dept,
const QString &post,
const QString &phone, QFileInfo fileInfo)
{
mName = name;
mAdminName = adminName;
mAdminId = adminId;
mCorpName = corpName;
mCorpId = corpId;
mDept = dept;
mPost = post;
mPhone = phone;
mFileInfo = fileInfo;
}
bool SaveThread::Open(QString Dir)
{
// 新建一个word应用程序,并设置为可见
m_WordFile = new QAxObject();//取代QAxWidget,使其在子线程中可用
bool bFlag = m_WordFile->setControl("word.Application");
if (NULL == m_WordFile)
{
qDebug() << "word opening false";
// 尝试用wps打开
bFlag = m_WordFile->setControl("kwps.Application");
if (!bFlag)
{
return false;
}
}
//设置打开的word应用可见,建议一开始可见。这样你可以看到这个过程。
m_WordFile->setProperty("Visible", false);
// 获取所有的工作文档
Documents = m_WordFile->querySubObject("Documents");
if (NULL == Documents)
{
qDebug() << "documents opening false";
return false;
}
// 以文件template.dot为模版新建一个文档
Documents->dynamicCall("Add(QString)", Dir);
// 获取当前激活的文档
m_Document = m_WordFile->querySubObject("ActiveDocument");
qDebug() <<"ActiveDocument:"<< m_WordFile;
/*cout << *m_WordFile << endl;*/
if (NULL == m_Document)
{
qDebug() << "Active Doc opening false";
return false;
}
return true;
}
void SaveThread::SaveFile(QString dir)
{
if (dir.isEmpty()) {
qDebug() << "dir isEmpty";
return;
}
qDebug() << "begin edit" << dir;
// 将文件另存为outFileName,关闭工作文档,退出应用程序
m_Document->dynamicCall("SaveAs (const QString&)", dir);
m_Document->dynamicCall("Close (boolean)", true); //关闭文本窗口
m_WordFile->dynamicCall("Quit(void)"); //退出word
delete m_Document;
delete Documents;
delete m_WordFile;
emit SaveFinished(dir);
}
void SaveThread::InputText(const QString &posName, const QString &text)
{
//根据bookmark找到书签位置
QAxObject *bookmark_text = m_Document->querySubObject(QString("Bookmarks(%1)").arg(posName).toLocal8Bit().data());
//根据bookmark找到selection,也就是光标位置
bookmark_text->dynamicCall("Select(void)");
QAxObject* selection = m_WordFile->querySubObject("Selection");
if (!selection)
{
return;
}
selection->dynamicCall("TypeText(const QString&)", text);
}
void SaveThread::run()
{
QString odt = QDir::tempPath() + "/1.odt";
QFile::copy(":/odt/1.odt", odt);
QString time = QDate::currentDate().toString(tr("yyyy-MM-dd"));
if (Open(odt))
{
InputText("time1", time);
InputText("time2", time);
InputText("time3", time);
InputText("time4", time);
InputText("name1", mName);
InputText("name2", mName);
InputText("name3", mName);
InputText("name4", mName);
InputText("name5", mName);
InputText("name6", mName);
InputText("admin1", mAdminName);
InputText("admin2", mAdminName);
InputText("adminid1", mAdminId);
InputText("adminid2", mAdminId);
InputText("corp", mCorpName);
InputText("corpid", mCorpId);
InputText("dept", mDept);
InputText("post", mPost);
InputText("phone", mPhone);
SaveFile(mFileInfo.absoluteFilePath());
// QMessageBox::information(this, tr("完成"), tr("文件已存储在") + fileInfo.absoluteFilePath());
}
}