https://github.com/dbzhang800/QtXlsxWriter 下载qtxlsx地址

QtXlsx is a library that can read and write Excel files. It doesn't require Microsoft Excel and can be used in any platform that Qt5 supported. The library can be used to

  • Generate a new .xlsx file from scratch
  • Extract data from an existing .xlsx file
  • Edit an existing .xlsx file

Getting Started

  • For linux user, if your Qt is installed through package manager tools such "apt-get", make sure that you have installed the Qt5 develop package qtbase5-private-dev

Usage(1): Use Xlsx as Qt5's addon module

Building the module

Note: Perl is needed in this step.

  • Download the source code.

  • Put the source code in any directory you like

  • Go to top directory of the project in a terminal and run

    qmakemakemake install

The library, the header files, and others will be installed to your system.

make html_docs can be used to generate documentations of the library, and make check can be used to run unit tests of the library.

Using the module

  • Add following line to your qmake's project file:
    QT += xlsx
  • Then, using Qt Xlsx in your code
    #include <QtXlsx>int main() { QXlsx::Document xlsx; xlsx.write("A1", "Hello Qt!"); xlsx.saveAs("Test.xlsx"); return 0; }

Usage(2): Use source code directly

The package contains a qtxlsx.pri file that allows you to integrate the component into applications that use qmake for the build step.

  • Download the source code.

  • Put the source code in any directory you like. For example, 3rdparty:

    |-- project.pro|-- ....|-- 3rdparty\|     |-- qtxlsx\|     |
  • Add following line to your qmake project file:
    include(3rdparty/qtxlsx/src/xlsx/qtxlsx.pri)

Note: If you like, you can copy all files from src/xlsx to your application's source path. Then add following line to your project file:

include(qtxlsx.pri)

