Browse Source

Add a demo example

master
Debao Zhang 11 years ago
parent
commit
0a0e24960a
  1. 9
      examples/xlsx/demo/demo.pro
  2. BIN
      examples/xlsx/demo/doc/images/xlsx_demo.png
  3. 8
      examples/xlsx/demo/doc/src/demo.qdoc
  4. 120
      examples/xlsx/demo/main.cpp
  5. 3
      examples/xlsx/xlsx.pro
  6. 22
      src/xlsx/doc/src/qtxlsx-index.qdoc

9
examples/xlsx/demo/demo.pro

@ -0,0 +1,9 @@
TARGET = demo
#include(../../../src/xlsx/qtxlsx.pri)
QT+=xlsx
CONFIG += console
CONFIG -= app_bundle
SOURCES += main.cpp

BIN
examples/xlsx/demo/doc/images/xlsx_demo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

8
examples/xlsx/demo/doc/src/demo.qdoc

@ -0,0 +1,8 @@
/*!
\example demo
\title Xlsx Demo
\brief This is a demo which is used to show features of the library
\ingroup qtxlsx-examples
\image xlsx_demo.png
*/

120
examples/xlsx/demo/main.cpp

@ -0,0 +1,120 @@
#include <QtCore>
#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 = xlsx.createFormat();
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 = xlsx.createFormat();
format->setVerticalAlignment(align);
format->setBorderStyle(Format::BorderThin);
xlsx.mergeCells(range);
CellRange r(range);
for (int row=r.firstRow(); row<=r.lastRow(); ++row) {
for (int col=r.firstColumn(); col<=r.lastColumn(); ++col) {
if (row == r.firstRow() && col == r.firstColumn())
xlsx.write(row, col, text, format);
else
xlsx.write(row, col, QVariant(), format);
}
}
}
void writeBorderStyleCell(Document &xlsx, const QString &cell, const QString &text, Format::BorderStyle bs)
{
Format *format = xlsx.createFormat();
format->setBorderStyle(bs);
xlsx.write(cell, text, format);
}
void writeSolidFillCell(Document &xlsx, const QString &cell, const QColor &color)
{
Format *format = xlsx.createFormat();
format->setPatternBackgroundColor(color);
xlsx.write(cell, QVariant(), format);
}
void writePatternFillCell(Document &xlsx, const QString &cell, Format::FillPattern pattern, const QColor &color)
{
Format *format = xlsx.createFormat();
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 = xlsx.createFormat();
format->setBorderStyle(Format::BorderThin);
format->setBorderColor(color);
format->setFontColor(color);
xlsx.write(cell, text, format);
}
int main()
{
Document xlsx;
//The default sheet is "Sheet1"
xlsx.setSheetName("Aligns & Borders");
xlsx.setColumn("B", "B", 20);
xlsx.setColumn("H", "H", 12);
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);
xlsx.saveAs("Book1.xlsx");
//Make sure that read/write works well.
Document xlsx2("Book1.xlsx");
xlsx2.saveAs("Book2.xlsx");
return 0;
}

3
examples/xlsx/xlsx.pro

@ -7,5 +7,6 @@ SUBDIRS = hello style \
numberformat \ numberformat \
readwrite \ readwrite \
datavalidation \ datavalidation \
definename definename \
demo

22
src/xlsx/doc/src/qtxlsx-index.qdoc

@ -28,6 +28,17 @@
\page index.html \page index.html
\brief Qt Xlsx provides functionality for handling .xlsx files. \brief Qt Xlsx provides functionality for handling .xlsx files.
The \l{Qt Xlsx C++ Classes}{Qt Xlsx Module} provides a set of classes to 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
\list
\li Generate a new .xlsx file from scratch
\li Extract data from an existing .xlsx file
\li Edit an existing .xlsx file
\endlist
\image xlsx_demo.png
\table \table
\row \row
\li Source code: \li \l{https://github.com/dbzhang800/QtXlsxWriter} \li Source code: \li \l{https://github.com/dbzhang800/QtXlsxWriter}
@ -37,17 +48,6 @@
\li License: \li MIT \li License: \li MIT
\endtable \endtable
The \l{Qt Xlsx C++ Classes}{Qt Xlsx Module} provides a set of classes to 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
\list
\li Generate a new .xlsx file from scratch
\li Extract data from an existing .xlsx file
\li Edit an existing .xlsx file
\endlist
\section1 Getting Started \section1 Getting Started
To include the definitions of the module's classes, using the following directive: To include the definitions of the module's classes, using the following directive:

Loading…
Cancel
Save