Qt 生成word文档报告

报表模板:

 

代码:

//新建一个word应用程序,并设置为不可见

    QAxWidget *word = new QAxWidget("Word.Application",0,Qt::MSWindowsOwnDC);

    word->setProperty("Visible",false);

 

    //获取所有的工作文档

    QAxObject *documents = word->querySubObject("Documents");

    //以文件bb.doc为模板新建一个文档

    documents->dynamicCall("Add(QString)",QString("E:/QtCode/newExample/wordBaoBiao/bb.doc"));

 

    //获取当前激活的文档

    QAxObject *document = word->querySubObject("ActiveDocument");

 

    //获取文档中的名字为code的标签

    QString code = "code";

    QAxObject *bookmark_text = document->querySubObject("Bookmarks(QVariant)", code);

 

    //选中标签,将字符insertText插入到标签位置

    if(!bookmark_text->isNull())

    {

        bookmark_text->dynamicCall("Select(void)");      ///选中要选中的区域

        bookmark_text->querySubObject("Range")->setProperty("Text","insertText");  ///进行替换操作

    }

 

    QAxObject *bookmark_ndvi = document->querySubObject("Bookmarks(QVariant)", "ndvi");

    if ( !bookmark_ndvi->isNull())

    {

        bookmark_ndvi->dynamicCall("Select(void)");

        bookmark_ndvi->querySubObject("Range")->setProperty("Text", "ndvi");

    }

 

    //获取文档中名字为pic的标签

    QAxObject *bookmark_pic = document->querySubObject("Bookmarks(QVariant)", "pic");

 

    //选中标签,将图片插入到标签的位置

    if(!bookmark_pic->isNull())

    {

        bookmark_pic->dynamicCall("Select(void)");

        QAxObject *inlineshapes = document->querySubObject("InlineShapes");

        inlineshapes->dynamicCall("AddPicture(const QString&)","E:\\123.jpg");  ///路径必须为windo路径格式

    }

 

    //将文件另存为docbyqt.doc,关闭工作文档,退出应用程序

    document->dynamicCall("SaveAs(const QString&)",QString("E:/testword.doc"));

    document->dynamicCall("Close(boolean)",false);

    word->dynamicCall("Quit()");

 

    delete bookmark_text;

    delete bookmark_pic;

    delete document;

    delete documents;

    delete word;

 

最后效果图:

Qt生成Word文档,你可以使用QtQTextDocument类和QTextCursor类来完成。下面是一个简单的示例代码,演示如何创建一个带有文本和表格的Word文档: ```cpp #include <QApplication> #include <QTextDocument> #include <QTextCursor> #include <QTextTable> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建一个QTextDocument对象 QTextDocument document; // 创建一个QTextCursor对象,并将其绑定到文档上 QTextCursor cursor(&document); // 在文档中插入文本 cursor.insertText("这是一个示例文档\n\n"); // 在文档中插入表格 QTextTableFormat tableFormat; tableFormat.setBorder(1); tableFormat.setCellPadding(10); tableFormat.setAlignment(Qt::AlignHCenter); QTextTable *table = cursor.insertTable(3, 2, tableFormat); table->cellAt(0, 0).firstCursorPosition().insertText("表头1"); table->cellAt(0, 1).firstCursorPosition().insertText("表头2"); table->cellAt(1, 0).firstCursorPosition().insertText("内容1"); table->cellAt(1, 1).firstCursorPosition().insertText("内容2"); table->cellAt(2, 0).firstCursorPosition().insertText("内容3"); table->cellAt(2, 1).firstCursorPosition().insertText("内容4"); // 保存文档为Word格式 QString fileName = "example.docx"; QFile file(fileName); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextDocumentWriter writer(&file, "plaintext"); writer.setFormat("odf"); writer.write(&document); } file.close(); return 0; } ``` 这段代码会创建一个带有文本和表格的Word文档,并将其保存为"example.docx"文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值