头文件
#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);
}
执行