Add format param to the mergeCells() API
This commit is contained in:
+15
-11
@@ -19,17 +19,9 @@ void writeVerticalAlignCell(Document &xlsx, const QString &range, const QString
|
|||||||
Format *format = xlsx.createFormat();
|
Format *format = xlsx.createFormat();
|
||||||
format->setVerticalAlignment(align);
|
format->setVerticalAlignment(align);
|
||||||
format->setBorderStyle(Format::BorderThin);
|
format->setBorderStyle(Format::BorderThin);
|
||||||
xlsx.mergeCells(range);
|
|
||||||
|
|
||||||
CellRange r(range);
|
CellRange r(range);
|
||||||
for (int row=r.firstRow(); row<=r.lastRow(); ++row) {
|
xlsx.write(r.firstRow(), r.firstColumn(), text);
|
||||||
for (int col=r.firstColumn(); col<=r.lastColumn(); ++col) {
|
xlsx.mergeCells(r, format);
|
||||||
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)
|
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 *format = xlsx.createFormat();
|
||||||
format->setNumberFormatIndex(numFmt);
|
format->setNumberFormatIndex(numFmt);
|
||||||
xlsx.write(row, 0, value);
|
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);
|
xlsx.write(row, 2, value, format);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,6 +257,18 @@ int main()
|
|||||||
writeCustomNumFormatsCell(xlsx, 14, 1.23, "0.00 \"RMB\"");
|
writeCustomNumFormatsCell(xlsx, 14, 1.23, "0.00 \"RMB\"");
|
||||||
writeCustomNumFormatsCell(xlsx, 15, 60, "[Red][<=100];[Green][>100]");
|
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");
|
xlsx.saveAs("Book1.xlsx");
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
@@ -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
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -1,15 +1,24 @@
|
|||||||
#include "xlsxdocument.h"
|
#include "xlsxdocument.h"
|
||||||
|
#include "xlsxformat.h"
|
||||||
|
|
||||||
|
QTXLSX_USE_NAMESPACE
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
QXlsx::Document xlsx;
|
Document xlsx;
|
||||||
|
//![0]
|
||||||
xlsx.write("B1", "Merge Cells");
|
Format *format = xlsx.createFormat();
|
||||||
xlsx.mergeCells("B1:B5");
|
format->setHorizontalAlignment(Format::AlignHCenter);
|
||||||
|
format->setVerticalAlignment(Format::AlignVCenter);
|
||||||
xlsx.write("E2", "Merge Cells 2");
|
//![0]
|
||||||
xlsx.mergeCells("E2:G4");
|
//![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();
|
xlsx.save();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -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)
|
int Document::unmergeCells(const QString &range)
|
||||||
{
|
{
|
||||||
return currentWorksheet()->unmergeCells(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.
|
* \brief Set properties for a row of cells.
|
||||||
* \param row The worksheet row (zero indexed).
|
* \param row The worksheet row (zero indexed).
|
||||||
|
|||||||
@@ -58,7 +58,9 @@ public:
|
|||||||
int write(const QString &cell, const QVariant &value, Format *format=0);
|
int write(const QString &cell, const QVariant &value, Format *format=0);
|
||||||
int write(int row, int col, 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 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);
|
int unmergeCells(const QString &range);
|
||||||
bool setRow(int row, double height, Format* format=0, bool hidden=false);
|
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);
|
bool setRow(const QString &row, double height, Format* format=0, bool hidden=false);
|
||||||
|
|||||||
+42
-13
@@ -679,7 +679,13 @@ int Worksheet::insertImage(int row, int column, const QImage &image, const QPoin
|
|||||||
return 0;
|
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);
|
Q_D(Worksheet);
|
||||||
if (range.rowCount() < 2 && range.columnCount() < 2)
|
if (range.rowCount() < 2 && range.columnCount() < 2)
|
||||||
@@ -688,11 +694,37 @@ int Worksheet::mergeCells(const CellRange &range)
|
|||||||
if (d->checkDimensions(range.firstRow(), range.firstColumn()))
|
if (d->checkDimensions(range.firstRow(), range.firstColumn()))
|
||||||
return -1;
|
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);
|
d->merges.append(range);
|
||||||
return 0;
|
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(':'));
|
QStringList cells = range.split(QLatin1Char(':'));
|
||||||
if (cells.size() != 2)
|
if (cells.size() != 2)
|
||||||
@@ -703,14 +735,12 @@ int Worksheet::mergeCells(const QString &range)
|
|||||||
if (cell1 == QPoint(-1,-1) || cell2 == QPoint(-1, -1))
|
if (cell1 == QPoint(-1,-1) || cell2 == QPoint(-1, -1))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return mergeCells(CellRange(cell1.x(), cell1.y(), cell2.x(), cell2.y()));
|
return mergeCells(CellRange(cell1.x(), cell1.y(), cell2.x(), cell2.y()), format);
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Unmerge the cells in the \a range.
|
||||||
|
*/
|
||||||
int Worksheet::unmergeCells(const CellRange &range)
|
int Worksheet::unmergeCells(const CellRange &range)
|
||||||
{
|
{
|
||||||
Q_D(Worksheet);
|
Q_D(Worksheet);
|
||||||
@@ -721,6 +751,10 @@ int Worksheet::unmergeCells(const CellRange &range)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\overload
|
||||||
|
Unmerge the cells in the \a range.
|
||||||
|
*/
|
||||||
int Worksheet::unmergeCells(const QString &range)
|
int Worksheet::unmergeCells(const QString &range)
|
||||||
{
|
{
|
||||||
QStringList cells = range.split(QLatin1Char(':'));
|
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()));
|
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)
|
void Worksheet::saveToXmlFile(QIODevice *device)
|
||||||
{
|
{
|
||||||
Q_D(Worksheet);
|
Q_D(Worksheet);
|
||||||
|
|||||||
@@ -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 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, Format *format=0);
|
||||||
int mergeCells(const QString &range);
|
int mergeCells(const CellRange &range, Format *format=0);
|
||||||
int mergeCells(const CellRange &range);
|
|
||||||
int unmergeCells(int row_begin, int column_begin, int row_end, int column_end);
|
|
||||||
int unmergeCells(const QString &range);
|
int unmergeCells(const QString &range);
|
||||||
int unmergeCells(const CellRange &range);
|
int unmergeCells(const CellRange &range);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user