官方文档:Qt Xlsx | QtXlsx 0.3 (debao.me)
下载地址:dbzhang800/QtXlsxWriter: .xlsx file reader and writer for Qt5 (github.com)
QtXlsx是可以读取和写入Excel文件的库。 它不需要Microsoft Excel,并且可以在Qt5支持的任何平台中使用。 该库可用于:
注意:
对于Linux用户,如果Qt是通过“ apt-get”之类的软件包管理器工具安装的,请确保已安装Qt5开发软件包qtbase5-private-dev
注意:此步骤需要用到Perl。
如果您已经安装则可进行2.2步骤,否则请先安装perl,参考我另一篇博客:
perl安装教程_超级大洋葱的博客-CSDN博客
https://github.com/dbzhang800/QtXlsxWriter
注意,源代码前级目录不要有中文!!!
我这里有5个编译器:
现在以给MinGW 32-bit 作为示例,其它编译器类似:
我的路径地址为:
H:\0.Download\Edge\QtXlsxWriter-master\src
接下来进行以下操作(Linux):
qmake src.pro
make
make install
接下来进行以下操作(mingw32、mingw64):
qmake src.pro
mingw32-make
mingw32-make install
执行qmake,会自动生成makefile文件
这里会编译很久,喝杯茶休息休息:
编译完成:
执行以下命令:
H:\0.Download\Edge\QtXlsxWriter-master\src>mingw32-make install
cd xlsx\ && ( if not exist Makefile E:\Qt5.14.2\5.14.2\mingw73_32\bin\qmake.exe -o Makefile H:\0.Download\Edge\QtXlsxWriter-master\src\xlsx\xlsx.pro ) && mingw32-make -f Makefile install
mingw32-make[1]: Entering directory 'H:/0.Download/Edge/QtXlsxWriter-master/src/xlsx'
mingw32-make -f Makefile.Release install
mingw32-make[2]: Entering directory 'H:/0.Download/Edge/QtXlsxWriter-master/src/xlsx'
copy /y ..\..\lib\Qt5Xlsx.dll E:\Qt5.14.2\5.14.2\mingw73_32\bin\Qt5Xlsx.dll
已复制 1 个文件。
....此处省略一万字
E:\Qt5.14.2\5.14.2\mingw73_32\bin\qmake.exe -install qinstall H:\0.Download\Edge\QtXlsxWriter-master\lib\cmake\Qt5Xlsx\Qt5XlsxConfig.cmake E:\Qt5.14.2\5.14.2\mingw73_32\lib\cmake\Qt5Xlsx\Qt5XlsxConfig.cmake
E:\Qt5.14.2\5.14.2\mingw73_32\bin\qmake.exe -install qinstall H:\0.Download\Edge\QtXlsxWriter-master\lib\cmake\Qt5Xlsx\Qt5XlsxConfigVersion.cmake E:\Qt5.14.2\5.14.2\mingw73_32\lib\cmake\Qt5Xlsx\Qt5XlsxConfigVersion.cmake
mingw32-make[2]: Leaving directory 'H:/0.Download/Edge/QtXlsxWriter-master/src/xlsx'
mingw32-make[1]: Leaving directory 'H:/0.Download/Edge/QtXlsxWriter-master/src/xlsx'
首先要解压另一份源码:
这里我们用64bit工程做示范,32位一样:
设置工程名称:
选择编译器版本:
创建完成:
我这里地址为:
H:\0.Download\Edge\QtXlsxWriter-master\examples\xlsx
#include
#include "xlsxdocument.h"
int main()
{
//![0]
QXlsx::Document xlsx;
//![0]
//![1]
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));
//![1]
//![2]
xlsx.save();
//![2]
return 0;
}
看到有报错,是因为没有引入xlsx模块
。
QT += xlsx
生成了xlsx格式的文件
表格内容如下:
该包包含一个 qtxlsx.pri文件,允许您将组件集成到使用 qmake 进行生成步骤的应用程序中。
来到工程目录下:
打开网上下载的源码路径:
# 使用qtxlsx源代码
include(qtxlsx/src/xlsx/qtxlsx.pri)
#include
#include "xlsxdocument.h"
int main()
{
//![0]
QXlsx::Document xlsx;
//![0]
//![1]
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));
//![1]
//![2]
xlsx.save();
//![2]
return 0;
}
内容一致:
#include
#include "xlsxdocument.h"
#include "xlsxformat.h"
#include "xlsxcellrange.h"
#include "xlsxworksheet.h"
QTXLSX_USE_NAMESPACE
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
// Select a proper locale
// QLocale::setDefault(QLocale(QLocale::English));
Document xlsx;
QDate today(QDate::currentDate());
for (int month = 1; month <= 12; ++month) {
xlsx.addSheet(QLocale().monthName(month));
xlsx.currentWorksheet()->setGridLinesVisible(false);
// the header row
Format 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 titles
for (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 day
for (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 day
for (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");
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
using namespace QXlsx;
int main()
{
//![0]
Document xlsx;
for (int i = 1; i < 10; ++i) {
xlsx.write(i, 1, i * i * i); // A1:A9
xlsx.write(i, 2, i * i); // B1:B9
xlsx.write(i, 3, i * i - 1); // C1:C9
}
//![0]
//![1]
Chart *pieChart = xlsx.insertChart(3, 3, QSize(300, 300));
pieChart->setChartType(Chart::CT_Pie);
pieChart->addSeries(CellRange("A1:A9"));
pieChart->addSeries(CellRange("B1:B9"));
pieChart->addSeries(CellRange("C1:C9"));
Chart *pie3DChart = xlsx.insertChart(3, 9, QSize(300, 300));
pie3DChart->setChartType(Chart::CT_Pie3D);
pie3DChart->addSeries(CellRange("A1:C9"));
Chart *barChart = xlsx.insertChart(23, 3, QSize(300, 300));
barChart->setChartType(Chart::CT_Bar);
barChart->addSeries(CellRange("A1:C9"));
Chart *bar3DChart = xlsx.insertChart(23, 9, QSize(300, 300));
bar3DChart->setChartType(Chart::CT_Bar3D);
bar3DChart->addSeries(CellRange("A1:C9"));
Chart *lineChart = xlsx.insertChart(43, 3, QSize(300, 300));
lineChart->setChartType(Chart::CT_Line);
lineChart->addSeries(CellRange("A1:C9"));
Chart *line3DChart = xlsx.insertChart(43, 9, QSize(300, 300));
line3DChart->setChartType(Chart::CT_Line3D);
line3DChart->addSeries(CellRange("A1:C9"));
Chart *areaChart = xlsx.insertChart(63, 3, QSize(300, 300));
areaChart->setChartType(Chart::CT_Area);
areaChart->addSeries(CellRange("A1:C9"));
Chart *area3DChart = xlsx.insertChart(63, 9, QSize(300, 300));
area3DChart->setChartType(Chart::CT_Area3D);
area3DChart->addSeries(CellRange("A1:C9"));
Chart *scatterChart = xlsx.insertChart(83, 3, QSize(300, 300));
scatterChart->setChartType(Chart::CT_Scatter);
// Will generate three lines.
scatterChart->addSeries(CellRange("A1:A9"));
scatterChart->addSeries(CellRange("B1:B9"));
scatterChart->addSeries(CellRange("C1:C9"));
Chart *scatterChart_2 = xlsx.insertChart(83, 9, QSize(300, 300));
scatterChart_2->setChartType(Chart::CT_Scatter);
// Will generate two lines.
scatterChart_2->addSeries(CellRange("A1:C9"));
Chart *doughnutChart = xlsx.insertChart(103, 3, QSize(300, 300));
doughnutChart->setChartType(Chart::CT_Doughnut);
doughnutChart->addSeries(CellRange("A1:C9"));
//![1]
//![2]
xlsx.saveAs("Book1.xlsx");
//![2]
Document xlsx2("Book1.xlsx");
xlsx2.saveAs("Book2.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxchartsheet.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
using namespace QXlsx;
int main()
{
//![0]
Document xlsx;
for (int i = 1; i < 10; ++i)
xlsx.write(i, 1, i * i);
//![0]
//![1]
xlsx.addSheet("Chart1", AbstractSheet::ST_ChartSheet);
Chartsheet *sheet = static_cast<Chartsheet *>(xlsx.currentSheet());
Chart *barChart = sheet->chart();
barChart->setChartType(Chart::CT_Bar);
barChart->addSeries(CellRange("A1:A9"), xlsx.sheet("Sheet1"));
//![1]
//![2]
xlsx.saveAs("Book1.xlsx");
//![2]
Document xlsx2("Book1.xlsx");
xlsx2.saveAs("Book2.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxconditionalformatting.h"
using namespace QXlsx;
int main()
{
//![0]
Document xlsx;
Format hFmt;
hFmt.setFontBold(true);
xlsx.write("B1", "(-inf,40)", hFmt);
xlsx.write("C1", "[30,70]", hFmt);
xlsx.write("D1", "startsWith 2", hFmt);
xlsx.write("E1", "dataBar", hFmt);
xlsx.write("F1", "colorScale", hFmt);
for (int row = 3; row < 22; ++row) {
for (int col = 2; col < 22; ++col)
xlsx.write(row, col, qrand() % 100);
}
//![0]
//![cf1]
ConditionalFormatting cf1;
Format fmt1;
fmt1.setFontColor(Qt::green);
fmt1.setBorderStyle(Format::BorderDashed);
cf1.addHighlightCellsRule(ConditionalFormatting::Highlight_LessThan, "40", fmt1);
cf1.addRange("B3:B21");
xlsx.addConditionalFormatting(cf1);
//![cf1]
//![cf2]
ConditionalFormatting cf2;
Format fmt2;
fmt2.setBorderStyle(Format::BorderDotted);
fmt2.setBorderColor(Qt::blue);
cf2.addHighlightCellsRule(ConditionalFormatting::Highlight_Between, "30", "70", fmt2);
cf2.addRange("C3:C21");
xlsx.addConditionalFormatting(cf2);
//![cf2]
//![cf3]
ConditionalFormatting cf3;
Format fmt3;
fmt3.setFontStrikeOut(true);
fmt3.setFontBold(true);
cf3.addHighlightCellsRule(ConditionalFormatting::Highlight_BeginsWith, "2", fmt3);
cf3.addRange("D3:D21");
xlsx.addConditionalFormatting(cf3);
//![cf3]
//![cf4]
ConditionalFormatting cf4;
cf4.addDataBarRule(Qt::blue);
cf4.addRange("E3:E21");
xlsx.addConditionalFormatting(cf4);
//![cf4]
//![cf5]
ConditionalFormatting cf5;
cf5.add2ColorScaleRule(Qt::blue, Qt::red);
cf5.addRange("F3:F21");
xlsx.addConditionalFormatting(cf5);
//![cf5]
//![2]
xlsx.saveAs("Book1.xlsx");
//![2]
Document xlsx2("Book1.xlsx");
xlsx2.saveAs("Book2.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxdatavalidation.h"
QTXLSX_USE_NAMESPACE
int main()
{
Document xlsx;
xlsx.write("A1", "A2 and A3:E5 only accept the number between 33 and 99");
//![1]
DataValidation validation(DataValidation::Whole, DataValidation::Between, "33", "99");
validation.addRange("A2");
validation.addRange("A3:E5");
validation.setPromptMessage("Please Input Integer between 33 and 99");
xlsx.addDataValidation(validation);
//![1]
xlsx.saveAs("Book1.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
QTXLSX_USE_NAMESPACE
int main()
{
//![0]
Document xlsx;
for (int i = 1; i <= 10; ++i) {
xlsx.write(i, 1, i);
xlsx.write(i, 2, i * i);
xlsx.write(i, 3, i * i * i);
}
//![0]
//![1]
xlsx.defineName("MyCol_1", "=Sheet1!$A$1:$A$10");
xlsx.defineName("MyCol_2", "=Sheet1!$B$1:$B$10", "This is comments");
xlsx.defineName("MyCol_3", "=Sheet1!$C$1:$C$10", "", "Sheet1");
xlsx.defineName("Factor", "=0.5");
//![1]
//![2]
xlsx.write(11, 1, "=SUM(MyCol_1)");
xlsx.write(11, 2, "=SUM(MyCol_2)");
xlsx.write(11, 3, "=SUM(MyCol_3)");
xlsx.write(12, 1, "=SUM(MyCol_1)*Factor");
xlsx.write(12, 2, "=SUM(MyCol_2)*Factor");
xlsx.write(12, 3, "=SUM(MyCol_3)*Factor");
//![2]
xlsx.saveAs("Book1.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxformat.h"
#include "xlsxcellrange.h"
#include "xlsxworksheet.h"
QTXLSX_USE_NAMESPACE
void writeHorizontalAlignCell(Document &xlsx, const QString &cell, const QString &text,
Format::HorizontalAlignment align)
{
Format format;
format.setHorizontalAlignment(align);
format.setBorderStyle(Format::BorderThin);
xlsx.write(cell, text, format);
}
void writeVerticalAlignCell(Document &xlsx, const QString &range, const QString &text,
Format::VerticalAlignment align)
{
Format format;
format.setVerticalAlignment(align);
format.setBorderStyle(Format::BorderThin);
CellRange r(range);
xlsx.write(r.firstRow(), r.firstColumn(), text);
xlsx.mergeCells(r, format);
}
void writeBorderStyleCell(Document &xlsx, const QString &cell, const QString &text,
Format::BorderStyle bs)
{
Format format;
format.setBorderStyle(bs);
xlsx.write(cell, text, format);
}
void writeSolidFillCell(Document &xlsx, const QString &cell, const QColor &color)
{
Format format;
format.setPatternBackgroundColor(color);
xlsx.write(cell, QVariant(), format);
}
void writePatternFillCell(Document &xlsx, const QString &cell, Format::FillPattern pattern,
const QColor &color)
{
Format format;
format.setPatternForegroundColor(color);
format.setFillPattern(pattern);
xlsx.write(cell, QVariant(), format);
}
void writeBorderAndFontColorCell(Document &xlsx, const QString &cell, const QString &text,
const QColor &color)
{
Format format;
format.setBorderStyle(Format::BorderThin);
format.setBorderColor(color);
format.setFontColor(color);
xlsx.write(cell, text, format);
}
void writeFontNameCell(Document &xlsx, const QString &cell, const QString &text)
{
Format format;
format.setFontName(text);
format.setFontSize(16);
xlsx.write(cell, text, format);
}
void writeFontSizeCell(Document &xlsx, const QString &cell, int size)
{
Format format;
format.setFontSize(size);
xlsx.write(cell, "Qt Xlsx", format);
}
void writeInternalNumFormatsCell(Document &xlsx, int row, double value, int numFmt)
{
Format format;
format.setNumberFormatIndex(numFmt);
xlsx.write(row, 1, value);
xlsx.write(row, 2, QString("Builtin NumFmt %1").arg(numFmt));
xlsx.write(row, 3, value, format);
}
void writeCustomNumFormatsCell(Document &xlsx, int row, double value, const QString &numFmt)
{
Format format;
format.setNumberFormat(numFmt);
xlsx.write(row, 1, value);
xlsx.write(row, 2, numFmt);
xlsx.write(row, 3, value, format);
}
int main()
{
Document xlsx;
//---------------------------------------------------------------
// Create the first sheet (Otherwise, default "Sheet1" will be created)
xlsx.addSheet("Aligns & Borders");
xlsx.setColumnWidth(2, 20); // Column B
xlsx.setColumnWidth(8, 12); // Column H
xlsx.currentWorksheet()->setGridLinesVisible(false);
// Alignment
writeHorizontalAlignCell(xlsx, "B3", "AlignLeft", Format::AlignLeft);
writeHorizontalAlignCell(xlsx, "B5", "AlignHCenter", Format::AlignHCenter);
writeHorizontalAlignCell(xlsx, "B7", "AlignRight", Format::AlignRight);
writeVerticalAlignCell(xlsx, "D3:D7", "AlignTop", Format::AlignTop);
writeVerticalAlignCell(xlsx, "F3:F7", "AlignVCenter", Format::AlignVCenter);
writeVerticalAlignCell(xlsx, "H3:H7", "AlignBottom", Format::AlignBottom);
// Border
writeBorderStyleCell(xlsx, "B13", "BorderMedium", Format::BorderMedium);
writeBorderStyleCell(xlsx, "B15", "BorderDashed", Format::BorderDashed);
writeBorderStyleCell(xlsx, "B17", "BorderDotted", Format::BorderDotted);
writeBorderStyleCell(xlsx, "B19", "BorderThick", Format::BorderThick);
writeBorderStyleCell(xlsx, "B21", "BorderDouble", Format::BorderDouble);
writeBorderStyleCell(xlsx, "B23", "BorderDashDot", Format::BorderDashDot);
// Fill
writeSolidFillCell(xlsx, "D13", Qt::red);
writeSolidFillCell(xlsx, "D15", Qt::blue);
writeSolidFillCell(xlsx, "D17", Qt::yellow);
writeSolidFillCell(xlsx, "D19", Qt::magenta);
writeSolidFillCell(xlsx, "D21", Qt::green);
writeSolidFillCell(xlsx, "D23", Qt::gray);
writePatternFillCell(xlsx, "F13", Format::PatternMediumGray, Qt::red);
writePatternFillCell(xlsx, "F15", Format::PatternDarkHorizontal, Qt::blue);
writePatternFillCell(xlsx, "F17", Format::PatternDarkVertical, Qt::yellow);
writePatternFillCell(xlsx, "F19", Format::PatternDarkDown, Qt::magenta);
writePatternFillCell(xlsx, "F21", Format::PatternLightVertical, Qt::green);
writePatternFillCell(xlsx, "F23", Format::PatternLightTrellis, Qt::gray);
writeBorderAndFontColorCell(xlsx, "H13", "Qt::red", Qt::red);
writeBorderAndFontColorCell(xlsx, "H15", "Qt::blue", Qt::blue);
writeBorderAndFontColorCell(xlsx, "H17", "Qt::yellow", Qt::yellow);
writeBorderAndFontColorCell(xlsx, "H19", "Qt::magenta", Qt::magenta);
writeBorderAndFontColorCell(xlsx, "H21", "Qt::green", Qt::green);
writeBorderAndFontColorCell(xlsx, "H23", "Qt::gray", Qt::gray);
//---------------------------------------------------------------
// Create the second sheet.
xlsx.addSheet("Fonts");
xlsx.write("B3", "Normal");
Format font_bold;
font_bold.setFontBold(true);
xlsx.write("B4", "Bold", font_bold);
Format font_italic;
font_italic.setFontItalic(true);
xlsx.write("B5", "Italic", font_italic);
Format font_underline;
font_underline.setFontUnderline(Format::FontUnderlineSingle);
xlsx.write("B6", "Underline", font_underline);
Format font_strikeout;
font_strikeout.setFontStrikeOut(true);
xlsx.write("B7", "StrikeOut", font_strikeout);
writeFontNameCell(xlsx, "D3", "Arial");
writeFontNameCell(xlsx, "D4", "Arial Black");
writeFontNameCell(xlsx, "D5", "Comic Sans MS");
writeFontNameCell(xlsx, "D6", "Courier New");
writeFontNameCell(xlsx, "D7", "Impact");
writeFontNameCell(xlsx, "D8", "Times New Roman");
writeFontNameCell(xlsx, "D9", "Verdana");
writeFontSizeCell(xlsx, "G3", 10);
writeFontSizeCell(xlsx, "G4", 12);
writeFontSizeCell(xlsx, "G5", 14);
writeFontSizeCell(xlsx, "G6", 16);
writeFontSizeCell(xlsx, "G7", 18);
writeFontSizeCell(xlsx, "G8", 20);
writeFontSizeCell(xlsx, "G9", 25);
Format font_vertical;
font_vertical.setRotation(255);
font_vertical.setFontSize(16);
xlsx.write("J3", "vertical", font_vertical);
xlsx.mergeCells("J3:J9");
//---------------------------------------------------------------
// Create the third sheet.
xlsx.addSheet("Formulas");
xlsx.setColumnWidth(1, 2, 40);
Format rAlign;
rAlign.setHorizontalAlignment(Format::AlignRight);
Format lAlign;
lAlign.setHorizontalAlignment(Format::AlignLeft);
xlsx.write("B3", 40, lAlign);
xlsx.write("B4", 30, lAlign);
xlsx.write("B5", 50, lAlign);
xlsx.write("A7", "SUM(B3:B5)=", rAlign);
xlsx.write("B7", "=SUM(B3:B5)", lAlign);
xlsx.write("A8", "AVERAGE(B3:B5)=", rAlign);
xlsx.write("B8", "=AVERAGE(B3:B5)", lAlign);
xlsx.write("A9", "MAX(B3:B5)=", rAlign);
xlsx.write("B9", "=MAX(B3:B5)", lAlign);
xlsx.write("A10", "MIN(B3:B5)=", rAlign);
xlsx.write("B10", "=MIN(B3:B5)", lAlign);
xlsx.write("A11", "COUNT(B3:B5)=", rAlign);
xlsx.write("B11", "=COUNT(B3:B5)", lAlign);
xlsx.write("A13", "IF(B7>100,\"large\",\"small\")=", rAlign);
xlsx.write("B13", "=IF(B7>100,\"large\",\"small\")", lAlign);
xlsx.write("A15", "SQRT(25)=", rAlign);
xlsx.write("B15", "=SQRT(25)", lAlign);
xlsx.write("A16", "RAND()=", rAlign);
xlsx.write("B16", "=RAND()", lAlign);
xlsx.write("A17", "2*PI()=", rAlign);
xlsx.write("B17", "=2*PI()", lAlign);
xlsx.write("A19", "UPPER(\"qtxlsx\")=", rAlign);
xlsx.write("B19", "=UPPER(\"qtxlsx\")", lAlign);
xlsx.write("A20", "LEFT(\"ubuntu\",3)=", rAlign);
xlsx.write("B20", "=LEFT(\"ubuntu\",3)", lAlign);
xlsx.write("A21", "LEN(\"Hello Qt!\")=", rAlign);
xlsx.write("B21", "=LEN(\"Hello Qt!\")", lAlign);
Format dateFormat;
dateFormat.setHorizontalAlignment(Format::AlignLeft);
dateFormat.setNumberFormat("yyyy-mm-dd");
xlsx.write("A23", "DATE(2013,8,13)=", rAlign);
xlsx.write("B23", "=DATE(2013,8,13)", dateFormat);
xlsx.write("A24", "DAY(B23)=", rAlign);
xlsx.write("B24", "=DAY(B23)", lAlign);
xlsx.write("A25", "MONTH(B23)=", rAlign);
xlsx.write("B25", "=MONTH(B23)", lAlign);
xlsx.write("A26", "YEAR(B23)=", rAlign);
xlsx.write("B26", "=YEAR(B23)", lAlign);
xlsx.write("A27", "DAYS360(B23,TODAY())=", rAlign);
xlsx.write("B27", "=DAYS360(B23,TODAY())", lAlign);
xlsx.write("A29", "B3+100*(2-COS(0)))=", rAlign);
xlsx.write("B29", "=B3+100*(2-COS(0))", lAlign);
xlsx.write("A30", "ISNUMBER(B29)=", rAlign);
xlsx.write("B30", "=ISNUMBER(B29)", lAlign);
xlsx.write("A31", "AND(1,0)=", rAlign);
xlsx.write("B31", "=AND(1,0)", lAlign);
xlsx.write("A33", "HYPERLINK(\"http://qt-project.org\")=", rAlign);
xlsx.write("B33", "=HYPERLINK(\"http://qt-project.org\")", lAlign);
//---------------------------------------------------------------
// Create the fourth sheet.
xlsx.addSheet("NumFormats");
xlsx.setColumnWidth(2, 40);
writeInternalNumFormatsCell(xlsx, 4, 2.5681, 2);
writeInternalNumFormatsCell(xlsx, 5, 2500000, 3);
writeInternalNumFormatsCell(xlsx, 6, -500, 5);
writeInternalNumFormatsCell(xlsx, 7, -0.25, 9);
writeInternalNumFormatsCell(xlsx, 8, 890, 11);
writeInternalNumFormatsCell(xlsx, 9, 0.75, 12);
writeInternalNumFormatsCell(xlsx, 10, 41499, 14);
writeInternalNumFormatsCell(xlsx, 11, 41499, 17);
writeCustomNumFormatsCell(xlsx, 13, 20.5627, "#.###");
writeCustomNumFormatsCell(xlsx, 14, 4.8, "#.00");
writeCustomNumFormatsCell(xlsx, 15, 1.23, "0.00 \"RMB\"");
writeCustomNumFormatsCell(xlsx, 16, 60, "[Red][<=100];[Green][>100]");
//---------------------------------------------------------------
// Create the fifth sheet.
xlsx.addSheet("Merging");
Format centerAlign;
centerAlign.setHorizontalAlignment(Format::AlignHCenter);
centerAlign.setVerticalAlignment(Format::AlignVCenter);
xlsx.write("B4", "Hello Qt!");
xlsx.mergeCells("B4:F6", centerAlign);
xlsx.write("B8", 1);
xlsx.mergeCells("B8:C21", centerAlign);
xlsx.write("E8", 2);
xlsx.mergeCells("E8:F21", centerAlign);
//---------------------------------------------------------------
// Create the fifth sheet.
xlsx.addSheet("Grouping");
qsrand(QDateTime::currentMSecsSinceEpoch());
for (int row = 2; row < 31; ++row) {
for (int col = 1; col <= 10; ++col)
xlsx.write(row, col, qrand() % 100);
}
xlsx.groupRows(4, 7);
xlsx.groupRows(11, 26, false);
xlsx.groupRows(15, 17, false);
xlsx.groupRows(20, 22, false);
xlsx.setColumnWidth(1, 10, 10.0);
xlsx.groupColumns(1, 2);
xlsx.groupColumns(5, 8, false);
xlsx.saveAs("Book1.xlsx");
// Make sure that read/write works well.
Document xlsx2("Book1.xlsx");
xlsx2.saveAs("Book2.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
int main()
{
QXlsx::Document xlsx;
xlsx.write("A1", "View the properties through:");
xlsx.write("A2", "Office Button -> Prepare -> Properties option in Excel");
xlsx.setDocumentProperty("title", "This is an example spreadsheet");
xlsx.setDocumentProperty("subject", "With document properties");
xlsx.setDocumentProperty("creator", "Debao Zhang");
xlsx.setDocumentProperty("company", "HMICN");
xlsx.setDocumentProperty("category", "Example spreadsheets");
xlsx.setDocumentProperty("keywords", "Sample, Example, Properties");
xlsx.setDocumentProperty("description", "Created with Qt Xlsx");
xlsx.saveAs("Test.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
int main()
{
{
// Create a new .xlsx file.
QXlsx::Document xlsx;
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.saveAs("Book1.xlsx");
}
//![0]
QXlsx::Document xlsx("Book1.xlsx");
//![0]
//![1]
qDebug() << xlsx.read("A1");
qDebug() << xlsx.read("A2");
qDebug() << xlsx.read("A3");
qDebug() << xlsx.read("A4");
qDebug() << xlsx.read("A5");
qDebug() << xlsx.read("A6");
qDebug() << xlsx.read("A7");
//![1]
//![2]
for (int row = 1; row < 10; ++row) {
if (QXlsx::Cell *cell = xlsx.cellAt(row, 1))
qDebug() << cell->value();
}
//![2]
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxformat.h"
#include "xlsxworksheet.h"
#include "xlsxcellformula.h"
QTXLSX_USE_NAMESPACE
int main()
{
//![0]
Document xlsx;
//![0]
//![1]
xlsx.setColumnWidth(1, 2, 40);
Format rAlign;
rAlign.setHorizontalAlignment(Format::AlignRight);
Format lAlign;
lAlign.setHorizontalAlignment(Format::AlignLeft);
xlsx.write("B3", 40, lAlign);
xlsx.write("B4", 30, lAlign);
xlsx.write("B5", 50, lAlign);
xlsx.write("A7", "SUM(B3:B5)=", rAlign);
xlsx.write("B7", "=SUM(B3:B5)", lAlign);
xlsx.write("A8", "AVERAGE(B3:B5)=", rAlign);
xlsx.write("B8", "=AVERAGE(B3:B5)", lAlign);
xlsx.write("A9", "MAX(B3:B5)=", rAlign);
xlsx.write("B9", "=MAX(B3:B5)", lAlign);
xlsx.write("A10", "MIN(B3:B5)=", rAlign);
xlsx.write("B10", "=MIN(B3:B5)", lAlign);
xlsx.write("A11", "COUNT(B3:B5)=", rAlign);
xlsx.write("B11", "=COUNT(B3:B5)", lAlign);
xlsx.write("A13", "IF(B7>100,\"large\",\"small\")=", rAlign);
xlsx.write("B13", "=IF(B7>100,\"large\",\"small\")", lAlign);
xlsx.write("A15", "SQRT(25)=", rAlign);
xlsx.write("B15", "=SQRT(25)", lAlign);
xlsx.write("A16", "RAND()=", rAlign);
xlsx.write("B16", "=RAND()", lAlign);
xlsx.write("A17", "2*PI()=", rAlign);
xlsx.write("B17", "=2*PI()", lAlign);
xlsx.write("A19", "UPPER(\"qtxlsx\")=", rAlign);
xlsx.write("B19", "=UPPER(\"qtxlsx\")", lAlign);
xlsx.write("A20", "LEFT(\"ubuntu\",3)=", rAlign);
xlsx.write("B20", "=LEFT(\"ubuntu\",3)", lAlign);
xlsx.write("A21", "LEN(\"Hello Qt!\")=", rAlign);
xlsx.write("B21", "=LEN(\"Hello Qt!\")", lAlign);
//![1]
//![2]
xlsx.addSheet("ArrayFormula");
Worksheet *sheet = xlsx.currentWorksheet();
for (int row = 2; row < 20; ++row) {
sheet->write(row, 2, row * 2); // B2:B19
sheet->write(row, 3, row * 3); // C2:C19
}
sheet->writeFormula("D2", CellFormula("B2:B19+C2:C19", "D2:D19", CellFormula::ArrayType));
sheet->writeFormula("E2",
CellFormula("=CONCATENATE(\"The total is \",D2:D19,\" units\")", "E2:E19",
CellFormula::ArrayType));
//![2]
//![21]
xlsx.addSheet("SharedFormula");
sheet = xlsx.currentWorksheet();
for (int row = 2; row < 20; ++row) {
sheet->write(row, 2, row * 2); // B2:B19
sheet->write(row, 3, row * 3); // C2:C19
}
sheet->writeFormula("D2", CellFormula("=B2+C2", "D2:D19", CellFormula::SharedType));
sheet->writeFormula("E2",
CellFormula("=CONCATENATE(\"The total is \",D2,\" units\")", "E2:E19",
CellFormula::SharedType));
//![21]
//![3]
xlsx.save();
//![3]
// Make sure that read/write works well.
Document xlsx2("Book1.xlsx");
Worksheet *sharedFormulaSheet = dynamic_cast<Worksheet *>(xlsx2.sheet("SharedFormula"));
for (int row = 2; row < 20; ++row) {
qDebug() << sharedFormulaSheet->read(row, 4);
}
xlsx2.saveAs("Book2.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
int main()
{
//![0]
QXlsx::Document xlsx;
//![0]
//![1]
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));
//![1]
//![2]
xlsx.save();
//![2]
return 0;
}
#include
#include "xlsxdocument.h"
int main()
{
//![0]
QXlsx::Document xlsx;
//![0]
//![1]
xlsx.write("A1", "http://qt-project.org");
xlsx.write("A2", "http://qt-project.org/wiki#0f68b904e33d9ac04605aecc958bcf52");
xlsx.write("A3", "mailto:[email protected]");
xlsx.write("A4", "file:///C:/User/test/abc.txt");
//![1]
//![2]
xlsx.save();
//![2]
QXlsx::Document xlsx2("Book1.xlsx");
xlsx2.saveAs("Book2.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
int main(int argc, char **argv)
{
QGuiApplication(argc, argv);
QXlsx::Document xlsx;
QImage image(40, 30, QImage::Format_RGB32);
image.fill(Qt::green);
for (int i = 0; i < 10; ++i)
xlsx.insertImage(10 * i, 5, image);
xlsx.saveAs("Book1.xlsx");
QXlsx::Document xlsx2("Book1.xlsx");
xlsx2.saveAs("Book2.xlsx");
return 0;
}
#include "xlsxdocument.h"
#include "xlsxformat.h"
QTXLSX_USE_NAMESPACE
int main()
{
Document xlsx;
//![0]
Format format;
format.setHorizontalAlignment(Format::AlignHCenter);
format.setVerticalAlignment(Format::AlignVCenter);
//![0]
//![1]
xlsx.write("B4", "Hello Qt!");
xlsx.mergeCells("B4:F6", format);
xlsx.write("B8", 1);
xlsx.mergeCells("B8:C21", format);
xlsx.write("E8", 2);
xlsx.mergeCells("E8:F21", format);
//![1]
xlsx.save();
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxformat.h"
int main(int argc, char **argv)
{
QGuiApplication(argc, argv);
QXlsx::Document xlsx;
xlsx.setColumnWidth(1, 4, 20.0);
QXlsx::Format header;
header.setFontBold(true);
header.setFontSize(20);
// Custom number formats
QStringList numFormats;
numFormats << "Qt #"
<< "yyyy-mmm-dd"
<< "$ #,##0.00"
<< "[red]0.00";
xlsx.write(1, 1, "Raw data", header);
xlsx.write(1, 2, "Format", header);
xlsx.write(1, 3, "Shown value", header);
for (int i = 0; i < numFormats.size(); ++i) {
int row = i + 2;
xlsx.write(row, 1, 100.0);
xlsx.write(row, 2, numFormats[i]);
QXlsx::Format format;
format.setNumberFormat(numFormats[i]);
xlsx.write(row, 3, 100.0, format);
}
// Builtin number formats
xlsx.addSheet();
xlsx.setColumnWidth(1, 4, 20.0);
xlsx.write(1, 1, "Raw data", header);
xlsx.write(1, 2, "Builtin Format", header);
xlsx.write(1, 3, "Shown value", header);
for (int i = 0; i < 50; ++i) {
int row = i + 2;
int numFmt = i;
xlsx.write(row, 1, 100.0);
xlsx.write(row, 2, numFmt);
QXlsx::Format format;
format.setNumberFormatIndex(numFmt);
xlsx.write(row, 3, 100.0, format);
}
xlsx.save();
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
#include "xlsxformat.h"
int main()
{
//![0]
QXlsx::Document xlsx;
//![0]
//![1]
QXlsx::Format blue;
blue.setFontColor(Qt::blue);
QXlsx::Format red;
red.setFontColor(Qt::red);
red.setFontSize(15);
QXlsx::Format bold;
bold.setFontBold(true);
QXlsx::RichString rich;
rich.addFragment("Hello ", blue);
rich.addFragment("Qt ", red);
rich.addFragment("Xlsx", bold);
xlsx.write("B2", rich);
xlsx.workbook()->setHtmlToRichStringEnabled(true);
xlsx.write("B4", "Hello Qt Xlsx");
xlsx.write("B6", "Qt Xlsx");
//![1]
//![2]
xlsx.saveAs("Test1.xlsx");
//![2]
QXlsx::Document("Test1.xlsx");
xlsx.saveAs("Test2.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxformat.h"
int main()
{
QXlsx::Document xlsx;
xlsx.write(1, 2, "Row:0, Col:2 ==> (C1)");
// Set the height of the first row to 50.0(points)
xlsx.setRowHeight(1, 50.0);
// Set the width of the third column to 40.0(chars)
xlsx.setColumnWidth(3, 3, 40.0);
// Set style for the row 11th.
QXlsx::Format format1;
format1.setFontBold(true);
format1.setFontColor(QColor(Qt::blue));
format1.setFontSize(20);
xlsx.write(11, 1, "Hello Row Style");
xlsx.write(11, 6, "Blue Color");
xlsx.setRowFormat(11, format1);
xlsx.setRowHeight(11, 41);
// Set style for the col [9th, 16th)
QXlsx::Format format2;
format2.setFontBold(true);
format2.setFontColor(QColor(Qt::magenta));
for (int row = 12; row <= 30; row++)
for (int col = 9; col <= 15; col++)
xlsx.write(row, col, row + col);
xlsx.setColumnWidth(9, 16, 5.0);
xlsx.setColumnFormat(9, 16, format2);
xlsx.save();
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxformat.h"
int main()
{
QXlsx::Document xlsx;
QXlsx::Format format1;
format1.setFontColor(QColor(Qt::red));
format1.setFontSize(15);
format1.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
format1.setBorderStyle(QXlsx::Format::BorderDashDotDot);
xlsx.write("A1", "Hello Qt!", format1);
xlsx.write("B3", 12345, format1);
QXlsx::Format format2;
format2.setFontBold(true);
format2.setFontUnderline(QXlsx::Format::FontUnderlineDouble);
format2.setFillPattern(QXlsx::Format::PatternLightUp);
xlsx.write("C5", "=44+33", format2);
xlsx.write("D7", true, format2);
QXlsx::Format format3;
format3.setFontBold(true);
format3.setFontColor(QColor(Qt::blue));
format3.setFontSize(20);
xlsx.write(11, 1, "Hello Row Style");
xlsx.write(11, 6, "Blue Color");
xlsx.setRowFormat(11, 41, format3);
QXlsx::Format format4;
format4.setFontBold(true);
format4.setFontColor(QColor(Qt::magenta));
for (int row = 21; row <= 40; row++)
for (int col = 9; col < 16; col++)
xlsx.write(row, col, row + col);
xlsx.setColumnFormat(9, 16, format4);
xlsx.write("A5", QDate(2013, 8, 29));
QXlsx::Format format6;
format6.setPatternBackgroundColor(QColor(Qt::green));
xlsx.write("A6", "Background color: green", format6);
xlsx.saveAs("book1.xlsx");
return 0;
}
#include
#include "xlsxdocument.h"
#include "xlsxabstractsheet.h"
QTXLSX_USE_NAMESPACE
int main()
{
//![Create a xlsx file]
Document xlsx;
for (int i = 1; i < 20; ++i) {
for (int j = 1; j < 15; ++j)
xlsx.write(i, j, QString("R %1 C %2").arg(i).arg(j));
}
xlsx.addSheet();
xlsx.write(2, 2, "Hello Qt Xlsx");
xlsx.addSheet();
xlsx.write(3, 3, "This will be deleted...");
xlsx.addSheet("HiddenSheet");
xlsx.currentSheet()->setHidden(true);
xlsx.write("A1", "This sheet is hidden.");
xlsx.addSheet("VeryHiddenSheet");
xlsx.sheet("VeryHiddenSheet")->setSheetState(AbstractSheet::SS_VeryHidden);
xlsx.write("A1", "This sheet is very hidden.");
xlsx.save(); // Default name is "Book1.xlsx"
//![Create a xlsx file]
Document xlsx2("Book1.xlsx");
//![add_copy_move_delete]
xlsx2.renameSheet("Sheet1", "TheFirstSheet");
xlsx2.copySheet("TheFirstSheet", "CopyOfTheFirst");
xlsx2.selectSheet("CopyOfTheFirst");
xlsx2.write(25, 2, "On the Copy Sheet");
xlsx2.deleteSheet("Sheet3");
xlsx2.moveSheet("Sheet2", 0);
//![add_copy_move_delete]
//![show_hidden_sheets]
xlsx2.sheet("HiddenSheet")->setVisible(true);
xlsx2.sheet("VeryHiddenSheet")->setVisible(true);
//![show_hidden_sheets]
xlsx2.saveAs("Book2.xlsx");
return 0;
}