From bc1d90b6d2ce49b73dfecd4c4ebc6f65d632350f Mon Sep 17 00:00:00 2001 From: Christoph Deil Date: Fri, 16 Oct 2026 13:39:42 +0200 Subject: [PATCH 1/2] fix: stream rows from a worksheet that prefixes its elements streamRows looked for a literal (the form the eager reader learned in 2.28.0) yielded no rows at all while excel['Sheet1'] read the same cells fine. The tag is now found with whatever prefix it carries; the cell walk already compared local names, and the part is still never parsed as a tree, so the memory profile of streaming is unchanged. Co-Authored-By: Claude Fable 5.1 --- lib/src/reader/sheet_stream.dart | 17 +++++++++++++---- test/sheet_stream_test.dart | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/src/reader/sheet_stream.dart b/lib/src/reader/sheet_stream.dart index e8d5408..7193d72 100644 --- a/lib/src/reader/sheet_stream.dart +++ b/lib/src/reader/sheet_stream.dart @@ -1,5 +1,10 @@ part of '../../excel_plus.dart'; +/// The opening `])'); + /// Memory-bounded reading of a worksheet, one row at a time. /// /// {@category Core} @@ -138,13 +143,17 @@ extension SheetStream on Excel { file.decompress(); final xmlStr = utf8.decode(file.content); - final start = xmlStr.indexOf('', start); + // The eager reader strips a part's element prefix by re-serialising the + // whole part, which would cost the memory this path exists to avoid; the + // tag is located with its prefix instead, and the cell walk below already + // compares local names. + final open = _sheetDataOpenTag.firstMatch(xmlStr); + if (open == null) return; + final openEnd = xmlStr.indexOf('>', open.start); if (openEnd == -1) return; // carries no rows at all. if (xmlStr[openEnd - 1] == '/') return; - final end = xmlStr.indexOf('', openEnd); + final end = xmlStr.indexOf('', openEnd); if (end == -1) return; yield* _streamSheetData(xmlStr.substring(openEnd + 1, end)); diff --git a/test/sheet_stream_test.dart b/test/sheet_stream_test.dart index cd68bab..97439ba 100644 --- a/test/sheet_stream_test.dart +++ b/test/sheet_stream_test.dart @@ -22,6 +22,37 @@ List> _streamedRows(Excel excel, String sheet) => excel.streamRows(sheet).toList(); void main() { + group('Streaming Namespace Parity', () { + test('reads a worksheet with every element namespace prefixed', () { + const ns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; + final bytes = buildXlsx( + '', + extraParts: { + 'xl/worksheets/sheet1.xml': + '' + '42' + '', + }, + ); + final excel = Excel.decodeBytes(bytes); + expect(excel.streamRows('Sheet1').toList(), [ + [const IntCellValue(42)], + ]); + }); + + test('yields no rows for a prefixed worksheet with empty sheetData', () { + const ns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; + final bytes = buildXlsx( + '', + extraParts: { + 'xl/worksheets/sheet1.xml': + '', + }, + ); + expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), isEmpty); + }); + }); + group('Streaming Row Read', () { test('agrees with the eager reader on a generated workbook', () { final excel = Excel.createExcel(); From 212004d2d815e50625c70efe11f166694433562d Mon Sep 17 00:00:00 2001 From: Christoph Deil Date: Fri, 16 Oct 2026 14:21:37 +0200 Subject: [PATCH 2/2] fix(reader): Locate streamed sheet data with XML events --- lib/src/reader/sheet_stream.dart | 41 ++++++++++++++------------------ test/sheet_stream_test.dart | 25 +++++++++++++++++++ 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/lib/src/reader/sheet_stream.dart b/lib/src/reader/sheet_stream.dart index 7193d72..5f35514 100644 --- a/lib/src/reader/sheet_stream.dart +++ b/lib/src/reader/sheet_stream.dart @@ -1,10 +1,5 @@ part of '../../excel_plus.dart'; -/// The opening `])'); - /// Memory-bounded reading of a worksheet, one row at a time. /// /// {@category Core} @@ -142,25 +137,12 @@ extension SheetStream on Excel { if (file == null) return; file.decompress(); - final xmlStr = utf8.decode(file.content); - // The eager reader strips a part's element prefix by re-serialising the - // whole part, which would cost the memory this path exists to avoid; the - // tag is located with its prefix instead, and the cell walk below already - // compares local names. - final open = _sheetDataOpenTag.firstMatch(xmlStr); - if (open == null) return; - final openEnd = xmlStr.indexOf('>', open.start); - if (openEnd == -1) return; - // carries no rows at all. - if (xmlStr[openEnd - 1] == '/') return; - final end = xmlStr.indexOf('', openEnd); - if (end == -1) return; - - yield* _streamSheetData(xmlStr.substring(openEnd + 1, end)); + yield* _streamWorksheet(utf8.decode(file.content)); } - /// SAX-walks the inner content of `` and yields one list per row. - Iterable> _streamSheetData(String inner) sync* { + /// Lazily walks XML events inside `sheetData`, without building a tree or + /// matching tag-like text in comments. Stops at the end of the cell data. + Iterable> _streamWorksheet(String xml) sync* { var row = []; var col = -1; String? cellRef; @@ -170,8 +152,21 @@ extension SheetStream on Excel { final valueBuf = StringBuffer(); StringBuffer? formulaBuf; var inRow = false; + var inSheetData = false; - for (final event in parseEvents('$inner')) { + for (final event in parseEvents(xml)) { + if (!inSheetData) { + if (event is XmlStartElementEvent && + _localName(event.name) == 'sheetData') { + if (event.isSelfClosing) return; + inSheetData = true; + } + continue; + } + if (event is XmlEndElementEvent && + _localName(event.name) == 'sheetData') { + return; + } if (event is XmlStartElementEvent) { switch (_localName(event.name)) { case 'row': diff --git a/test/sheet_stream_test.dart b/test/sheet_stream_test.dart index 97439ba..e502685 100644 --- a/test/sheet_stream_test.dart +++ b/test/sheet_stream_test.dart @@ -23,6 +23,31 @@ List> _streamedRows(Excel excel, String sheet) => void main() { group('Streaming Namespace Parity', () { + test('accepts whitespace in a prefixed closing tag', () { + final bytes = buildXlsx( + '', + extraParts: { + 'xl/worksheets/sheet1.xml': + '' + '42' + '', + }, + ); + expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), [ + [const IntCellValue(42)], + ]); + }); + + test('ignores a prefixed sheetData tag inside a comment', () { + final bytes = buildXlsx( + '42', + beforeSheetData: '', + ); + expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), [ + [const IntCellValue(42)], + ]); + }); + test('reads a worksheet with every element namespace prefixed', () { const ns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; final bytes = buildXlsx(