QT AxObject库的简单操作

这个博客介绍了如何使用Qt库中的QAxObject来操作Microsoft Office应用程序,包括创建、读取和写入Excel电子表格以及Word文档。通过动态调用对象的方法,实现了向Excel单元格写入和读取数据,以及在Word文档中插入和读取文本。示例代码详细展示了具体的实现步骤。
摘要由CSDN通过智能技术生成

 头文件

#ifndef DOOFFICE_H
#define DOOFFICE_H

#include <QMainWindow>
#include <QAxObject>
#include <QMessageBox>

QT_BEGIN_NAMESPACE
namespace Ui { class doOffice; }
QT_END_NAMESPACE

class doOffice : public QMainWindow
{
    Q_OBJECT

public:
    doOffice(QWidget *parent = nullptr);
    ~doOffice();

private slots:

    void on_write_excel_Bt_clicked();

    void on_read_excel_Bt_clicked();

    void on_write_word_Bt_clicked();

    void on_read_word_Bt_clicked();

private:
    Ui::doOffice *ui;
    //操作excel
    QAxObject *myExcel;
    QAxObject *myWorks;
    QAxObject *workBook;//工作簿
    QAxObject *mySheets;//表格单元嗝

    //操作word
    QAxObject *myWord;
    QAxObject *myDocs;
    QAxObject *document;//文档
    QAxObject *paragraph;//文本段落


};
#endif // DOOFFICE_H

源文件

#include "dooffice.h"
#include "ui_dooffice.h"

const static QString excelPath = "e:\\03_proj\\qt\\QT_小项目\\my_project\\officeApp\\myExcel.xls";
const static QString wordPath = "e:\\03_proj\\qt\\QT_小项目\\my_project\\officeApp\\myWord.doc";

doOffice::doOffice(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::doOffice)
{
    ui->setupUi(this);
    //excel
    myExcel = new QAxObject("Ket.Application");
    myWorks = myExcel->querySubObject("WorkBooks");
    myWorks->dynamicCall("Add");
    workBook = myExcel->querySubObject("ActiveWorkBook");//获取当前活动的工作簿
    mySheets = workBook->querySubObject("Sheets");

    //word
    myWord = new QAxObject("Word.Application");
    myDocs = myWord->querySubObject("Documents");
    myDocs->dynamicCall("Add(void)");
    document = myWord->querySubObject("ActiveDocument");
    paragraph = myWord->querySubObject("Selection");

}

doOffice::~doOffice()
{
    delete ui;
}

void doOffice::on_write_excel_Bt_clicked()
{
    //添加一个表
    mySheets->dynamicCall("Add");
    //指向活动的表格
    QAxObject *sheet = workBook->querySubObject("ActiveSheet");
    //给表格命名
    sheet->setProperty("Name","hello excel");
    //指向c3单元
    QAxObject *cell = sheet->querySubObject("Range(QVariant,QVariant)","B3");
    //向单元写入内容
    QString inStr = ui->lineEdit->text();
    cell->dynamicCall("SetValue(const QVariant&)",QVariant(inStr));
    //指向第二个表格
    sheet = mySheets->querySubObject("Item(int)",2);
    sheet->setProperty("Name","hello QT");

    cell = sheet->querySubObject("Range(QVariant,QVariant)","B5");
    cell->dynamicCall("SetValue(const QVariant &)",QVariant("Hi i love Qt!"));

    workBook->dynamicCall("SaveAs(const QString &)",excelPath);
    workBook->dynamicCall("Close()");
    myExcel->dynamicCall("Quit()");

    QMessageBox::information(this,tr("完毕"),tr("文件已保存"));
    ui->write_excel_Bt->setEnabled(false);
    ui->read_excel_Bt->setEnabled(true);

}


void doOffice::on_read_excel_Bt_clicked()
{
    myExcel->querySubObject("Ket.Application");
    myWorks = myExcel->querySubObject("WorkBooks");
    myWorks->dynamicCall("Open(const QString &)",excelPath);

    workBook = myExcel->querySubObject("ActiveWorkBook");//获取当前活动的工作簿
    mySheets = workBook->querySubObject("WorkSheets");

    QAxObject *sheet = workBook->querySubObject("Sheets(int)",1);
    QAxObject *cell = sheet->querySubObject("Range(QVariant,QVariant)","B3");
    QString outStr = cell->dynamicCall("Value2()").toString();
    ui->read_msg->setText(outStr);

    sheet = workBook->querySubObject("Sheets(int)",2);
    cell = sheet->querySubObject("Range(QVariant,QVariant)","B5");
    outStr = cell->dynamicCall("Value2()").toString();

    workBook->dynamicCall("Close()");
    myExcel->dynamicCall("Quit()");
    QMessageBox::information(this,tr("消息"),outStr);
    ui->write_excel_Bt->setEnabled(true);
    ui->read_excel_Bt->setEnabled(false);


}


void doOffice::on_write_word_Bt_clicked()
{
    QString inStr = ui->lineEdit_2->text();
    paragraph->dynamicCall("TypeText(const QString&)",inStr);
    paragraph->dynamicCall("TypeText(const QVariant&)",QVariant("\nhello word"));

    document->dynamicCall("SaveAs(const QString&)",wordPath);

    delete paragraph;
    paragraph = nullptr;
    document->dynamicCall("Close()");
    myWord->dynamicCall("Quit()");
    QMessageBox::information(this,tr("完毕"),tr("文档已保存"));
    ui->write_word_Bt->setEnabled(false);
    ui->read_word_Bt->setEnabled(true);
}


void doOffice::on_read_word_Bt_clicked()
{
    myWord = new QAxObject("Word.Application");
    myDocs = myWord->querySubObject("Documents");
    myDocs->dynamicCall("Open(const QString&)",wordPath);

    document = myWord->querySubObject("ActiveDocument");
    paragraph = document->querySubObject("Range()");
    QString outStr = paragraph->property("Text").toString();
    ui->read_msg_2->setText(outStr.split("1").at(0));
    paragraph = document->querySubObject("Range(QVariant,QVariant)",14,30);//   分割法

    outStr = paragraph->property("Text").toString();
    delete paragraph;
    paragraph = nullptr;
    document->dynamicCall("Close()");
    myWord->dynamicCall("Quit()");
    QMessageBox::information(this,tr("消息"),outStr);
    ui->write_word_Bt->setEnabled(true);
    ui->read_word_Bt->setEnabled(false);

}

执行

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值