Qt 使用 QAxObject 类读写 Excel 文档
因为工作需要,最近研究了一下使用 Qt 的 QAxObject 类实现对 Excel 文档的简单读写,查阅了很多资料后,自己总结了一个 ExcelHandle 类出来,下面是实验效果图以及部分核心源码,详细源码请见文章底部
,代码注释还算比较详细,细致看看基本都还是很好懂的,如果不知道函数是来干嘛的,可以直接 copy 函数名称到有道翻译,按大写字母隔开即可。
实验环境
实验核心源码
excelHandle.h 文件
#ifndef EXCELHANDLE_H
#define EXCELHANDLE_H#include
#include
#include
#include
#include
#include
#include class ExcelHandle : public QObject {Q_OBJECT
public:explicit ExcelHandle(QObject *parent &#61; nullptr);~ExcelHandle();void init(bool showWindow, bool showAlert, QString title);void save(QString url);void exit();void openBook(QString url);void openSheet(int n);void setSheetName(QString name);void addSheet(QString name);void changeSheet(QString name);void changeSheet(int index);void deleteSheet(QString name);void deleteSheet(int index);void writeData(int col, int row, QVariant data);void writeDataSet(QString start_Addr, QList<QList<QVariant>> data_Set);QList<QList<QVariant>> readData();private:QAxObject *excel,*workbooks,*workbook,*sheets,*sheet,*usedRange;QString getEndAddr(QString start_Addr, int col, int row);
};#endif
excelHandle.cpp 文件
#include "excelHandle.h"ExcelHandle::ExcelHandle(QObject *parent) :QObject(parent) {}ExcelHandle::~ExcelHandle() {exit();delete excel;
}void ExcelHandle::init(bool showWindow, bool showAlert, QString title) {excel &#61; new QAxObject;excel -> setControl("Excel.Application");excel -> dynamicCall("SetVisible(bool)", showWindow);excel -> setProperty("DisplayAlerts", showAlert);excel -> setProperty("Caption", title);
}
void ExcelHandle::save(QString url) {workbook -> dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(url));
}
void ExcelHandle::exit() {workbooks -> dynamicCall("Close()");excel -> dynamicCall("Quit()");
}
void ExcelHandle::openBook(QString url) {workbooks &#61; excel -> querySubObject("Workbooks"); if(QFileInfo(url).exists()) {workbooks -> dynamicCall("Open(const QString&)", QString(url));}else {workbooks -> dynamicCall("Add");}workbook &#61; excel -> querySubObject("ActiveWorkBook");
}void ExcelHandle::openSheet(int n) {sheets &#61; workbook -> querySubObject("WorkSheets");sheet &#61; sheets -> querySubObject("Item(int)", n);
}
void ExcelHandle::setSheetName(QString name) {sheet -> setProperty("Name", name);
}
void ExcelHandle::addSheet(QString name) {int sheet_count &#61; sheets -> property("Count").toInt();QAxObject *last_sheet &#61; sheets -> querySubObject("Item(int)", sheet_count);QAxObject *work_sheet &#61; sheets -> querySubObject("Add(QVariant)", last_sheet -> asVariant());last_sheet -> dynamicCall("Move(QVariant)", work_sheet -> asVariant());work_sheet -> setProperty("Name", QString("%1").arg(name));
}void ExcelHandle::changeSheet(QString name) {int count &#61; sheets -> property("Count").toInt();for(int i &#61; 1; i <&#61; count; i &#43;&#43;) {sheet &#61; sheets -> querySubObject("Item(int)", i);if(sheet -> property("Name").toString() &#61;&#61; name) { break; }}
}void ExcelHandle::changeSheet(int index) {int count &#61; sheets -> property("Count").toInt();index &#61; index > count ? count : index;sheet &#61; sheets -> querySubObject("Item(int)", index);
}
void ExcelHandle::deleteSheet(QString name) {changeSheet(name);sheet -> dynamicCall("delete");
}void ExcelHandle::deleteSheet(int index) {changeSheet(index);sheet -> dynamicCall("delete");
}void ExcelHandle::writeData(int col, int row, QVariant data) {QAxObject *pRange &#61; sheet -> querySubObject("Cells(int,int)", col, row);pRange -> dynamicCall("Value", data);
}QString ExcelHandle::getEndAddr(QString start_Addr, int col, int row) {int temp_col &#61; 0, temp_row, temp_base &#61; 1;QString temp_str;QRegExp reg_exp_word("^[a-zA-Z]&#43;"),reg_exp_num("[0-9]&#43;$");reg_exp_word.indexIn(start_Addr);temp_str &#61; reg_exp_word.cap(0);for(int i &#61; 0; i < temp_str.length(); i &#43;&#43;) {temp_col &#43;&#61; (temp_str[temp_str.length() - i - 1].unicode() - &#39;A&#39; &#43; 1) * temp_base;temp_base *&#61; 26;}temp_col &#43;&#61; col;temp_str &#61; "";while(temp_col > 1) {temp_str &#61; QString("%1%2").arg(static_cast<char>(temp_col % 26 &#43; &#39;A&#39; - 1)).arg(temp_str);temp_col /&#61; 26;}reg_exp_num.indexIn(start_Addr);temp_row &#61; reg_exp_num.cap(0).toInt();temp_row &#43;&#61; row;return QString("%1%2").arg(temp_str).arg(temp_row);
}void ExcelHandle::writeDataSet(QString start_Addr, QList<QList<QVariant>> data_Set) {QList<QVariant> res, temp;for(int i &#61; 0; i < data_Set.length(); i &#43;&#43;) {temp.clear();for(int j &#61; 0; j < data_Set[0].length(); j &#43;&#43;) {temp.append(data_Set[i][j]);}res.append(QVariant(temp));}QString end_addr &#61; getEndAddr(start_Addr, data_Set[0].length() - 1, data_Set.length() - 1);QString range &#61; QString("range(%1:%2)").arg(start_Addr).arg(end_addr);QAxObject *usedRange &#61; sheet -> querySubObject(range.toUtf8().data());usedRange -> setProperty("Value", QVariant(res));
}QList<QList<QVariant>> ExcelHandle::readData() {usedRange &#61; sheet -> querySubObject("UsedRange");QVariant v &#61; usedRange -> dynamicCall("Value");QList<QVariant> vl &#61; v.toList();QList<QList<QVariant>> vll;for(int i &#61; 0; i < vl.size(); i &#43;&#43;) {vll.append(vl[i].toList());}return vll;
}
使用方式(该部分代码仅供参考)
QString url &#61; "E:/test11.xlsx";
QList<QVariant> vl;
QList<QList<QVariant>> vll;for(int i &#61; 0; i < 5; i &#43;&#43;) {vl.clear();for(int j &#61; 0; j < 5; j &#43;&#43;) {vl.append(i &#43; j);}vll.append(vl);
}excel_handle &#61; new ExcelHandle;
excel_handle -> init(true, false, "我是混子我怕谁");
excel_handle -> openBook(url);
excel_handle -> openSheet(1);
excel_handle -> writeData(1,2,QVariant(12));
excel_handle -> writeDataSet("C2", vll);
excel_handle -> save(url);
以上使用方式仅为参考代码&#xff0c;对于 save 函数以及 exit 函数的使用&#xff0c;一定要在文件打开的时候调用&#xff0c;不然可能会出现错误提示&#xff0c;但不会使程序崩溃&#xff0c;对于判断文件是否已被关闭&#xff0c;目前暂未研究出来&#xff0c;有研究出来的盆友可以在评论区分享一下。
源码分享&#xff1a;https://gitee.com/mjzhutianxiu/dataToExcel
学习分享&#xff0c;一起成长&#xff01;以上为小编的经验分享&#xff0c;若存在不当之处&#xff0c;请批评指正&#xff01;