From ee9f43e21ddc0860da048adcb32887ac793da8b7 Mon Sep 17 00:00:00 2001 From: Debao Zhang Date: Fri, 1 Nov 2013 11:05:29 +0800 Subject: [PATCH] Use the orignal cell format if no format is specified --- src/xlsx/xlsxworksheet.cpp | 17 ++++++++++++++++- src/xlsx/xlsxworksheet_p.h | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index 3220f28..32b5064 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/src/xlsx/xlsxworksheet.cpp @@ -498,6 +498,15 @@ Cell *Worksheet::cellAt(int row, int column) const return d->cellTable[row][column].data(); } +Format *WorksheetPrivate::cellFormat(int row, int col) const +{ + if (!cellTable.contains(row)) + return 0; + if (!cellTable[row].contains(col)) + return 0; + return cellTable[row][col]->format(); +} + int Worksheet::writeString(int row, int column, const QString &value, Format *format) { Q_D(Worksheet); @@ -512,7 +521,7 @@ int Worksheet::writeString(int row, int column, const QString &value, Format *fo } d->sharedStrings()->addSharedString(content); - + format = format ? format : d->cellFormat(row, column); d->cellTable[row][column] = QSharedPointer(new Cell(content, Cell::String, format, this)); d->workbook->styles()->addFormat(format); return error; @@ -531,6 +540,7 @@ int Worksheet::writeInlineString(int row, int column, const QString &value, Form error = -2; } + format = format ? format : d->cellFormat(row, column); d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::InlineString, format, this)); d->workbook->styles()->addFormat(format); return error; @@ -542,6 +552,7 @@ int Worksheet::writeNumeric(int row, int column, double value, Format *format) if (d->checkDimensions(row, column)) return -1; + format = format ? format : d->cellFormat(row, column); d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::Numeric, format, this)); d->workbook->styles()->addFormat(format); return 0; @@ -559,6 +570,7 @@ int Worksheet::writeFormula(int row, int column, const QString &content, Format if (formula.startsWith(QLatin1String("="))) formula.remove(0,1); + format = format ? format : d->cellFormat(row, column); Cell *data = new Cell(result, Cell::Formula, format, this); data->d_ptr->formula = formula; d->cellTable[row][column] = QSharedPointer(data); @@ -573,6 +585,7 @@ int Worksheet::writeBlank(int row, int column, Format *format) if (d->checkDimensions(row, column)) return -1; + format = format ? format : d->cellFormat(row, column); d->cellTable[row][column] = QSharedPointer(new Cell(QVariant(), Cell::Blank, format, this)); d->workbook->styles()->addFormat(format); @@ -585,6 +598,7 @@ int Worksheet::writeBool(int row, int column, bool value, Format *format) if (d->checkDimensions(row, column)) return -1; + format = format ? format : d->cellFormat(row, column); d->cellTable[row][column] = QSharedPointer(new Cell(value, Cell::Boolean, format, this)); d->workbook->styles()->addFormat(format); @@ -652,6 +666,7 @@ int Worksheet::writeHyperlink(int row, int column, const QUrl &url, Format *form //Write the hyperlink string as normal string. d->sharedStrings()->addSharedString(displayString); + format = format ? format : d->cellFormat(row, column); d->cellTable[row][column] = QSharedPointer(new Cell(displayString, Cell::String, format, this)); d->workbook->styles()->addFormat(format); diff --git a/src/xlsx/xlsxworksheet_p.h b/src/xlsx/xlsxworksheet_p.h index edf8f46..4157e59 100644 --- a/src/xlsx/xlsxworksheet_p.h +++ b/src/xlsx/xlsxworksheet_p.h @@ -154,6 +154,7 @@ public: WorksheetPrivate(Worksheet *p); ~WorksheetPrivate(); int checkDimensions(int row, int col, bool ignore_row=false, bool ignore_col=false); + Format *cellFormat(int row, int col) const; QString generateDimensionString(); void calculateSpans(); void writeSheetData(XmlStreamWriter &writer);