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

New commits:
commit fb5e7b6ab1d62699d53c7ea3b3fff2c00f3a4b6e
Author:     Markus Mohrhard <[email protected]>
AuthorDate: Sun Jul 20 05:19:40 2025 +0800
Commit:     Miklos Vajna <[email protected]>
CommitDate: Fri Dec 5 16:39: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]>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/195000
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Miklos Vajna <[email protected]>

diff --git a/sc/source/filter/excel/xepivotxml.cxx 
b/sc/source/filter/excel/xepivotxml.cxx
index bbd70dd86557..1a9a1b31a47e 100644
--- a/sc/source/filter/excel/xepivotxml.cxx
+++ b/sc/source/filter/excel/xepivotxml.cxx
@@ -1034,29 +1034,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