聊城哪里做网站,做庭院景观的那个网站推广好,软件技术专业主要学什么,3d效果图什么网站做的好Qt Excel读写 - QXlsx的安装配置以及测试 引言一、安装配置二、简单测试 引言
Qt无自带的库处理Excel 文件#xff0c;但可通过QAxObject 借助COM接口进行Excel的读写1。亦可使用免费的开源第三方库#xff1a;QXlsx#xff0c;一个基于Qt库开发的用于读写Microsoft Excel文… Qt Excel读写 - QXlsx的安装配置以及测试 引言一、安装配置二、简单测试 引言
Qt无自带的库处理Excel 文件但可通过QAxObject 借助COM接口进行Excel的读写1。亦可使用免费的开源第三方库QXlsx一个基于Qt库开发的用于读写Microsoft Excel文件的C库。它提供了一组简单易用的API可以方便地创建、修改和操作Excel文件。 可参考 https://github.com/QtExcel/QXlsx 官方源码 https://gitcode.com/qtexcel/qxlsx/overview?utm_sourcecsdn_github_acceleratorisLogin1 官方源码 - gitCode(GitHub加速计划 - 推荐) https://blog.csdn.net/qq_36393978/article/details/132542318 使用 QAxObject 高效任意读写 Excel 表1 https://blog.csdn.net/techenliu/article/details/133089775 QT之excel的读写2 一、安装配置 下载源码如下图所示QXlsx就是核心代码里面还包含了很多示例。 创建一个工程控制台或者带界面的都可以将QXlsx复制到工程目录下。 在.pro文件中添加以下语句将QXlsx添加到工程中保存pro文件 (自动会执行qmake 或自己手动执行)
# QXlsx code for Application Qt project
QXLSX_PARENTPATH./QXlsx/ # current QXlsx path is . (. means curret directory)
QXLSX_HEADERPATH./QXlsx/header/ # current QXlsx header path is ./header/
QXLSX_SOURCEPATH./QXlsx/source/ # current QXlsx source path is ./source/
include(./QXlsx/QXlsx.pri)二、简单测试 创建一个.xlsx文件对单元格赋值保存到磁盘上。
#include mainwindow.h
#include QApplication// QXlsx
#include xlsxdocument.h
#include xlsxchartsheet.h
#include xlsxcellrange.h
#include xlsxchart.h
#include xlsxrichstring.h
#include xlsxworkbook.hint main(int argc, char *argv[])
{
// QApplication a(argc, argv);
// MainWindow w;
// w.show();// return a.exec();// 测试QXlsxQXlsx::Document xlsx;xlsx.write(A1, A1); // (A,1) 第一行第一列xlsx.write(1, 2, A2); // (A,2) 第一行第二列xlsx.saveAs(Test.xlsx); // 保存return 0;
} 读取一个.xlsx文件输出到控制台。(使用官方的ShowConsole代码) 修改官方代码.pro文件配置添加QXlsx。修改xlsxFileName为刚才生成的Test.xlsx运行即可。
// main.cpp#include iostream
#include vector#include QCoreApplication
#include QDebug
#include QDir
#include QVariant
#include QtCore
#include QtGlobal
using namespace std;// [0] include QXlsx headers
#include xlsxcellrange.h
#include xlsxchart.h
#include xlsxchartsheet.h
#include xlsxdocument.h
#include xlsxrichstring.h
#include xlsxworkbook.h
using namespace QXlsx;#include fort.hpp // libfortint main(int argc, char *argv[])
{QCoreApplication app(argc, argv);// get xlsx file nameQString xlsxFileName Test.xlsx;qDebug() xlsxFileName;// tried to load xlsx using temporary documentQXlsx::Document xlsxTmp(xlsxFileName);if (!xlsxTmp.isLoadPackage()) {qCritical() Failed to load xlsxFileName;return (-1); // failed to load}// load new xlsx using new documentQXlsx::Document xlsxDoc(xlsxFileName);xlsxDoc.isLoadPackage();int sheetIndexNumber 0;foreach (QString curretnSheetName, xlsxDoc.sheetNames()) {QXlsx::AbstractSheet *currentSheet xlsxDoc.sheet(curretnSheetName);if (NULL currentSheet)continue;// get full cells of sheetint maxRow -1;int maxCol -1;currentSheet-workbook()-setActiveSheet(sheetIndexNumber);Worksheet *wsheet (Worksheet *) currentSheet-workbook()-activeSheet();if (NULL wsheet)continue;QString strSheetName wsheet-sheetName(); // sheet name// display sheet namestd::cout std::string(strSheetName.toLocal8Bit()) std::endl;QVectorCellLocation clList wsheet-getFullCells(maxRow, maxCol);QVectorQVectorQString cellValues;for (int rc 0; rc maxRow; rc) {QVectorQString tempValue;for (int cc 0; cc maxCol; cc) {tempValue.push_back(QString());}cellValues.push_back(tempValue);}for (int ic 0; ic clList.size(); ic) {// cell locationCellLocation cl clList.at(ic);int row cl.row - 1;int col cl.col - 1;// https://github.com/QtExcel/QXlsx/commit/9ab612ff5c9defc35333799c55b01be31aa66fc2// {{// QSharedPointerCell ptrCell cl.cell; // cell pointerstd::shared_ptrCell ptrCell cl.cell; // cell pointer// value of cell// QVariant var cl.cell.data()-value();QVariant var ptrCell-value();// }}QString str var.toString();cellValues[row][col] str;}fort::table fortTable;for (int rc 0; rc maxRow; rc) {for (int cc 0; cc maxCol; cc) {QString strTemp cellValues[rc][cc];fortTable std::string(strTemp.toLocal8Bit()); // display value}fortTable fort::endr; // change to new row}std::cout fortTable.to_string() std::endl; // display forttable rowsheetIndexNumber;}return 0;
} https://blog.csdn.net/qq_36393978/article/details/132542318 使用 QAxObject 高效任意读写 Excel 表 ↩︎ ↩︎ https://blog.csdn.net/techenliu/article/details/133089775 QT之excel的读写 ↩︎