QAxObject .docx设置表格高 C++
时间: 2023-11-14 19:18:38 浏览: 165
高质量C++。doc
可以使用QAxObject来操作Microsoft Word文档中的表格。设置表格高度可以按照以下步骤进行:
1. 打开Word文档,获取表格对象。
```C++
QAxObject *word = new QAxObject("Word.Application");
QAxObject *documents = word->querySubObject("Documents");
QAxObject *document = documents->querySubObject("Open(const QString&)", "your_doc.docx");
QAxObject *tables = document->querySubObject("Tables");
QAxObject *table = tables->querySubObject("Item(int)", 1); // 获取第一个表格
```
2. 获取表格中的行和列数。
```C++
int rows = table->dynamicCall("Rows.Count").toInt();
int cols = table->dynamicCall("Columns.Count").toInt();
```
3. 设置表格行高。可以通过循环遍历每一行,然后设置行高。在本例中,我们设置每一行的高度为1厘米。
```C++
for (int i = 1; i <= rows; i++) {
QAxObject *row = table->querySubObject("Rows(int)", i);
row->setProperty("Height", 1.0);
}
```
4. 释放对象。
```C++
table->clear();
tables->clear();
document->dynamicCall("Close()");
documents->dynamicCall("Close()");
word->dynamicCall("Quit()");
```
完整代码如下:
```C++
QAxObject *word = new QAxObject("Word.Application");
QAxObject *documents = word->querySubObject("Documents");
QAxObject *document = documents->querySubObject("Open(const QString&)", "your_doc.docx");
QAxObject *tables = document->querySubObject("Tables");
QAxObject *table = tables->querySubObject("Item(int)", 1); // 获取第一个表格
int rows = table->dynamicCall("Rows.Count").toInt();
int cols = table->dynamicCall("Columns.Count").toInt();
for (int i = 1; i <= rows; i++) {
QAxObject *row = table->querySubObject("Rows(int)", i);
row->setProperty("Height", 1.0);
}
table->clear();
tables->clear();
document->dynamicCall("Close()");
documents->dynamicCall("Close()");
word->dynamicCall("Quit()");
```
阅读全文