转:Qt操作Excel文件 QtXlsxWriter的配置使用说明
转:https://blog.csdn.net/linbounconstraint/article/details/52102503
QtXlsxWriter是一个Qt下的库,使用很方便。
项目主页:http://qtxlsx.debao.me/ 有一些演示
Git地址:https://github.com/dbzhang800/QtXlsxWriter
但是上面的git编译不通过。我的Qt5.7下有问题。
在Issures里面 #125 里看到有人修改过的版本支持5.6. qt5.6没试。 #135是解决办法,再次贴出。
下载新的git code:https://github.com/VSRonin/QtXlsxWriter
编译前下载perl. 下载地址(百度的,官网down不下来):http://rj.baidu.com/soft/detail/14792.html?ald
测试的环境:
windows7
Qt5.7
perl: ActivePerl_5.16.2.3010812913.msi
所有文件打包下载:code和activeperl打包下载
1.首先安装ActivePerl_5.16.2.3010812913.msi
2.下载down下来的code。https://github.com/VSRonin/QtXlsxWriter
3.把下载的QtXlsxWriter文件放在一个文件夹下。我的在D:\LeoQt\qt\QtStuProjects\QtXlsxWriter-master5.6\QtXlsxWriter5.6
开始菜单 –> Qt 5.7.0 –> 5.7 –> MinGW 5..3 (32-bit) –> Qt 5.7 for Desktop , 打开该命令行工具,就能使用 MinGW 和 Qt 库进行编译程序。
4 . cd /d D:\LeoQt\qt\QtStuProjects\QtXlsxWriter-master5.6\QtXlsxWriter5.6
依次执行
qmake
mingw32-make
mingw32-make install
安装ok后。
在pro文件添加
QT += xlsx
- 1
#include <QtXlsx>
int main()
{
QXlsx::Document xlsx;
xlsx.write("A1", "Hello Qt!");
xlsx.saveAs("Test.xlsx");
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
所有文件资源打包下载:git的code和activeperl软件打包下载
test demo
#include "mainwindow.h"
#include <QApplication>
#include <QtXlsx>
#include <QDate>
#include <QTime>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QXlsx::Document xlsx;
xlsx.write("A1", "Hello Qt!");
xlsx.write("A2", "Hxxello Qt!");
xlsx.write("A1", "Hello Qt!");
xlsx.write("A2", 12345);
xlsx.write("A3", "=44+33");
xlsx.write("A4", true);
xlsx.write("A5", "http://qt-project.org");
xlsx.write("A6", QDate(2013, 12, 27));
xlsx.write("A7", QTime(6, 30));
xlsx.write("B8", "sadwa");
xlsx.write("B1", "Hello Qt!");
xlsx.write("C1", "Hello Qt!");
xlsx.saveAs("Test.xlsx");
qDebug() << "ok...................................................";
QXlsx::Document xlsx1("Test.xlsx");
QString xx = xlsx1.read("A1").toString();
QString xxx = xlsx1.read("A5").toString();
qDebug() << "xx" << xx << "xxx" << xxx;
qDebug()<<xlsx1.read("A1");
qDebug()<<xlsx1.read("A2");
qDebug()<<xlsx1.read("A3");
qDebug()<<xlsx1.read("A4");
qDebug()<<xlsx1.read("A5");
qDebug()<<xlsx1.read("A6");
qDebug()<<xlsx1.read("A7");
qDebug() << "-----------------------------------------";
for (int row=1; row<10; ++row) {
if (QXlsx::Cell *cell=xlsx.cellAt(row, 1))
qDebug()<<cell->value();
}
return a.exec();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
结果
ok............
xx "Hello Qt!" xxx "http://qt-project.org"
QVariant(QString, "Hello Qt!")
QVariant(double, 12345)
QVariant(QString, "=44+33")
QVariant(bool, true)
QVariant(QString, "http://qt-project.org")
QVariant(QDate, QDate("2013-12-27"))
QVariant(QTime, QTime("06:30:00.000"))
-----------------------------------------
QVariant(QString, "Hello Qt!")
QVariant(double, 12345)
QVariant(double, 0)
QVariant(bool, true)
QVariant(QString, "http://qt-project.org")
QVariant(double, 41635)
QVariant(double, 0.270833)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
//加载。获取exeel里的表单,并设置
void MainWindow::on_pushButton_load_clicked()
{
QXlsx::Document xlsx_tmp(excel_path_file);//E:/Git/Qt/excel_read/Profile TR098.xlsx
QStringList sheetname_tmp = xlsx_tmp.sheetNames();//表单名
qDebug() << "表单名:" << sheetname_tmp;
ui->comboBox_sheet->addItems(sheetname_tmp);
QString tmp = sheetname_tmp.join(",");
qDebug() << "tmp:" << tmp;
//ui->textBrowser_print->append("当前文件中的sheet:"+ tmp);
ui->labelsheet_log->setText("当前文件中表单为:"+ tmp);
//获取选择表单的行列
//xlsx_tmp.selectSheet(arg1);//选中表单
QXlsx::CellRange range;
range = xlsx_tmp.dimension();
sheet_row = range.rowCount();//行
sheet_col = range.columnCount();//列
qDebug() << "打印当前表的行sheet_row:" << sheet_row << " 打印当前表的列sheet_col:" << sheet_col;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
结果:
tmp_path: "E:/Git/Qt/excel_read/Profile TR098.xlsx"
path_file: "E:/Git/Qt/excel_read/Profile TR098.xlsx"
表单名: ("Detail", "Sheet3")
设置当前选中的表里面是项(列): "Detail"
QVariant(QString, "ID")
QVariant(QString, "Profile Name")
QVariant(QString, "Parameter Name")
hang_vlue: ("ID", "Profile Name", "Parameter Name")
tmp: "Detail,Sheet3"
打印当前表的行sheet_row: 1080 打印当前表的列sheet_col: 3
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
QtXlsxWriter 说明文档
http://qtxlsx.debao.me/qtxlsx-examples.html