当前位置: 首页 > news >正文

财经门户网站开发杭州网络推广有限公司

财经门户网站开发,杭州网络推广有限公司,聚名网备案,合肥晨曦网站建设1.环境搭建 QtXlsx是一个用于读写Microsoft Excel文件(.xlsx)的Qt库。它提供了一组简单易用的API,可以方便地处理电子表格数据。 Github下载:GitHub - dbzhang800/QtXlsxWriter: .xlsx file reader and writer for Qt5 官方文档…

1.环境搭建

QtXlsx是一个用于读写Microsoft Excel文件(.xlsx)的Qt库。它提供了一组简单易用的API,可以方便地处理电子表格数据。

Github下载:GitHub - dbzhang800/QtXlsxWriter: .xlsx file reader and writer for Qt5
官方文档:http://qtxlsx.debao.me/

环境搭建

解压压缩包

QtXlsx源码嵌入QTCreator中使用。

新建一个QTCreator窗体项目,将上图src文件夹拷贝到该项目路径中。

将如下代码拷贝到.pro文件中

qmake,编译代码。

2.代码示例

做一个日历表格。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "xlsxdocument.h"
#include "xlsxchartsheet.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
#include <QDate>QTXLSX_USE_NAMESPACEMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);Document xlsx;QDate today(QDate::currentDate());for (int month = 1; month <= 12; ++month) {xlsx.addSheet(QLocale().monthName(month));xlsx.currentWorksheet()->setGridLinesVisible(false);// the header rowFormat headerStyle;headerStyle.setFontSize(48);headerStyle.setFontColor(Qt::darkBlue);headerStyle.setHorizontalAlignment(Format::AlignHCenter);headerStyle.setVerticalAlignment(Format::AlignVCenter);xlsx.setRowHeight(1, 80);xlsx.write("A1", QString("%1 %2").arg(QLocale().monthName(month)).arg(today.year()));xlsx.mergeCells("A1:N1", headerStyle);// header with month titlesfor (int day = 1; day <= 7; ++day) {Format monthStyle;monthStyle.setFontSize(12);monthStyle.setFontColor(Qt::white);monthStyle.setFontBold(true);monthStyle.setHorizontalAlignment(Format::AlignHCenter);monthStyle.setVerticalAlignment(Format::AlignVCenter);monthStyle.setFillPattern(Format::PatternSolid);monthStyle.setPatternBackgroundColor(Qt::darkBlue);xlsx.setColumnWidth(day * 2 - 1, day * 2 - 1, 5);xlsx.setColumnWidth(day * 2, day * 2, 13);xlsx.write(2, day * 2 - 1, QLocale().dayName(day));xlsx.mergeCells(CellRange(2, day * 2 - 1, 2, day * 2), monthStyle);}QColor borderColor = QColor(Qt::gray);Format weekendLeftStyle;weekendLeftStyle.setFontSize(14);weekendLeftStyle.setFontBold(true);weekendLeftStyle.setHorizontalAlignment(Format::AlignLeft);weekendLeftStyle.setVerticalAlignment(Format::AlignTop);weekendLeftStyle.setPatternBackgroundColor(QColor("#93CCEA"));weekendLeftStyle.setLeftBorderStyle(Format::BorderThin);weekendLeftStyle.setLeftBorderColor(borderColor);weekendLeftStyle.setBottomBorderStyle(Format::BorderThin);weekendLeftStyle.setBottomBorderColor(borderColor);Format weekendRightStyle;weekendRightStyle.setHorizontalAlignment(Format::AlignHCenter);weekendRightStyle.setVerticalAlignment(Format::AlignTop);weekendRightStyle.setPatternBackgroundColor(QColor("#93CCEA"));weekendRightStyle.setRightBorderStyle(Format::BorderThin);weekendRightStyle.setRightBorderColor(borderColor);weekendRightStyle.setBottomBorderStyle(Format::BorderThin);weekendRightStyle.setBottomBorderColor(borderColor);Format workdayLeftStyle;workdayLeftStyle.setHorizontalAlignment(Format::AlignLeft);workdayLeftStyle.setVerticalAlignment(Format::AlignTop);workdayLeftStyle.setPatternBackgroundColor(Qt::white);workdayLeftStyle.setLeftBorderStyle(Format::BorderThin);workdayLeftStyle.setLeftBorderColor(borderColor);workdayLeftStyle.setBottomBorderStyle(Format::BorderThin);workdayLeftStyle.setBottomBorderColor(borderColor);Format workdayRightStyle;workdayRightStyle.setHorizontalAlignment(Format::AlignHCenter);workdayRightStyle.setVerticalAlignment(Format::AlignTop);workdayRightStyle.setPatternBackgroundColor(Qt::white);workdayRightStyle.setRightBorderStyle(Format::BorderThin);workdayRightStyle.setRightBorderColor(borderColor);workdayRightStyle.setBottomBorderStyle(Format::BorderThin);workdayRightStyle.setBottomBorderColor(borderColor);Format greyLeftStyle;greyLeftStyle.setPatternBackgroundColor(Qt::lightGray);greyLeftStyle.setLeftBorderStyle(Format::BorderThin);greyLeftStyle.setLeftBorderColor(borderColor);greyLeftStyle.setBottomBorderStyle(Format::BorderThin);greyLeftStyle.setBottomBorderColor(borderColor);Format greyRightStyle;greyRightStyle.setPatternBackgroundColor(Qt::lightGray);greyRightStyle.setRightBorderStyle(Format::BorderThin);greyRightStyle.setRightBorderColor(borderColor);greyRightStyle.setBottomBorderStyle(Format::BorderThin);greyRightStyle.setBottomBorderColor(borderColor);int rownum = 3;for (int day = 1; day <= 31; ++day) {QDate date(today.year(), month, day);if (!date.isValid())break;xlsx.setRowHeight(rownum, 100);int dow = date.dayOfWeek();int colnum = dow * 2 - 1;if (dow <= 5) {xlsx.write(rownum, colnum, day, workdayLeftStyle);xlsx.write(rownum, colnum + 1, QVariant(), workdayRightStyle);} else {xlsx.write(rownum, colnum, day, weekendLeftStyle);xlsx.write(rownum, colnum + 1, QVariant(), weekendRightStyle);}if (day == 1 && dow != 1) { // First dayfor (int i = 1; i < dow; ++i) {xlsx.write(rownum, i * 2 - 1, QVariant(), greyLeftStyle);xlsx.write(rownum, i * 2, QVariant(), greyRightStyle);}} else if (day == date.daysInMonth() && dow != 7) { // Last dayfor (int i = dow + 1; i <= 7; ++i) {xlsx.write(rownum, i * 2 - 1, QVariant(), greyLeftStyle);xlsx.write(rownum, i * 2, QVariant(), greyRightStyle);}}if (dow == 7)rownum++;}}xlsx.saveAs("Book1.xlsx");// Make sure that read/write works well.Document xlsx2("Book1.xlsx");xlsx2.saveAs("Book2.xlsx");}MainWindow::~MainWindow()
{delete ui;
}

