OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLStreamReader.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11//
12// XLStreamReader — SAX streaming reader for large worksheets (phase C enhancements).
13//
14
15#ifndef OPENXLSX_XLSTREAMREADER_HPP
16#define OPENXLSX_XLSTREAMREADER_HPP
17
18#include "OpenXLSX-Exports.hpp"
19#include "IZipArchive.hpp"
20#include "XLCellValue.hpp"
21#include "XLRichText.hpp"
22#include "XLStyles_Common.hpp" // XLStyleIndex
23#include <cstdint>
24#include <filesystem>
25#include <functional>
26#include <optional>
27#include <string>
28#include <unordered_set>
29#include <vector>
30
31namespace OpenXLSX
32{
33
34 class XLWorksheet;
35
39 enum class XLStreamEmptyRowPolicy : uint8_t {
51 };
52
56 enum class XLStreamSharedStringMode : uint8_t {
60 Eager = 0,
66 };
67
71 struct OPENXLSX_EXPORT XLStreamReadOptions
72 {
78 bool applyNumberFormats{false};
85 size_t maxRowBytes{64 * 1024 * 1024};
90 size_t sstSpillThreshold{16 * 1024 * 1024};
92 std::filesystem::path tempDir{};
97 std::vector<uint16_t> columnFilter{};
102 size_t maxSstEntryBytes{256 * 1024 * 1024};
107 bool parseRichText{false};
111 size_t maxColumnarCells{50ull * 1000 * 1000};
112 };
113
117 struct OPENXLSX_EXPORT XLStreamCellView
118 {
120 std::optional<std::string> formula{};
121 std::optional<XLStyleIndex> styleIndex{};
122 uint16_t column{0}; // 1-based; 0 if unknown
123
124 // Formula metadata (OOXML &lt;f&gt; attributes)
125 std::optional<std::string> formulaType{}; // array | shared | dataTable
126 std::optional<std::string> formulaRef{};
127 std::optional<int> formulaSi{};
128 bool formulaCa{false};
129
130 // Rich text (when parseRichText and inlineStr has &lt;r&gt; runs)
131 std::optional<XLRichText> richText{};
132 };
133
139 class OPENXLSX_EXPORT XLStreamColumns
140 {
141 public:
142 XLStreamColumns() = default;
143 explicit XLStreamColumns(std::vector<std::vector<XLCellValue>> columns);
144
145 bool next();
147 uint16_t currentColumn() const { return m_index; }
148 const std::vector<XLCellValue>& values() const;
149 size_t columnCount() const { return m_columns.size(); }
150
151 private:
152 std::vector<std::vector<XLCellValue>> m_columns;
153 uint16_t m_index{0}; // 1-based after successful next()
154 bool m_started{false};
155 };
156
160 struct OPENXLSX_EXPORT XLStreamRowOptsView
161 {
162 std::optional<double> height{};
163 std::optional<bool> hidden{};
164 std::optional<uint8_t> outlineLevel{};
165 std::optional<XLStyleIndex> styleIndex{};
166 bool isSyntheticEmpty{false}; // true when emitted by EmitEmptyRows
167 };
168
169 class OPENXLSX_EXPORT XLStreamReader
170 {
171 public:
172 XLStreamReader() = default;
175
176 XLStreamReader(XLStreamReader&& other) noexcept;
177 XLStreamReader& operator=(XLStreamReader&& other) noexcept;
178
180
184 bool hasNext();
185
191 std::vector<XLCellValue> nextRow();
192
196 std::vector<XLStreamCellView> nextRowDetailed();
197
203 std::vector<std::string> nextRowStrings();
204
208 std::vector<XLStreamCellView> nextRowSparse();
209
214 bool nextRowInto(std::vector<XLCellValue>& out);
215
219 bool nextRowDetailedInto(std::vector<XLStreamCellView>& out);
220
225 std::vector<XLStreamCellView> nextRowProjected();
226
231 bool forEachCellInNextRow(const std::function<void(const XLStreamCellView&)>& fn);
232
238
242 std::string formattedValue(const XLStreamCellView& cell) const;
243
247 uint32_t currentRow() const;
248
253
257 const std::string& lastError() const { return m_lastError; }
258
262 void close();
263
264 const XLStreamReadOptions& options() const { return m_options; }
265
266 private:
267 friend class XLWorksheet;
268
269 explicit XLStreamReader(const XLWorksheet* worksheet, XLStreamReadOptions options = {});
270
271 void fetchMoreData();
272 void cleanup();
273 void compactBufferIfNeeded();
274
276 bool loadNextPhysicalRow();
277
279 bool consumeNextLogicalRow();
280
281 void parsePhysicalRowSlice(const char* p, const char* end);
282 XLCellValue parseCellValue(const std::string& cellType, std::string& cellValue) const;
283 std::string resolveFormatCode(XLStyleIndex styleIndex) const;
284 std::string valueToRawString(const XLCellValue& value) const;
285 void ensureIndexedSst() const;
286 std::string lookupIndexedSst(int32_t index) const;
287 bool columnAllowed(uint16_t col) const;
288 void rebuildColumnFilterSet() const;
289
290 static std::string builtinNumFmtCode(uint32_t numFmtId);
291
292 const XLWorksheet* m_worksheet{nullptr};
293 IZipArchive m_archive{}; // copy of archive handle for independent stream close
294 void* m_zipStream{nullptr};
295 XLStreamReadOptions m_options{};
296 mutable std::unordered_set<uint16_t> m_columnFilterSet{};
297 mutable bool m_columnFilterReady{false};
298
299 // Indexed SST (lazy): in-memory string or file-backed for large tables
300 mutable bool m_sstIndexReady{false};
301 mutable std::string m_sstXml; // small SST or empty when file-backed
302 mutable std::filesystem::path m_sstTempPath; // large SST spilled to disk
303 mutable bool m_sstFileBacked{false};
304 mutable std::vector<uint32_t> m_sstOffsets; // start offset of each <si> element
305
306 std::string m_buffer;
307 size_t m_bufPos{0};
308 bool m_eof{false};
309
310 uint32_t m_currentRow{0}; // last returned logical row
311 uint32_t m_lastPhysicalRow{0}; // last physical row number seen in XML
312 bool m_hasPendingPhysical{false};
313 bool m_physicalExhausted{false};
314
315 // Pending physical row (after loadNextPhysicalRow)
316 uint32_t m_pendingRowNum{0};
317 XLStreamRowOptsView m_pendingRowOpts{};
318 std::vector<XLStreamCellView> m_pendingCells{};
319
320 // Last returned logical row metadata
321 XLStreamRowOptsView m_currentRowOpts{};
322
323 // Reusable scratch buffers
324 std::string m_tagNameBuf;
325 std::string m_attrNameBuf;
326 std::string m_attrValueBuf;
327 std::string m_textContentBuf;
328 std::string m_lastError;
329
330 // Reusable outputs to cut per-row allocations when caller reuses patterns
331 std::vector<XLCellValue> m_reuseValues;
332 std::vector<XLStreamCellView> m_reuseDetailed;
333 std::vector<std::string> m_reuseStrings;
334 };
335
336} // namespace OpenXLSX
337#endif // OPENXLSX_XLSTREAMREADER_HPP
Class encapsulating a cell value.
Definition XLCellValue.hpp:89
Column-oriented cursor over a worksheet (excelize Cols-like).
Definition XLStreamReader.hpp:140
bool next()
Definition XLStreamReader.cpp:942
uint16_t currentColumn() const
Definition XLStreamReader.hpp:147
size_t columnCount() const
Definition XLStreamReader.hpp:149
std::vector< std::string > nextRowStrings()
Next row as display strings (optionally applying number formats).
Definition XLStreamReader.cpp:1088
bool forEachCellInNextRow(const std::function< void(const XLStreamCellView &)> &fn)
Consume the next logical row and invoke fn for each projected cell.
Definition XLStreamReader.cpp:928
XLStreamRowOptsView currentRowOpts() const
Row options for the last returned row.
Definition XLStreamReader.cpp:1104
XLStreamReader(const XLStreamReader &)=delete
std::vector< XLStreamCellView > nextRowDetailed()
Next row with formula, style index, and column metadata per cell.
Definition XLStreamReader.cpp:856
friend class XLWorksheet
Definition XLStreamReader.hpp:267
uint32_t currentRow() const
1-based index of the row last returned by nextRow / nextRowDetailed / nextRowStrings.
Definition XLStreamReader.cpp:1102
bool nextRowDetailedInto(std::vector< XLStreamCellView > &out)
Fill out with the next detailed row; returns false at end.
Definition XLStreamReader.cpp:883
bool hasNext()
Checks if there are more rows to read (physical and/or synthetic empty).
Definition XLStreamReader.cpp:277
const std::string & lastError() const
Last error message from a soft parse failure (empty if none).
Definition XLStreamReader.hpp:257
std::vector< XLStreamCellView > nextRowProjected()
Next row with only columns listed in options.columnFilter (or non-empty cells if filter empty).
Definition XLStreamReader.cpp:909
XLStreamColumns streamColumns()
Materialize all columns via one full stream pass (excelize Cols equivalent).
Definition XLStreamReader.cpp:962
bool nextRowInto(std::vector< XLCellValue > &out)
Fill out with the next dense row values; returns false at end.
Definition XLStreamReader.cpp:873
const XLStreamReadOptions & options() const
Definition XLStreamReader.hpp:264
XLStreamReader & operator=(const XLStreamReader &)=delete
std::vector< XLCellValue > nextRow()
Parses and returns the next row of cell values (backward-compatible API).
Definition XLStreamReader.cpp:850
std::vector< XLStreamCellView > nextRowSparse()
Next row containing only non-empty cells (no sparse padding).
Definition XLStreamReader.cpp:862
std::string formattedValue(const XLStreamCellView &cell) const
Format a single cell view using workbook styles when applyNumberFormats is enabled.
Definition XLStreamReader.cpp:1073
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:132
Definition IXLCellProvider.hpp:24
XLStreamEmptyRowPolicy
Policy for logical row gaps (missing <row> elements in sheetData).
Definition XLStreamReader.hpp:39
@ EmitEmptyRows
Synthesize empty rows for gaps in row numbers (excelize GetRows-like).
Definition XLStreamReader.hpp:50
@ SkipMissingRows
Only return rows that exist in the XML (legacy default).
Definition XLStreamReader.hpp:45
size_t XLStyleIndex
Definition XLStyles_Common.hpp:36
XLStreamSharedStringMode
How shared-string cells (t="s") are resolved during streaming reads.
Definition XLStreamReader.hpp:56
@ Eager
Use the workbook's in-memory shared string table (default, fastest for small SST).
Definition XLStreamReader.hpp:60
@ Indexed
Build a byte-offset index into sharedStrings.xml and resolve strings on demand.
Definition XLStreamReader.hpp:65
Rich view of a single streamed cell (value + formula + style).
Definition XLStreamReader.hpp:118
std::optional< int > formulaSi
Definition XLStreamReader.hpp:127
std::optional< std::string > formula
Definition XLStreamReader.hpp:120
XLCellValue value
Definition XLStreamReader.hpp:119
std::optional< XLStyleIndex > styleIndex
Definition XLStreamReader.hpp:121
std::optional< std::string > formulaType
Definition XLStreamReader.hpp:125
std::optional< XLRichText > richText
Definition XLStreamReader.hpp:131
uint16_t column
Definition XLStreamReader.hpp:122
bool formulaCa
Definition XLStreamReader.hpp:128
std::optional< std::string > formulaRef
Definition XLStreamReader.hpp:126
Options for XLStreamReader construction.
Definition XLStreamReader.hpp:72
bool parseRichText
When true, parse inlineStr <r> runs into XLRichText on XLStreamCellView.
Definition XLStreamReader.hpp:107
size_t maxRowBytes
Reject a single physical <row> larger than this many bytes (0 = unlimited).
Definition XLStreamReader.hpp:85
bool applyNumberFormats
When true, formattedValue() / nextRowStrings() apply number formats via XLNumberFormatter.
Definition XLStreamReader.hpp:78
std::vector< uint16_t > columnFilter
1-based column projection mask. Empty = all columns.
Definition XLStreamReader.hpp:97
XLStreamSharedStringMode sharedStringMode
Definition XLStreamReader.hpp:79
size_t maxSstEntryBytes
Reject sharedStrings (or other bulk loads via this reader) larger than this (0 = unlimited).
Definition XLStreamReader.hpp:102
size_t maxColumnarCells
Max cells buffered when materializing streamColumns() (0 = unlimited). Default 50M cells.
Definition XLStreamReader.hpp:111
std::filesystem::path tempDir
Definition XLStreamReader.hpp:92
size_t sstSpillThreshold
When Indexed SST exceeds this size, keep payload on a temp file and mmap-style seek.
Definition XLStreamReader.hpp:90
XLStreamEmptyRowPolicy emptyRows
Definition XLStreamReader.hpp:73
Row-level attributes for the last consumed physical/logical row (excelize GetRowOpts).
Definition XLStreamReader.hpp:161
std::optional< double > height
Definition XLStreamReader.hpp:162
std::optional< uint8_t > outlineLevel
Definition XLStreamReader.hpp:164
std::optional< XLStyleIndex > styleIndex
Definition XLStreamReader.hpp:165
std::optional< bool > hidden
Definition XLStreamReader.hpp:163
bool isSyntheticEmpty
Definition XLStreamReader.hpp:166