Browse Source

Add format param to the mergeCells() API

master
Debao Zhang 11 years ago
parent
commit
5a035ede93
  1. 26
      examples/xlsx/demo/main.cpp
  2. BIN
      examples/xlsx/mergecells/doc/images/xlsx-mergecells.png
  3. 19
      examples/xlsx/mergecells/doc/src/mergecells.qdoc
  4. 25
      examples/xlsx/mergecells/main.cpp
  5. 31
      src/xlsx/xlsxdocument.cpp
  6. 4
      src/xlsx/xlsxdocument.h
  7. 55
      src/xlsx/xlsxworksheet.cpp
  8. 6
      src/xlsx/xlsxworksheet.h

26
examples/xlsx/demo/main.cpp

@ -19,17 +19,9 @@ void writeVerticalAlignCell(Document &xlsx, const QString &range, const QString
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);
}
}
xlsx.write(r.firstRow(), r.firstColumn(), text);
xlsx.mergeCells(r, format);
}
void writeBorderStyleCell(Document &xlsx, const QString &cell, const QString &text, Format::BorderStyle bs)
@ -83,7 +75,7 @@ void writeInternalNumFormatsCell(Document &xlsx, int row, double value, int numF
Format *format = xlsx.createFormat();
format->setNumberFormatIndex(numFmt);
xlsx.write(row, 0, value);
xlsx.write(row, 1, numFmt);
xlsx.write(row, 1, QString("Builtin NumFmt %1").arg(numFmt));
xlsx.write(row, 2, value, format);
}
@ -265,6 +257,18 @@ int main()
writeCustomNumFormatsCell(xlsx, 14, 1.23, "0.00 \"RMB\"");
writeCustomNumFormatsCell(xlsx, 15, 60, "[Red][<=100];[Green][>100]");
//---------------------------------------------------------------
//Create the fifth sheet.
xlsx.addWorksheet("Merging");
Format *centerAlign = xlsx.createFormat();
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);
xlsx.saveAs("Book1.xlsx");

BIN
examples/xlsx/mergecells/doc/images/xlsx-mergecells.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

19
examples/xlsx/mergecells/doc/src/mergecells.qdoc

@ -0,0 +1,19 @@
/*!
\example mergecells
\title Merge Cells Example
\brief Demonstrates how to merge cells
\ingroup qtxlsx-examples
This example demonstrates how to generate a
simplest .xlsx file which contians merged cells with
Qt Xlsx Library.
\image xlsx-mergecells.png
Create an format which will be applied to the merged cells:
\snippet mergecells/main.cpp 0
Merge cells.
\snippet mergecells/main.cpp 1
*/

25
examples/xlsx/mergecells/main.cpp

