sc/source/filter/excel/xepivotxml.cxx |   42 ++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 19 deletions(-)

New commits:
commit a62e6c819192d77251a9643dc229b82fcdcc6c91
Author:     Markus Mohrhard <[email protected]>
AuthorDate: Sun Jul 20 05:19:40 2025 +0800
Commit:     Xisco Fauli <[email protected]>
CommitDate: Thu Nov 27 16:34:24 2025 +0100

    tdf#158921: fix invalid OOXML during pivot table export
    
    Change-Id: Ie3f0fb7302c21d56fedff8db3f2b1c2b7a5805eb
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/188079
    Tested-by: Jenkins
    Reviewed-by: Markus Mohrhard <[email protected]>
    (cherry picked from commit b5837d2667c2be2c485753f9cf75e84032a15f74)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/194211
    Reviewed-by: Xisco Fauli <[email protected]>
    Code-Style: Xisco Fauli <[email protected]>

diff --git a/sc/source/filter/excel/xepivotxml.cxx 
b/sc/source/filter/excel/xepivotxml.cxx
index bbb9957ee1e9..99cf0024043e 100644
--- a/sc/source/filter/excel/xepivotxml.cxx
+++ b/sc/source/filter/excel/xepivotxml.cxx
@@ -1066,29 +1066,33 @@ void XclExpXmlPivotTables::SavePivotTableXml( 
XclExpXmlStream& rStrm, const ScDP
             pAttList->add( XML_outline, ToPsz10(false));
         pPivotStrm->startElement(XML_pivotField, pAttList);
 
-        pPivotStrm->startElement(XML_items,
-            XML_count, 
OString::number(static_cast<tools::Long>(aMemberSequence.size() + 
aSubtotalSequence.size())));
-
-        for (const auto & nMember : aMemberSequence)
+        sal_uInt32 nItems = aMemberSequence.size() + aSubtotalSequence.size();
+        if (nItems > 0)
         {
-            auto pItemAttList = 
sax_fastparser::FastSerializerHelper::createAttrList();
-            if (nMember.second)
-                pItemAttList->add(XML_h, ToPsz10(true));
-            pItemAttList->add(XML_x, 
OString::number(static_cast<tools::Long>(nMember.first)));
-            pPivotStrm->singleElement(XML_item, pItemAttList);
-        }
+            pPivotStrm->startElement(XML_items,
+                XML_count, OString::number(nItems));
 
-        if (strcmp(toOOXMLAxisType(eOrient), "axisCol") == 0)
-            nColItemsCount = pivotTableColCount - nFirstDataCol;
-        else if (strcmp(toOOXMLAxisType(eOrient), "axisRow") == 0)
-            nRowItemsCount = pivotTableRowCount - nFirstDataRow;
+            for (const auto & nMember : aMemberSequence)
+            {
+                auto pItemAttList = 
sax_fastparser::FastSerializerHelper::createAttrList();
+                if (nMember.second)
+                    pItemAttList->add(XML_h, ToPsz10(true));
+                pItemAttList->add(XML_x, 
OString::number(static_cast<tools::Long>(nMember.first)));
+                pPivotStrm->singleElement(XML_item, pItemAttList);
+            }
 
-        for (const OString& sSubtotal : aSubtotalSequence)
-        {
-            pPivotStrm->singleElement(XML_item, XML_t, sSubtotal);
-        }
+            if (strcmp(toOOXMLAxisType(eOrient), "axisCol") == 0)
+                nColItemsCount = pivotTableColCount - nFirstDataCol;
+            else if (strcmp(toOOXMLAxisType(eOrient), "axisRow") == 0)
+                nRowItemsCount = pivotTableRowCount - nFirstDataRow;
 
-        pPivotStrm->endElement(XML_items);
+            for (const OString& sSubtotal : aSubtotalSequence)
+            {
+                pPivotStrm->singleElement(XML_item, XML_t, sSubtotal);
+            }
+
+            pPivotStrm->endElement(XML_items);
+        }
         pPivotStrm->endElement(XML_pivotField);
     }
 

Reply via email to