QAxObject .docx设置表格页边距 C++
时间: 2023-11-14 11:21:36 浏览: 178
使用QAxObject操作Word.docx
可以使用 QAxObject 来操作 Word 文档进行设置表格页边距的操作,具体代码如下:
```c++
QAxObject* wordApplication = new QAxObject("Word.Application");
wordApplication->setProperty("Visible", false);
QAxObject* documents = wordApplication->querySubObject("Documents");
QAxObject* document = documents->querySubObject("Open(const QString&)", "test.docx");
QAxObject* tables = document->querySubObject("Tables");
QAxObject* table = tables->querySubObject("Item(int)", 1);
QAxObject* tableBorders = table->querySubObject("Borders");
tableBorders->setProperty("InsideLineStyle", "wdLineStyleSingle");
QAxObject* tablePreferredWidth = table->querySubObject("PreferredWidth");
tablePreferredWidth->setProperty("Type", "wdPreferredWidthPoints");
tablePreferredWidth->setProperty("Value", 468);
QAxObject* rows = table->querySubObject("Rows");
QAxObject* row1 = rows->querySubObject("Item(int)", 1);
QAxObject* row1Height = row1->querySubObject("Height");
row1Height->setProperty("Rule", "wdRowHeightExactly");
row1Height->setProperty("Height", 12);
QAxObject* cells = table->querySubObject("Cells");
QAxObject* cell1 = cells->querySubObject("Item(int,int)", 1, 1);
QAxObject* cell1Range = cell1->querySubObject("Range");
QAxObject* cell1Paragraphs = cell1Range->querySubObject("Paragraphs");
QAxObject* cell1Paragraph1 = cell1Paragraphs->querySubObject("Item(int)", 1);
QAxObject* cell1Paragraph1Range = cell1Paragraph1->querySubObject("Range");
cell1Paragraph1Range->setProperty("Text", "Name");
QAxObject* cell2 = cells->querySubObject("Item(int,int)", 1, 2);
QAxObject* cell2Range = cell2->querySubObject("Range");
QAxObject* cell2Paragraphs = cell2Range->querySubObject("Paragraphs");
QAxObject* cell2Paragraph1 = cell2Paragraphs->querySubObject("Item(int)", 1);
QAxObject* cell2Paragraph1Range = cell2Paragraph1->querySubObject("Range");
cell2Paragraph1Range->setProperty("Text", "Age");
QAxObject* cell3 = cells->querySubObject("Item(int,int)", 1, 3);
QAxObject* cell3Range = cell3->querySubObject("Range");
QAxObject* cell3Paragraphs = cell3Range->querySubObject("Paragraphs");
QAxObject* cell3Paragraph1 = cell3Paragraphs->querySubObject("Item(int)", 1);
QAxObject* cell3Paragraph1Range = cell3Paragraph1->querySubObject("Range");
cell3Paragraph1Range->setProperty("Text", "Gender");
QAxObject* tableRange = table->querySubObject("Range");
tableRange->setProperty("Style", "Table Grid");
tableRange->setProperty("ParagraphFormat.Alignment", "wdAlignParagraphCenter");
// 设置表格页边距
QAxObject* pageSetup = document->querySubObject("PageSetup");
pageSetup->setProperty("TopMargin", 20);
pageSetup->setProperty("BottomMargin", 20);
pageSetup->setProperty("LeftMargin", 20);
pageSetup->setProperty("RightMargin", 20);
document->dynamicCall("Save()");
document->dynamicCall("Close()");
wordApplication->dynamicCall("Quit()");
```
在这个示例代码中,我们首先创建一个 Word.Application 对象,并打开一个名为 test.docx 的文档。然后,我们使用 QAxObject API 操作该文档中的表格,设置表格样式和内容。最后,我们使用 PageSetup 对象来设置表格的页边距。
需要注意的是,这个示例中的代码可能需要根据实际情况进行修改,比如具体的表格位置、大小等参数。
阅读全文