@ -1,15 +1,24 @@
#include "xlsxdocument.h"
#include "xlsxformat.h"
QTXLSX_USE_NAMESPACE
int main()
{
QXlsx::Document xlsx;
xlsx.write("B1", "Merge Cells");
xlsx.mergeCells("B1:B5");
xlsx.write("E2", "Merge Cells 2");
xlsx.mergeCells("E2:G4");
Document xlsx;
//![0]
Format *format = xlsx.createFormat();
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;

31
src/xlsx/xlsxdocument.cpp

@ -142,21 +142,44 @@ int Document::insertImage(int row, int column, const QImage &image, double xOffs
}
/*!
* Merge cell \a range.
Merge a \a range of cells. The first cell should contain the data and the others should
be blank. All cells will be applied the same style if a valid \a format is given.
\note All cells except the top-left one will be cleared.
*/
int Document::mergeCells(const QString &range)
int Document::mergeCells(const CellRange &range, Format *format)
{
return currentWorksheet()->mergeCells(range);
return currentWorksheet()->mergeCells(range, format);
}
/*!
* Unmerge cell \a range.
\overload
Merge a \a range of cells. The first cell should contain the data and the others should
be blank. All cells will be applied the same style if a valid \a format is given.
\note All cells except the top-left one will be cleared.
*/
int Document::mergeCells(const QString &range, Format *format)
{
return currentWorksheet()->mergeCells(range, format);
}
/*!
Unmerge the cells in the \a range.
*/
int Document::unmergeCells(const QString &range)
{
return currentWorksheet()->unmergeCells(range);
}
/*!
Unmerge the cells in the \a range.
*/
int Document::unmergeCells(const CellRange &range)
{
return currentWorksheet()->unmergeCells(range);
}
/*!
* \brief Set properties for a row of cells.
* \param row The worksheet row (zero indexed).

4
src/xlsx/xlsxdocument.h

@ -58,7 +58,9 @@ public:
int write(const QString &cell, const QVariant &value, Format *format=0);
int write(int row, int col, const QVariant &value, Format *format=0);
int insertImage(int row, int column, const QImage &image, double xOffset=0, double yOffset=0, double xScale=1, double yScale=1);
int mergeCells(const QString &range);
int mergeCells(const CellRange &range, Format *format=0);
int mergeCells(const QString &range, Format *format=0);
int unmergeCells(const CellRange &range);
int unmergeCells(const QString &range);
bool setRow(int row, double height, Format* format=0, bool hidden=false);
bool setRow(const QString &row, double height, Format* format=0, bool hidden=false);

55
src/xlsx/xlsxworksheet.cpp

@ -679,7 +679,13 @@ int Worksheet::insertImage(int row, int column, const QImage &image, const QPoin
return 0;
}
int Worksheet::mergeCells(const CellRange &range)
/*!
Merge a \a range of cells. The first cell should contain the data and the others should
be blank. All cells will be applied the same style if a valid \a format is given.
\note All cells except the top-left one will be cleared.
*/
int Worksheet::mergeCells(const CellRange &range, Format *format)
{
Q_D(Worksheet);
if (range.rowCount() < 2 && range.columnCount() < 2)
@ -688,11 +694,37 @@ int Worksheet::mergeCells(const CellRange &range)
if (d->checkDimensions(range.firstRow(), range.firstColumn()))
return -1;
for (int row = range.firstRow(); row <= range.lastRow(); ++row) {
for (int col = range.firstColumn(); col <= range.lastColumn(); ++col) {
if (row == range.firstRow() && col == range.firstColumn()) {
Cell *cell = cellAt(row, col);
if (cell) {
if (format)
cell->d_ptr->format = format;
} else {
writeBlank(row, col, format);
}
} else {
writeBlank(row, col, format);
}
}
}
if (format)
d->workbook->styles()->addFormat(format);
d->merges.append(range);
return 0;
}
int Worksheet::mergeCells(const QString &range)
/*!
\overload
Merge a \a range of cells. The first cell should contain the data and the others should
be blank. All cells will be applied the same style if a valid \a format is given.
\note All cells except the top-left one will be cleared.
*/
int Worksheet::mergeCells(const QString &range, Format *format)
{
QStringList cells = range.split(QLatin1Char(':'));
if (cells.size() != 2)
@ -703,14 +735,12 @@ int Worksheet::mergeCells(const QString &range)
if (cell1 == QPoint(-1,-1) || cell2 == QPoint(-1, -1))
return -1;
return mergeCells(CellRange(cell1.x(), cell1.y(), cell2.x(), cell2.y()));
}
int Worksheet::mergeCells(int row_begin, int column_begin, int row_end, int column_end)
{
return mergeCells(CellRange(row_begin, column_begin, row_end, column_end));
return mergeCells(CellRange(cell1.x(), cell1.y(), cell2.x(), cell2.y()), format);
}
/*!
Unmerge the cells in the \a range.
*/
int Worksheet::unmergeCells(const CellRange &range)
{
Q_D(Worksheet);
@ -721,6 +751,10 @@ int Worksheet::unmergeCells(const CellRange &range)
return 0;
}
/*!
\overload
Unmerge the cells in the \a range.
*/
int Worksheet::unmergeCells(const QString &range)
{
QStringList cells = range.split(QLatin1Char(':'));
@ -735,11 +769,6 @@ int Worksheet::unmergeCells(const QString &range)
return unmergeCells(CellRange(cell1.x(), cell1.y(), cell2.x(), cell2.y()));
}
int Worksheet::unmergeCells(int row_begin, int column_begin, int row_end, int column_end)
{
return unmergeCells(CellRange(row_begin, column_begin, row_end, column_end));
}
void Worksheet::saveToXmlFile(QIODevice *device)
{
Q_D(Worksheet);

6
src/xlsx/xlsxworksheet.h

@ -71,10 +71,8 @@ public:
int insertImage(int row, int column, const QImage &image, const QPointF &offset=QPointF(), double xScale=1, double yScale=1);
int mergeCells(int row_begin, int column_begin, int row_end, int column_end);
int mergeCells(const QString &range);
int mergeCells(const CellRange &range);
int unmergeCells(int row_begin, int column_begin, int row_end, int column_end);
int mergeCells(const QString &range, Format *format=0);
int mergeCells(const CellRange &range, Format *format=0);
int unmergeCells(const QString &range);
int unmergeCells(const CellRange &range);

Loading…
Cancel
Save