如何使用 QT5 将新行添加到现有的 MS Word 表格?

问题描述 投票:0回答:1

一般来说,我尝试将数据从

QTableView
传输到准备好的
.docx file
,我将在其中使用占位符来替换我需要的数据。

此刻,我停止向 MS Word 文档中的现有表格添加新行,但我无法理解语法(它一直说“没有这样的属性”,具有不同的功能)。 唯一正确的工作是获取文档中的表格数量。

QAxObject *wApp = new QAxObject("Word.Application");
auto docs = wApp->querySubObject("Documents");
auto doc  = docs->querySubObject("Open(QString)","C:\\pathToFile\\qwe.docx");
if (doc == nullptr) {
    doc->dynamicCall("Close()");
    wApp->dynamicCall("Quit()");
    QMessageBox::information(this, "File opening", "File not found");
    return;
}

auto active  = wApp->querySubObject("ActiveDocument()");
auto tables  = active->querySubObject("Tables");

qDebug() << tables->dynamicCall("Count()");

// doc->dynamicCall("Save()");
doc->dynamicCall("Close()");
wApp->dynamicCall("Quit()");
delete wApp;

我也尝试获取表中的行数,但它输出 0。

auto table = tables->querySubObject("Select(int)", 1);
qDebug() << table->dynamicCall("Rows");

错误示例:

QAxBase::dynamicCallHelper: Table: No such property in {000209ff-0000-0000-c000-000000000046} [Microsoft Word Application]
    Candidates are:
        TaskPanes
        Tasks
        Templates
        Top
QAxBase::dynamicCallHelper: Type(QString): No such property in  [unknown]
    Candidates are:
        TableDirection
        Tables
        Title
        TopPadding
c++ qt ms-word qt5
1个回答
0
投票

最后一行之后添加了新行

    QAxObject *wApp = new QAxObject("Word.Application");
    auto docs       = wApp->querySubObject("Documents");
    auto doc        = docs->querySubObject("Open(QString)", "C:\\pathToFile\\qwe.docx");
    if (doc == nullptr) {
        QMessageBox::information(this, "File opening", "File not found");
        wApp->dynamicCall("Quit()");
        delete wApp;
        return;
    }

    auto active = wApp->querySubObject("ActiveDocument()");
    auto tables = active->querySubObject("Tables");
    auto table  = tables->querySubObject("Item(int)", 1); // First table in the document

    auto rows = table->querySubObject("Rows");
    rows->dynamicCall("Add()");

    doc->dynamicCall("Save()");
    doc->dynamicCall("Close()");
    wApp->dynamicCall("Quit()");
    delete wApp;
© www.soinside.com 2019 - 2024. All rights reserved.