当前位置:网站首页>Simple operation of QT axobject Library
Simple operation of QT axobject Library
2022-04-22 09:36:00 【GoKu~】
The header file
#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;
// operation excel
QAxObject *myExcel;
QAxObject *myWorks;
QAxObject *workBook;// workbook
QAxObject *mySheets;// Table cell hiccup
// operation word
QAxObject *myWord;
QAxObject *myDocs;
QAxObject *document;// file
QAxObject *paragraph;// Text paragraphs
};
#endif // DOOFFICE_H
Source file
#include "dooffice.h"
#include "ui_dooffice.h"
const static QString excelPath = "e:\\03_proj\\qt\\QT_ Small projects \\my_project\\officeApp\\myExcel.xls";
const static QString wordPath = "e:\\03_proj\\qt\\QT_ Small projects \\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");// Get the currently active workbook
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()
{
// Add a table
mySheets->dynamicCall("Add");
// Point to the active table
QAxObject *sheet = workBook->querySubObject("ActiveSheet");
// Name the table
sheet->setProperty("Name","hello excel");
// Point to c3 unit
QAxObject *cell = sheet->querySubObject("Range(QVariant,QVariant)","B3");
// Write content to the unit
QString inStr = ui->lineEdit->text();
cell->dynamicCall("SetValue(const QVariant&)",QVariant(inStr));
// Point to the second table
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(" complete "),tr(" The file has been saved "));
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");// Get the currently active workbook
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(" news "),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(" complete "),tr(" Document saved "));
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);// Division method
outStr = paragraph->property("Text").toString();
delete paragraph;
paragraph = nullptr;
document->dynamicCall("Close()");
myWord->dynamicCall("Quit()");
QMessageBox::information(this,tr(" news "),outStr);
ui->write_word_Bt->setEnabled(true);
ui->read_word_Bt->setEnabled(false);
}
perform

版权声明
本文为[GoKu~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220925522609.html
边栏推荐
- 拜登宣布再向乌克兰提供8亿美元安全援助
- L3-004 tumor diagnosis (30 points) (3D BFS)
- uni-app项目之 请求的二次封装
- Stream API 优化代码
- Command ‘yum‘ not found, but can be installed with: apt install yum
- L3-007 ladder map (30 points) (condition DIJ)
- Softing dataFEED OPC Suite:赋予工业设备物联网连接能力
- PHP & Laravel & 掌握 api 生成 token 的几种方式以及一些注意事项(坑)
- Easy to use screenshot software
- [go] program flow control
猜你喜欢

量化投资学习——介绍orderflow

Cloud native enthusiast weekly: looking for open source alternatives to netlify

MOS管驱动电路及注意事项-KIA MOS管

Online yaml to properties tool

Starting from the needs of popular science, doctor training and promotion of innovative medical equipment products, how does "baiyihua" layout medical visualization SaaS services?

如何保障缓存和数据库的一致性(超详细案例)

支持AUTOSAR Classic以及Adaptive平台的DEXT诊断数据库

电脑拆机清灰及机械硬盘安装记录

Kernel pwn 基础教程之 Heap Overflow

Zhezheng nail scanning code login
随机推荐
L2-031 deep into the tiger's den (25 points) (DFS)
问题解决:dpkg-deb: error: package name has characters that aren‘t lowercase alphanums or ‘-+.‘
L2-032 彩虹瓶 (25 分)(栈
P8资料大放送
L3-005 垃圾箱分布 (30 分)(dijkstar
yolov5 口罩检测flask部署
支持AUTOSAR Classic以及Adaptive平台的DEXT诊断数据库
电脑拆机清灰及机械硬盘安装记录
【SQL server】SQL 概览
openlayer中,svg图片无width如何修改大小
PHP & Laravel & 掌握 api 生成 token 的几种方式以及一些注意事项(坑)
MySQL多实例安装方法一
GS waveform analysis of depth resolved MOS transistor Kia MOS transistor
mysql主备配置 二进制日志问题
【有趣的编程题之适龄的朋友】(Leetcode原题详解)
Write a simple examination program to complete the interaction of questions and answers on the console. Questions are divided into single choice and multi choice.
Beyond iterm! Known as the next generation terminal artifact, powerful!
Redis 内存回收策略
Quantitative investment learning -- Introduction to orderflow
uni-app项目之 请求的二次封装