OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLStreamWriter.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLSTREAMWRITER_HPP
2#define OPENXLSX_XLSTREAMWRITER_HPP
3
4#include "OpenXLSX-Exports.hpp"
5#include "XLCellValue.hpp"
6#include "XLStyles.hpp"
7#include <cstddef>
8#include <filesystem>
9#include <fstream>
10#include <optional>
11#include <string>
12#include <vector>
13
14namespace OpenXLSX
15{
16
17 class XLWorksheet;
18
24 class OPENXLSX_EXPORT XLStreamCell
25 {
26 public:
31 explicit XLStreamCell(XLCellValue val) : value(std::move(val)), styleIndex(std::nullopt) {}
32
38 XLStreamCell(XLCellValue val, XLStyleIndex style) : value(std::move(val)), styleIndex(style) {}
39
41 std::optional<XLStyleIndex> styleIndex;
42 };
43
44 class OPENXLSX_EXPORT XLStreamWriter
45 {
46 public:
47 XLStreamWriter() = default;
50
51 XLStreamWriter(XLStreamWriter&& other) noexcept;
52 XLStreamWriter& operator=(XLStreamWriter&& other) noexcept;
53
55
56 bool isStreamActive() const;
57
62 void appendRow(const std::vector<XLCellValue>& values);
63
68 void appendRow(const std::vector<XLStreamCell>& cells);
69
70 std::string getTempFilePath() const;
71 void close();
72
73 private:
74 friend class XLWorksheet;
75
76 explicit XLStreamWriter(XLWorksheet* worksheet);
77
78 template<typename T>
79 void appendRowImpl(const std::vector<T>& items);
80
81 void flushWriteBuffer();
82 void flushSheetDataClose();
83
84 // Flush the in-memory write buffer to disk when it exceeds this threshold.
85 // 256 KB gives a good balance between memory use and syscall frequency.
86 static constexpr size_t kFlushThreshold = 256 * 1024;
87
88 std::filesystem::path m_tempPath;
89 std::ofstream m_stream;
90 uint32_t m_currentRow{1};
91 bool m_active{false};
92 std::string m_bottomHalf;
93
94 // Write buffer — avoids one syscall per cell by coalescing multiple
95 // small writes into a single fstream::write() call.
96 std::string m_writeBuffer;
97 };
98
99} // namespace OpenXLSX
100#endif // OPENXLSX_XLSTREAMWRITER_HPP
Class encapsulating a cell value.
Definition XLCellValue.hpp:79
A structure that represents a cell to be appended via the streaming API.
Definition XLStreamWriter.hpp:25
XLStreamCell(XLCellValue val, XLStyleIndex style)
Constructs a styled streaming cell with an explicitly assigned style index.
Definition XLStreamWriter.hpp:38
XLCellValue value
Definition XLStreamWriter.hpp:40
XLStreamCell(XLCellValue val)
Explicitly constructs a styled streaming cell from an XLCellValue.
Definition XLStreamWriter.hpp:31
std::optional< XLStyleIndex > styleIndex
Definition XLStreamWriter.hpp:41
Definition XLStreamWriter.hpp:45
XLStreamWriter(const XLStreamWriter &)=delete
XLStreamWriter & operator=(const XLStreamWriter &)=delete
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:118
Definition IZipArchive.hpp:18
size_t XLStyleIndex
Definition XLStyles.hpp:31
Definition XLCellIterator.hpp:121