diff --git a/examples/xlsx/style/main.cpp b/examples/xlsx/style/main.cpp index 8c49253..7ce890a 100644 --- a/examples/xlsx/style/main.cpp +++ b/examples/xlsx/style/main.cpp @@ -46,6 +46,10 @@ int main() format5->setNumberFormat(22); xlsx.write("A5", QDate(2013, 8, 29), format5); + QXlsx::Format *format6 = xlsx.createFormat(); + format6->setPatternBackgroundColor(QColor(Qt::gray)); + xlsx.write("A6", "Background color: green", format6); + xlsx.saveAs(DATA_PATH"TestStyle.xlsx"); return 0; } diff --git a/src/xlsx/xlsxstyles.cpp b/src/xlsx/xlsxstyles.cpp index 0218d5b..0c57e88 100755 --- a/src/xlsx/xlsxstyles.cpp +++ b/src/xlsx/xlsxstyles.cpp @@ -311,12 +311,13 @@ void Styles::writeFill(XmlStreamWriter &writer, FillData *fill) writer.writeStartElement(QStringLiteral("fill")); writer.writeStartElement(QStringLiteral("patternFill")); writer.writeAttribute(QStringLiteral("patternType"), patternStrings[fill->pattern]); + // For a solid fill, Excel reverses the role of foreground and background colours if (fill->fgColor.isValid()) { - writer.writeEmptyElement(QStringLiteral("fgColor")); + writer.writeEmptyElement(fill->pattern == Format::PatternSolid ? QStringLiteral("bgColor") : QStringLiteral("fgColor")); writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill->fgColor.name().mid(1)); } if (fill->bgColor.isValid()) { - writer.writeEmptyElement(QStringLiteral("bgColor")); + writer.writeEmptyElement(fill->pattern == Format::PatternSolid ? QStringLiteral("fgColor") : QStringLiteral("bgColor")); writer.writeAttribute(QStringLiteral("rgb"), QStringLiteral("FF")+fill->bgColor.name().mid(1)); } diff --git a/tests/auto/styles/tst_stylestest.cpp b/tests/auto/styles/tst_stylestest.cpp index ce81891..c806753 100644 --- a/tests/auto/styles/tst_stylestest.cpp +++ b/tests/auto/styles/tst_stylestest.cpp @@ -13,6 +13,7 @@ public: private Q_SLOTS: void testEmptyStyle(); void testAddFormat(); + void testSolidFillBackgroundColor(); }; StylesTest::StylesTest() @@ -41,6 +42,19 @@ void StylesTest::testAddFormat() QVERIFY2(xmlData.contains(""), ""); //Note we have a default one } +// For a solid fill, Excel reverses the role of foreground and background colours +void StylesTest::testSolidFillBackgroundColor() +{ + QXlsx::Styles styles; + QXlsx::Format *format = styles.createFormat(); + format->setPatternBackgroundColor(QColor(Qt::red)); + styles.addFormat(format); + + QByteArray xmlData = styles.saveToXmlData(); + + QVERIFY(xmlData.contains("")); +} + QTEST_APPLESS_MAIN(StylesTest) #include "tst_stylestest.moc"