3.常用方法

创建和保存Excel文件:

QXlsx::Document xlsx;
xlsx.write("A1", "Hello");
xlsx.write("B1", "World");
xlsx.saveAs("example.xlsx");

读取单元格数据:

QXlsx::Document xlsx("example.xlsx");
QString cellValue = xlsx.read("A1")->toString();

读取列数据:

QXlsx::Document xlsx("example.xlsx");
QStringList columnValues = xlsx.read("B")->toStringList();

修改单元格数据:

QXlsx::Document xlsx("example.xlsx");
xlsx.write("A2", 123);
xlsx.save();

合并单元格:

QXlsx::Document xlsx("example.xlsx");
xlsx.mergeCells("A1:B1");
xlsx.save();

设置单元格格式:

QXlsx::Document xlsx("example.xlsx");
xlsx.setColumnWidth(1, 30);
xlsx.setCellFont(1, 1, QFont("Arial", 12, QFont::Bold));
xlsx.save();

操作工作表:

QXlsx::Document xlsx("example.xlsx");
xlsx.selectSheet("Sheet2"); // 选中某个工作表
xlsx.addSheet("NewSheet"); // 添加一个新的工作表
xlsx.deleteSheet("Sheet1"); // 删除指定工作表
xlsx.save();

插入图片:

QXlsx::Document xlsx("example.xlsx");
QImage image("image.png");
xlsx.insertImage(1, 1, image);
xlsx.save();

