作者:mobiledu2502931473 | 来源:互联网 | 2023-09-11 14:13
一、获取QtXlsxhttps:github.comdbzhang800QtXlsxWriter二、编译1、下载并解压,得到如下文件2、用QtCreater打开
一、获取QtXlsx
https://github.com/dbzhang800/QtXlsxWriter
二、编译
1、下载并解压,得到如下文件
2、用QtCreater打开,仅构建src项目即可
3、构建报错,将QList改为QVector即可,需要添加#include
4、编译成功后得到如下目录
5、拷贝bin目录下文件到Qt安装目录中,注意所要使用得编译器类型,我的C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\bin
->
6、拷贝include目录到C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\include
->
注意,QtXlsx中得部分头文件需要替换为E:\download\QtXlsxWriter-master\src\xlsx中得响应文件
7、拷贝下图中选中的lib中得文件到C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\lib
8、拷贝下面3个文件到C:\Qt\Qt5.12.4\5.12.4\msvc2017_64\mkspecs\modules
三、使用
1、.pro文件中添加QT+= xlsx
2、头文件包含:
#include "QtXlsx/QtXlsx"
3、写
QXlsx::Document xlsx;
xlsx.write("A1", "Hello Qt!");
xlsx.saveAs("Test.xlsx");/QXlsx::Document xlsx(path);for (int i = 0; i columnCount(); i++) {xlsx.write(1, i + 1, tbl->horizontalHeaderItem(i)->text());}for (int r = 0; r rowCount(); r++) {for (int c = 0; c columnCount(); c++) {QString item_text = tbl->item(r, c)->text();xlsx.write(r + 2, c + 1, item_text);}}QMessageBox::information(0, tr("Export"), xlsx.save() ? tr("Save sessuced!") : tr("Save failed!"));
4、读
QString path &#61; QFileDialog::getOpenFileName(m_wgt, "file");if(file_path.isEmpty()) return;if(!file_path.contains(".xlsx")) {QMessageBox::warning(m_wgt, tr("Warning"), tr("File selection error, place select again!"));return;}QXlsx::Document xlsx(path);QXlsx::Workbook *workBook &#61; xlsx.workbook();QXlsx::Worksheet *workSheet &#61; static_cast(workBook->sheet(0));//第一个标签页QDebug() <sheet(0)->sheetName();QString value;for (int i &#61; 0; i <&#61; workSheet->dimension().rowCount(); i&#43;&#43;){for (int j &#61; 1; j <&#61; workSheet->dimension().columnCount(); j&#43;&#43;){QXlsx::Cell *cell &#61; workSheet->cellAt(i, j);value &#61; cell->value().toString();QDebug() <