> **Note**: If you do not use qmake, you need to define the following macro manually> ```XLSX_NO_LIB
  • Then, using Qt Xlsx in your code
    #include "xlsxdocument.h"int main() { QXlsx::Document xlsx; xlsx.write("A1", "Hello Qt!"); xlsx.saveAs("Test.xlsx"); return 0; }

在构建的时候,可能会有如下的错误,

xlsxzipreader.cpp: In member function ‘void QXlsx::ZipReader::init()’:
xlsxzipreader.cpp:51:66: error: conversion from ‘QVector<QZipReader::FileInfo>’ to non-scalar type ‘QList<QZipReader::FileInfo>’ requestedQList<QZipReader::FileInfo> allFiles = m_reader->fileInfoList();
 

这里只需要吧出错的文件添加 :
 #include <QVector>
然后把出错的地方的"QList" 用"QVector" 替换掉重新构建就可以了。

 简单的写excel:

void BurnTaskWidget::OnExportButtonClick()
{QXlsx::Document z_xlsx;QStringList z_titleList;QString z_filePathName;QString z_defaultFileName = "刻录任务汇总.xls";// 设置保存的默认文件名称 文件名_当前时间.xlsQFileInfo z_fileinfo(z_defaultFileName);QDateTime z_curDateTime = QDateTime::currentDateTime();QString z_strCurTime = z_curDateTime.toString("yyyyMMddhhmmss");z_defaultFileName = z_fileinfo.baseName() + "_" + z_strCurTime + ".xls";// 获取保存文件路径QFileDialog *z_fileDlg = new QFileDialog(this);z_fileDlg->setWindowTitle("保存文件");z_fileDlg->setAcceptMode(QFileDialog::AcceptSave);z_fileDlg->selectFile(z_defaultFileName);z_fileDlg->setNameFilter("Excel Files(*.xls *.xlsx)");z_fileDlg->setDefaultSuffix("xls");if (z_fileDlg->exec() == QDialog::Accepted){z_filePathName = z_fileDlg->selectedFiles().at(0);}// 保存文件添加后缀名z_fileinfo =  QFileInfo(z_filePathName);if (z_fileinfo.suffix() != "xls" && z_fileinfo.suffix() != "xlsx"){z_filePathName += ".xls";}// 设置excel任务标题z_titleList << "流水号" << "光盘名称" << "密级" << "提交人" << "部门" << "申请时间" << "任务状态";for (int i = 0; i < z_titleList.size(); i++){z_xlsx.write(1, i+1, z_titleList.at(i));}// 设置烈宽z_xlsx.setColumnWidth(1, 20);z_xlsx.setColumnWidth(2, 20);z_xlsx.setColumnWidth(3, 10);z_xlsx.setColumnWidth(4, 15);z_xlsx.setColumnWidth(5, 20);z_xlsx.setColumnWidth(6, 25);z_xlsx.setColumnWidth(7, 10);// 获取表格内容设置excelint z_row = modelBurnTask->rowCount();int z_col = modelBurnTask->columnCount() - 1;// 最后一列为图标,不保存for (int i = 0; i < z_row; i++){for (int j = 0; j < z_col; j++){z_xlsx.write(i+2, j+1 ,modelBurnTask->index(i, j).data().toString());}}// 保存文件z_xlsx.saveAs(z_filePathName);
}

  

读写方法:

 读:
void FlightPlanWid::readExcel(QString path)
{QXlsx::Document xlsx(path);QXlsx::Workbook *workBook = xlsx.workbook();QXlsx::Worksheet *workSheet = static_cast<QXlsx::Worksheet*>(workBook->sheet(0));ui.tableWidget->setRowCount(workSheet->dimension().rowCount());ui.tableWidget->setColumnCount(23);QString value;for (int i = 6; i <= workSheet->dimension().rowCount(); i++){for (int j = 1; j <= workSheet->dimension().columnCount(); j++){QXlsx::Cell *cell = workSheet->cellAt(i, j);if (cell==NULL) continue;if (cell->isDateTime())//日期{if (cell->dateTime().date().year()==1899) continue;value = cell->dateTime().toString("yyyy/MM/dd hh:mm");}    else{value = cell->value().toString();}setItemValue(i - 6, j - 1, value);}}for (int i = ui.tableWidget->rowCount() - 1; i >= 0; i--)//删除末尾空白行{QTableWidgetItem *item = ui.tableWidget->item(i, 0);if (item==NULL)ui.tableWidget->removeRow(i);else break;}
}

  写:

void FlightPlanWid::saveBtnClickedSlot()
{if (currentScriptIndex == -1) return;QXlsx::Document xlsx;QString value;for (int i = 0; i < ui.tableWidget->rowCount(); i++){for (int j = 0; j < ui.tableWidget->columnCount(); j++){QTableWidgetItem *item = ui.tableWidget->item(i, j);if (item == NULL)value = "";elsevalue = item->text();xlsx.write(i + 1, j + 1, value);}}if (xlsx.saveAs("./Data/Saved/" + ui.nameEdt->text() + ".xlsx"))QMessageBox::information(this, "保存", "保存成功!", QMessageBox::Ok);
}

  

对于Qt Xlsx ( QtXlsxWriter ) 的安装请参考: 
http://blog.csdn.net/woshidahuaidan2011/article/details/52724452 
这里主要介绍其基本的编程使用方法。 
首先我们想到就是对xlsx文件进行读写,因此我们有如下的代码:

#include <QtCore>
#include "xlsxdocument.h" #include "xlsxformat.h" #include "xlsxcellrange.h" #include "xlsxchart.h" int main() { QXlsx::Document xlsx("book1.xlsx");/*打开一个book1的文件*/ QXlsx::Format format1;/*设置该单元的样式*/ format1.setFontColor(QColor(Qt::red));/*文字为红色*/ format1.setPatternBackgroundColor(QColor(152,251,152));/*北京颜色*/ format1.setFontSize(15);/*设置字体大小*/ format1.setHorizontalAlignment(QXlsx::Format::AlignHCenter);/*横向居中*/ format1.setBorderStyle(QXlsx::Format::BorderDashDotDot);/*边框样式*/ xlsx.write("A1", "Hello Qt!", format1);/*写入文字,应该刚才设置的样式*/ xlsx.write(2, 1, 12345, format1);/*写入文字,应该刚才设置的样式*/ QXlsx::Format format2;/*重新设置另一个单元的样式*/ format2.setFontBold(true);/*设置加粗*/ format2.setFontUnderline(QXlsx::Format::FontUnderlineDouble);/*下双划线*/ format2.setFillPattern(QXlsx::Format::PatternLightUp);/*填充方式*/ xlsx.write("A4", "=44+33", format2);/*写入文字,应该刚才设置的样式*/ xlsx.write("C4", true, format2); xlsx.saveAs("book1.xlsx");/*保存*/ QXlsx::Document xlsx2("Book1.xlsx");/*复制book1到book2*/ xlsx2.saveAs("Book2.xlsx"); return 0; }

其运行效果如下: 

接下来我们处理数据的是很多时候需要插入折线图、饼状图等等来图像化的处理数据,因此我们可以可以在原有代码上添加一段代码,使之添加一个sheet而且插入饼状图及其条形图:

#include <QtCore>
#include "xlsxdocument.h" #include "xlsxformat.h" #include "xlsxcellrange.h" #include "xlsxchart.h" int main() { QXlsx::Document xlsx("book1.xlsx");/*打开一个book1的文件*/ QXlsx::Format format1;/*设置该单元的样式*/ format1.setFontColor(QColor(Qt::red));/*文字为红色*/ format1.setPatternBackgroundColor(QColor(152,251,152));/*北京颜色*/ format1.setFontSize(15);/*设置字体大小*/ format1.setHorizontalAlignment(QXlsx::Format::AlignHCenter);/*横向居中*/ format1.setBorderStyle(QXlsx::Format::BorderDashDotDot);/*边框样式*/ xlsx.write("A1", "Hello Qt!", format1);/*写入文字,应该刚才设置的样式*/ xlsx.write(2, 1, 12345, format1);/*写入文字,应该刚才设置的样式*/ QXlsx::Format format2;/*重新设置另一个单元的样式*/ format2.setFontBold(true);/*设置加粗*/ format2.setFontUnderline(QXlsx::Format::FontUnderlineDouble);/*下双划线*/ format2.setFillPattern(QXlsx::Format::PatternLightUp);/*填充方式*/ xlsx.write("A4", "=44+33", format2);/*写入文字,应该刚才设置的样式*/ xlsx.write("C4", true, format2); if(!xlsx.selectSheet("ziv")){/*在当前打开的xlsx文件中,找一个名字为ziv的sheet*/ xlsx.addSheet("ziv");//找不到的话就添加一个名为ziv的sheet } for (int i=10; i<20; ++i) {/*写入一串数字*/ xlsx.write(i, 1, i*i*i); //A10:A19 xlsx.write(i, 2, i*i); //B10:B19 xlsx.write(i, 3, i*i-1); //C10:C19 } QXlsx::Chart *pieChart = xlsx.insertChart(3, 5, QSize(300, 300));/*在3行5列的位置插入一个图标*/ pieChart->setChartType(QXlsx::Chart::CT_Pie);/*插入一个饼形图*/ pieChart->addSeries(QXlsx::CellRange("A10:A19"));/*饼形图添加数据*/ pieChart->addSeries(QXlsx::CellRange("B10:B19")); pieChart->addSeries(QXlsx::CellRange("C10:C19")); QXlsx::Chart *barChart = xlsx.insertChart(3, 13, QSize(300, 300));/*在3行13列的位置插入一个图标*/ barChart->setChartType(QXlsx::Chart::CT_Bar);/*条形图*/ barChart->addSeries(QXlsx::CellRange("A10:C19"));/*给条形图加入数据*/ xlsx.saveAs("book1.xlsx");/*保存*/ QXlsx::Document xlsx2("Book1.xlsx");/*复制book1到book2*/ xlsx2.saveAs("Book2.xlsx"); return 0; } 

上面代码中运行效果如下: 

很多时候我们还需要添加图表来美化表格,代码如下:

#include <QtCore>
#include <QtGui> #include "xlsxdocument.h" #include "xlsxformat.h" #include "xlsxcellrange.h" #include "xlsxchart.h" int main() { QXlsx::Document xlsx("book1.xlsx");/*打开一个book1的文件*/ QXlsx::Format format1;/*设置该单元的样式*/ format1.setFontColor(QColor(Qt::red));/*文字为红色*/ format1.setPatternBackgroundColor(QColor(152,251,152));/*北京颜色*/ format1.setFontSize(15);/*设置字体大小*/ format1.setHorizontalAlignment(QXlsx::Format::AlignHCenter);/*横向居中*/ format1.setBorderStyle(QXlsx::Format::BorderDashDotDot);/*边框样式*/ xlsx.write("A1", "Hello Qt!", format1);/*写入文字,应该刚才设置的样式*/ xlsx.write(2, 1, 12345, format1);/*写入文字,应该刚才设置的样式*/ QXlsx::Format format2;/*重新设置另一个单元的样式*/ format2.setFontBold(true);/*设置加粗*/ format2.setFontUnderline(QXlsx::Format::FontUnderlineDouble);/*下双划线*/ format2.setFillPattern(QXlsx::Format::PatternLightUp);/*填充方式*/ xlsx.write("A4", "=44+33", format2);/*写入文字,应该刚才设置的样式*/ xlsx.write("C4", true, format2); if(!xlsx.selectSheet("ziv")){/*在当前打开的xlsx文件中,找一个名字为ziv的sheet*/ xlsx.addSheet("ziv");//找不到的话就添加一个名为ziv的sheet } for (int i=10; i<20; ++i) {/*写入一串数字*/ xlsx.write(i, 1, i*i*i); //A10:A19 xlsx.write(i, 2, i*i); //B10:B19 xlsx.write(i, 3, i*i-1); //C10:C19 } QXlsx::Chart *pieChart = xlsx.insertChart(3, 5, QSize(300, 300));/*在3行5列的位置插入一个图标*/ pieChart->setChartType(QXlsx::Chart::CT_Pie);/*插入一个饼形图*/ pieChart->addSeries(QXlsx::CellRange("A10:A19"));/*饼形图添加数据*/ pieChart->addSeries(QXlsx::CellRange("B10:B19")); pieChart->addSeries(QXlsx::CellRange("C10:C19")); QXlsx::Chart *barChart = xlsx.insertChart(3, 13, QSize(300, 300));/*在3行13列的位置插入一个图标*/ barChart->setChartType(QXlsx::Chart::CT_Bar);/*条形图*/ barChart->addSeries(QXlsx::CellRange("A10:C19"));/*给条形图加入数据*/ QImage image(40, 100, QImage::Format_RGB32);/*新建一个Qimage,大小40*100*/ image.fill(Qt::green);/*填充绿色*/ xlsx.insertImage(1, 1, image);/*插入图片*/ xlsx.saveAs("book1.xlsx");/*保存*/ QXlsx::Document xlsx2("Book1.xlsx");/*复制book1到book2*/ xlsx2.saveAs("Book2.xlsx"); return 0; } 

效果如下: 

到此为止对于Qt Xlsx 的使用应该可以满足大部分的需求,如有疑问欢迎留言指正,原文地址: 
http://blog.csdn.net/woshidahuaidan2011/article/details/53349163

转载于:https://www.cnblogs.com/wangjian8888/p/9176662.html

qt 使用qtxlsx 读写excel相关推荐

  1. QtXlsx 读写 excel

    Qt 之 QAxObject 读写 excel 如何配置 QtXlsx 的两种方法 要使用QtXlsx ,需要引入这个模块,不然是用不了的,引入方法,在工程 pro 文件中加入 QT += xlsx ...

  2. Qt使用QAxObject读写Excel 的方法

    参考资料: Qt导出Excel的简单实现 QT界面开发-QAxObject 读写excel(COM组件) 浅谈 Excel 对象模型 ActiveX Objects Excel VBA 参考 一.简介 ...

  3. Qt利用QtXlsx操作excel文件

    一.入门示例 代码来自QtXlsx提供的示例文件项目 F:\QtProject\QtXlsxWriter-master\examples\xlsx\hello 1.生成一个excel文件 2.代码如下 ...

  4. Qt使用Qtxlsx读写xlsx文件

    Qtxlsx官网:http://qtxlsx.debao.me/ QtXlsx是一个可以读取和写入Excel文件的库.它不需要安装Microsoft Excel,而且还可以跨平台,可以在Qt5支持的任 ...

  5. 一文搞定Qt读写excel以及qt读写xml数据

    一文搞定Qt读写excel以及qt读写xml数据 最终的实现效果图 RC_ICONS = logo.ico .pro文件同级目录下加入 logo.ico 图标文件,运行文件,文件的图标就被写入软件 u ...

  6. qt 编译的时候把源文件复制到编译目录_[QT]读写Excel

    C++读写Excel有许多开源库,下边这个连接有总结: troldal/OpenXLSX​github.com 因为工作需要QT,所以今天我们共同学习一下QT读写Excel.用到的开源库: https ...

  7. QT 利用QAxObject大数据读写excel文件

    不知不觉已经加入CSDN很久了,但从来没有发过文章,最近开发了一个小工具踩了很多坑,主要是QT操作大数据Excel文件,现在和大家分享一下. 查阅了大部分资料,大数据读写excel文件用QAxObje ...

  8. C++ QT 读写EXCEL表格基操

    要读写Excel表格,需要使用第三方库.可以使用Qt提供的QAxObject类来访问Microsoft Excel应用程序,从而读写Excel表格. 以下是一个简单的示例代码,演示如何使用QAxObj ...

  9. Qtxlsx操作Excel之使用

    相关连接: (1).QAxObject的使用方法:https://zhangzc.blog.csdn.net/article/details/106213607 (2).Qtxlsx库的环境配置:ht ...

最新文章

  1. C++学习路线(最全资源整合)
  2. 联邦家私:用宜搭一周上线售后管理系统,打通信息孤岛,提升协同效率
  3. Java 8 Friday:更好的异常
  4. POST和GET请求区别
  5. spring boot 拦截器 或 Spring AOP 方式记录请求日志
  6. 免费搭建网易云音乐API
  7. 终于找到了IE6修复大全
  8. java星空屏幕,CorelDraw 制作宇宙星空
  9. java爬取双色球所有中奖号码历史数据
  10. 多目标优化 MOP (一):遗传算法 SPEA 1999
  11. 鸿蒙无锡有什么特产,无锡太湖鼋头渚十大特产
  12. 分频电路设计(笔记)
  13. 微信隐藏功能:群接龙
  14. 疫情下的校园生活是咋样的?
  15. SSM项目的基本静态资源配置
  16. 2017年9月历史文章汇总
  17. 全球计算机科学研究生排名,新|美国计算机科学研究生专业世界排名靠50强名单...
  18. oracle csv只能导出1200条,Oracle PLSQL数据导出csv的案例
  19. Tampermonkey(油猴)脚本编写快速入门
  20. 计算机操作课后 陆丽娜第二版,操作系统 第四章课后部分答案

热门文章

  1. MySql 相似度计算
  2. java中 byte 取值范围_java中为什么byte的取值范围是-128到+127
  3. java jbutton 大小_java-jButton在运行时调整大小
  4. 数据仓库面试题集锦(附答案和数仓知识体系),小红书Python面试题目
  5. 【浙大版《Python 程序设计》题目集(解)】第6章-7 找出总分最高的学生(15分)
  6. 2014十大网络红人排行榜(附照片)
  7. android程序劫持持程序,安卓activity劫持测试工具开发
  8. java中数组的内存模型_Java如何在内存有限的情况下实现一个超大数组?jvm性能调优+内存模型+虚拟机原理能解决什么样问题...
  9. 可穿戴医疗设备是“香饽饽”,但也有数据和安全之痛
  10. 应用宝苹果版_点赞应用ios版下载-点赞应用苹果版下载v1.1