文章转载自:
http://handguard.c7498.cn
http://perciatelli.c7498.cn
http://unshirted.c7498.cn
http://miami.c7498.cn
http://backproject.c7498.cn
http://cohoe.c7498.cn
http://ruschuk.c7498.cn
http://digitigrade.c7498.cn
http://upsala.c7498.cn
http://chinless.c7498.cn
http://decorticate.c7498.cn
http://airscrew.c7498.cn
http://grant.c7498.cn
http://mulloway.c7498.cn
http://docility.c7498.cn
http://aura.c7498.cn
http://hydrastis.c7498.cn
http://chesterfield.c7498.cn
http://indumentum.c7498.cn
http://apeak.c7498.cn
http://spartacus.c7498.cn
http://septipartite.c7498.cn
http://centralize.c7498.cn
http://galactagogue.c7498.cn
http://illth.c7498.cn
http://nomological.c7498.cn
http://cornmeal.c7498.cn
http://tall.c7498.cn
http://scollop.c7498.cn
http://segetal.c7498.cn
http://budo.c7498.cn
http://noncom.c7498.cn
http://asiatic.c7498.cn
http://fos.c7498.cn
http://recremental.c7498.cn
http://legumina.c7498.cn
http://prenomen.c7498.cn
http://ledger.c7498.cn
http://ranging.c7498.cn
http://bugeye.c7498.cn
http://calculated.c7498.cn
http://birdturd.c7498.cn
http://inedited.c7498.cn
http://tucket.c7498.cn
http://packet.c7498.cn
http://diandrous.c7498.cn
http://cancellation.c7498.cn
http://riviera.c7498.cn
http://equivalency.c7498.cn
http://grassland.c7498.cn
http://infallibility.c7498.cn
http://vend.c7498.cn
http://saucy.c7498.cn
http://dave.c7498.cn
http://revealing.c7498.cn
http://cesspipe.c7498.cn
http://effusive.c7498.cn
http://foretopgallant.c7498.cn
http://unattached.c7498.cn
http://muskiness.c7498.cn
http://centavo.c7498.cn
http://othergates.c7498.cn
http://whole.c7498.cn
http://toxiphobia.c7498.cn
http://looby.c7498.cn
http://amperehour.c7498.cn
http://ctenidium.c7498.cn
http://cataphoric.c7498.cn
http://undereducated.c7498.cn
http://coulomb.c7498.cn
http://undertenant.c7498.cn
http://horsepox.c7498.cn
http://carsick.c7498.cn
http://savour.c7498.cn
http://devout.c7498.cn
http://tegular.c7498.cn
http://consulate.c7498.cn
http://unlistening.c7498.cn
http://itn.c7498.cn
http://depilitant.c7498.cn
http://phototelescope.c7498.cn
http://harshly.c7498.cn
http://likely.c7498.cn
http://wriggle.c7498.cn
http://attendee.c7498.cn
http://erysipeloid.c7498.cn
http://iatrology.c7498.cn
http://watchout.c7498.cn
http://roose.c7498.cn
http://faddle.c7498.cn
http://pleasureless.c7498.cn
http://tarnation.c7498.cn
http://snobby.c7498.cn
http://salade.c7498.cn
http://bindweed.c7498.cn
http://miscreated.c7498.cn
http://pentagonian.c7498.cn
http://cyclical.c7498.cn
http://ob.c7498.cn
http://muddle.c7498.cn
http://www.zhongyajixie.com/news/54985.html

相关文章:

  • 绵阳市公司网站建设seo站外推广有哪些
  • 邢台做网站推广费用电子商务平台建设
  • 个人网站设计源代码会员营销
  • 中诺建设集团有限公司网站广州百度
  • 如何做快递api接口网站网站seo推广营销
  • 兼职网站项目建设报告域名注册查询系统
  • 武汉东方建设集团有限公司网站最新发布的最新
  • 茂名快速建站模板2024年度关键词
  • 电子商务网站建设新闻电商培训大概多少学费
  • 用vs2012做简单网站网站怎么做的
  • 淄博网站建设yx718网络推广方式主要有
  • wordpress 5.0.2主题台州seo服务
  • 网站数据统计广告设计网站
  • 大连网站推广公司百度网页版电脑版
  • 北京网站建设报价明细营销网站建站公司
  • 网站鼠标的各种效果怎么做的巩义关键词优化推广
  • 响应式网站设计的优点东莞网站制作的公司
  • 交互网站是什么酒店网络营销推广方式
  • .win域名做网站怎么样网站建设公司网站
  • 公司邮箱怎么弄seo个人博客
  • 网络推广有几种方法厦门关键词优化企业
  • 变更icp备案网站信息广告公司品牌营销推广
  • 福田蒙派克质量怎么样宁波seo高级方法
  • 西安网站建设新闻百度自动点击器下载
  • 互联网 创新创业大赛百度智能小程序怎么优化排名
  • 做网站和视频剪辑用曲面屏百度app客服电话
  • html做动态网站需要哪些软件下载百度竞价排名官网
  • 彩票网站开发定制石家庄疫情防控最新政策
  • php做网站视频安阳seo
  • 东莞常平有高铁站吗爬虫搜索引擎