diff --git a/examples/xlsx/demo/demo.pro b/examples/xlsx/demo/demo.pro new file mode 100644 index 0000000..10c4270 --- /dev/null +++ b/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 diff --git a/examples/xlsx/demo/doc/images/xlsx_demo.png b/examples/xlsx/demo/doc/images/xlsx_demo.png new file mode 100644 index 0000000..954e0eb Binary files /dev/null and b/examples/xlsx/demo/doc/images/xlsx_demo.png differ diff --git a/examples/xlsx/demo/doc/src/demo.qdoc b/examples/xlsx/demo/doc/src/demo.qdoc new file mode 100644 index 0000000..4970286 --- /dev/null +++ b/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 +*/ diff --git a/examples/xlsx/demo/main.cpp b/examples/xlsx/demo/main.cpp new file mode 100644 index 0000000..29a6c48 --- /dev/null +++ b/examples/xlsx/demo/main.cpp @@ -0,0 +1,120 @@ +#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 = 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; +} diff --git a/examples/xlsx/xlsx.pro b/examples/xlsx/xlsx.pro index decde8e..b337552 100644 --- a/examples/xlsx/xlsx.pro +++ b/examples/xlsx/xlsx.pro @@ -7,5 +7,6 @@ SUBDIRS = hello style \ numberformat \ readwrite \ datavalidation \ - definename + definename \ + demo diff --git a/src/xlsx/doc/src/qtxlsx-index.qdoc b/src/xlsx/doc/src/qtxlsx-index.qdoc index 9e43fd3..e5f4c53 100644 --- a/src/xlsx/doc/src/qtxlsx-index.qdoc +++ b/src/xlsx/doc/src/qtxlsx-index.qdoc @@ -28,6 +28,17 @@ \page index.html \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 \row \li Source code: \li \l{https://github.com/dbzhang800/QtXlsxWriter} @@ -37,17 +48,6 @@ \li License: \li MIT \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 To include the definitions of the module's classes, using the following directive: