参照:

  1. word操作的接口,我前期是对照博主的代码对应api学习的 QT操作Word,插入表格

  2. 用Qt操作Word文档

成果复制图中的表格到文档末尾

在这里插入图片描述

今天想实现一个需求,复制word模板内的表格,搜索各大引擎大部分是修改表格内部属性或是取值赋值之类的操作,没有整体复制一个表格的需求。
在开始之前应该先了解QT 中 QAxObjext 方法中 querySubObjectdynamicCall,通俗点来说前者是调用方法或者调用属性,因为时间太长相关知识我也是囫囵吞枣,理解不太清楚。
操作word需要这个函数调用COM API的函数,相关的函数我是查找的Microsoft,因为这个的调用是从Microsoft.Office.Interop.Word.dll出来的,像我一样的可以直接搜c#代码,然后稍微变化一下即可。

下面进入正题,复制表格需要使用Copy()Paste()两个函数,就像我们操作word表格时的复制粘贴一样,ps:这一步我没有添加图片,所以没有发现什么问题

  1. 复制表格
1
2
3
4
5
6
7
8
9
10
11
12
13
QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
if(nullptr == table)
return;
table->dynamicCall("Select()");
word->querySubObject("Selection")->dynamicCall("Copy()");
//移动光标到文档末尾
QAxObject* selec = word->querySubObject("Selection");
QVariantList params2;
params2.append(6);
params2.append(0);
selec->dynamicCall("Endof(QVariant&,QVariant&",params2);

word->querySubObject("Selection")->dynamicCall("Paste()");

加上一个从其他对峙对照的c#代码,这样看起来更容易理解,来源C# 在word文档中复制表格并粘帖到下一页中

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
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
//显示word文档
oWord.Visible = true;
//取得word文件模板
object fileName = System.Windows.Forms.Application.StartupPath + "\word.doc";
//根据模板生成一个新文档,相当于另存为
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);

//复制第一个表格
oDoc.Tables[1].Select();
oWord.Selection.Copy();

//在这里操作表格中的文本
oDoc.Tables[1].Cell(1, 1).Range.Text = "这是第一个表格";

//下一页
object mymissing = System.Reflection.Missing.Value;
object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWord.Selection.EndKey(ref myunit, ref mymissing);
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
oWord.Selection.InsertBreak(ref pBreak);

//粘贴第一个表格
oWord.Selection.Paste();

oDoc.Tables[2].Cell(1, 1).Range.Text = "这是第二个表格";

所有的创建类代码,可以实现根据模板docx/dot复制表格,如果我们想要修改表格内的值就不能使用书签法了,需要单独对这个列表模板cell进行赋值,淡出创建一个方法类即可,这个地方可以提前创建一个工厂

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
    //put a box and set: star out put
QMessageBox::warning(this,"Start","Start Output",QMessageBox::Yes);
//create new doc application
QAxObject* word = new QAxObject();
bool flag = word->setControl("Word.Application");
if(!flag){
flag = word->setControl("kwps.Application");
if(!flag)
return;
}

//QAxWidget* word = new QAxWidget("Word.Application",0,Qt::MSWindowsOwnDC);
//set unvisible
word->setProperty("Visible",false);
word->setProperty("DisplayAlerts",false);
//get all document
QAxObject* documents = word->querySubObject("Documents");
//use document2.dot template to create a new document
documents->dynamicCall("Add(QString)",QString::fromLocal8Bit("C:/Users/****/Desktop/4444.docx"));
//get active document
QAxObject* document = word->querySubObject("ActiveDocument");

//get bookmark which name is 测试 in this document
QAxObject* bookmark_test = document->querySubObject("BookMarks(QVariant)","测试");
//select target bookmark and set value
if(!bookmark_test->isNull()){
QString sText = ui->pcEdit->text();
qDebug()<<sText;
bookmark_test->dynamicCall("Select(void)");
bookmark_test->querySubObject("Range")->setProperty("Text",sText);
}
// insert table
// int row = 14;
// int col = 0;
int tableIndex = 4;
QAxObject* tables = document->querySubObject("Tables");
if(nullptr == tables)
return;
QAxObject* selection = word->querySubObject("Selection");
if(nullptr == selection)
return;
QAxObject* range = selection->querySubObject("Range");
if(nullptr == range)
return;

QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
if(nullptr == table)
return;
int table_count = tables->dynamicCall("Count").toInt();
qDebug()<<table_count;

QAxObject* cols = table->querySubObject("Rows");
int count = cols->dynamicCall("Count").toInt();
qDebug()<<count;

/*QAxObject* selec = word->querySubObject("Selection");
QVariantList params2;
params2.append(6);
params2.append(0);
selec->dynamicCall("Endof(QVariant&,QVariant&)",params2);*/


table->dynamicCall("Select()");

word->querySubObject("Selection")->dynamicCall("Copy()");

QAxObject* selec = word->querySubObject("Selection");
QVariantList params2;
params2.append(6);
params2.append(0);
selec->dynamicCall("Endof(QVariant&,QVariant&",params2);

qDebug()<<"selection tables count:"<<selection->querySubObject("Tables")->dynamicCall("Count").toInt();
QVariantList t_params;
t_params.append("WdRecoveryType.wdUseDestinationStylesRecovery");
//selection->dynamicCall("PasteAndFormat(WdRecoveryType)",t_params);

word->querySubObject("Selection")->dynamicCall("Paste()");

//this table index is test value
QAxObject* t_table = tables->querySubObject("Item(int)",5);
QVariantList cellPara;
cellPara.append(0);
cellPara.append(0);
t_table->querySubObject("Cell(QVariant&,QVariant&)",cellPara)->querySubObject("Range")->dynamicCall("SetText(QString)",_unit->getProblemSymbol());





//save file as doc|docx
QString path_save = QFileDialog::getSaveFileName(this,"Save","../","word(*doc)");
if(path_save.isEmpty() == true){
return;
}
document->dynamicCall("SaveAs(const QString&))",QDir::toNativeSeparators(path_save));
document->dynamicCall("Close (boolean)",false);
word->dynamicCall("Quit()");
QMessageBox::warning(this,"Done","File saved",QMessageBox::Yes);

插入新的一页

1
m_word->querySubObject("Selection")->dynamicCall("InsertNewPage()");