OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLTables.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#ifndef OPENXLSX_XLTABLES_HPP
12#define OPENXLSX_XLTABLES_HPP
13
14// ===== External Includes ===== //
15#include <cstdint> // uint8_t, uint16_t, uint32_t
16#include <ostream> // std::basic_ostream
17#include <string>
18#include <string_view>
19#include <vector>
20
21// ===== OpenXLSX Includes ===== //
22#include "OpenXLSX-Exports.hpp"
23#include "XLAutoFilter.hpp"
24#include "XLException.hpp"
25#include "XLTableColumn.hpp"
26#include "XLXmlData.hpp"
27#include "XLXmlFile.hpp"
28
29namespace OpenXLSX
30{
31 class XLWorksheet;
32 class XLCellRange;
33
35 {
36 std::string name = ""; // Defaults to column name if empty
37 std::string caption = ""; // Defaults to column name if empty
38 uint32_t width = 144; // pixels
39 uint32_t height = 200; // pixels
40 int32_t offsetX = 0; // pixels
41 int32_t offsetY = 0; // pixels
42 std::string slicerStyle = ""; // Empty = use Excel default (no style attribute written)
43 std::vector<std::string> selectedItems = {};
44 };
45
46 class XLTable;
47
51 class OPENXLSX_EXPORT XLTableCollection
52 {
53 friend class XLWorksheet;
54
55 public:
59 XLTableCollection() = default;
60
66 explicit XLTableCollection(XMLNode node, XLWorksheet* worksheet);
67
71 size_t count() const;
72
76 XLTable operator[](size_t index) const;
77
81 XLTable table(std::string_view name) const;
82
89 XLTable add(std::string_view name, std::string_view range);
90
97 XLTable add(std::string_view name, const XLCellRange& range);
98
102 bool valid() const;
103
104 private:
105 XMLNode m_sheetNode;
106 XLWorksheet* m_worksheet{nullptr};
107 mutable std::vector<XLTable> m_tables;
108 mutable bool m_loaded{false};
109
110 void load() const;
111 };
112
116 class OPENXLSX_EXPORT XLTable : public XLXmlFile
117 {
118 friend class XLTableCollection;
119 friend class XLWorksheet;
120
121 public:
125 XLTable() : XLXmlFile(nullptr) {};
126
131 XLTable(XLXmlData* xmlData);
132
137 XLTable(const XLTable& other) = default;
138
142 XLTable(XLTable&& other) noexcept = default;
143
147 ~XLTable() = default;
148
152 XLTable& operator=(const XLTable&) = default;
153
157 XLTable& operator=(XLTable&& other) noexcept = default;
158
163 uint32_t id() const;
164
169 std::string name() const;
170
175 void setName(std::string_view name);
176
181 std::string displayName() const;
182
187 void setDisplayName(std::string_view name);
188
193 std::string rangeReference() const;
194
199 void setRangeReference(std::string_view ref);
200
205 std::string styleName() const;
206
211 void setStyleName(std::string_view styleName);
212
217 std::string comment() const;
218
223 void setComment(std::string_view comment);
224
229 uint32_t headerRowDxfId() const;
230
235 void setHeaderRowDxfId(uint32_t id);
236
241 uint32_t dataDxfId() const;
242
247 void setDataDxfId(uint32_t id);
248
253 bool published() const;
254
259 void setPublished(bool published);
260
265 bool showRowStripes() const;
266
271 void setShowRowStripes(bool show);
272
277 bool showHeaderRow() const;
278
283 void setShowHeaderRow(bool show);
284
289 bool showTotalsRow() const;
290
295 void setShowTotalsRow(bool show);
296
301 bool showColumnStripes() const;
302
307 void setShowColumnStripes(bool show);
308
313 bool showFirstColumn() const;
314
319 void setShowFirstColumn(bool show);
320
325 bool showLastColumn() const;
326
331 void setShowLastColumn(bool show);
332
338 XLTableColumn appendColumn(std::string_view name);
339
345 XLTableColumn column(std::string_view name) const;
346
352 XLTableColumn column(uint32_t id) const;
353
358 XLAutoFilter autoFilter() const;
359
364 void resizeToFitData(const XLWorksheet& worksheet);
365
370 void createColumnsFromRange(const XLWorksheet& worksheet);
371
375 void print(std::basic_ostream<char>& ostr) const;
376 };
377
378 // For compatibility
380} // namespace OpenXLSX
381
382#endif // OPENXLSX_XLTABLES_HPP
Definition XLAutoFilter.hpp:101
Represents a rectangular area of cells within a worksheet.
Definition XLCellRange.hpp:40
The XLTableCollection class manages multiple tables within a worksheet.
Definition XLTables.hpp:52
friend class XLWorksheet
Definition XLTables.hpp:53
size_t count() const
Get the number of tables in the collection.
Definition XLTables.cpp:60
XLTable table(std::string_view name) const
Get a table by its name.
Definition XLTables.cpp:70
bool valid() const
Check if the collection is valid.
Definition XLTables.cpp:164
XLTable operator[](size_t index) const
Get a table by its 0-based index.
Definition XLTables.cpp:65
XLTableCollection()=default
Constructor.
XLTable add(std::string_view name, std::string_view range)
Add a new table to the worksheet.
Definition XLTables.cpp:78
The XLTableColumn class encapsulates the <tableColumn> XML node. It allows setting different kinds of...
Definition XLTableColumn.hpp:31
The XLTable class represents a single Excel table (.xml file).
Definition XLTables.hpp:117
void setStyleName(std::string_view styleName)
Set the table style name.
Definition XLTables.cpp:249
std::string styleName() const
Get the table style name.
Definition XLTables.cpp:248
uint32_t headerRowDxfId() const
Get the differential formatting ID for the header row.
XLTableColumn appendColumn(std::string_view name)
Append a new column to the table.
Definition XLTables.cpp:305
void setShowLastColumn(bool show)
Set whether the last column is highlighted.
Definition XLTables.cpp:291
std::string displayName() const
Get the display name of the table.
Definition XLTables.cpp:217
void setShowHeaderRow(bool show)
Set whether the header row is shown.
Definition XLTables.cpp:324
void setName(std::string_view name)
Set the name of the table.
Definition XLTables.cpp:212
void setShowFirstColumn(bool show)
Set whether the first column is highlighted.
Definition XLTables.cpp:282
bool showFirstColumn() const
Check if the first column is highlighted.
Definition XLTables.cpp:280
friend class XLWorksheet
Definition XLTables.hpp:119
XLTable()
Constructor.
Definition XLTables.hpp:125
void setShowTotalsRow(bool show)
Set whether the totals row is shown.
Definition XLTables.cpp:331
void setHeaderRowDxfId(uint32_t id)
Set the differential formatting ID for the header row.
void setPublished(bool published)
Set whether the table is published.
Definition XLTables.cpp:260
uint32_t dataDxfId() const
Get the differential formatting ID for the data rows.
bool showTotalsRow() const
Check if the totals row is shown.
Definition XLTables.cpp:330
~XLTable()=default
The destructor.
void resizeToFitData(const XLWorksheet &worksheet)
Automatically resizes the table to fit the contiguous data in the worksheet.
Definition XLTables.cpp:379
XLTable(XLTable &&other) noexcept=default
Move constructor.
void setShowColumnStripes(bool show)
Set whether column stripes are shown.
Definition XLTables.cpp:273
XLTableColumn column(std::string_view name) const
Get a column by its name.
Definition XLTables.cpp:388
XLTable & operator=(const XLTable &)=default
Assignment operator.
XLTable & operator=(XLTable &&other) noexcept=default
Move assignment operator.
friend class XLTableCollection
Definition XLTables.hpp:118
bool showHeaderRow() const
Check if the header row is shown.
Definition XLTables.cpp:318
std::string name() const
Get the name of the table.
Definition XLTables.cpp:169
void setDataDxfId(uint32_t id)
Set the differential formatting ID for the data rows.
XLAutoFilter autoFilter() const
Get the auto filter object for the table.
Definition XLTables.cpp:298
bool showRowStripes() const
Check if row stripes are shown.
Definition XLTables.cpp:262
void setDisplayName(std::string_view name)
Set the display name of the table.
Definition XLTables.cpp:218
std::string rangeReference() const
Get the range reference of the table (e.g., "A1:C10").
Definition XLTables.cpp:224
void setShowRowStripes(bool show)
Set whether row stripes are shown.
Definition XLTables.cpp:264
uint32_t id() const
Get the name of the table.
Definition XLTables.cpp:167
bool showColumnStripes() const
Check if column stripes are shown.
Definition XLTables.cpp:271
bool published() const
Check if the table is published.
Definition XLTables.cpp:259
bool showLastColumn() const
Check if the last column is highlighted.
Definition XLTables.cpp:289
void createColumnsFromRange(const XLWorksheet &worksheet)
Automatically initializes the columns of the table from the header row of the specified worksheet ran...
Definition XLTables.cpp:358
void setComment(std::string_view comment)
Set the table comment.
Definition XLTables.cpp:257
void print(std::basic_ostream< char > &ostr) const
Print the XML contents of this XLTable instance using the underlying XMLNode print function.
Definition XLTables.cpp:404
std::string comment() const
Get the table comment.
Definition XLTables.cpp:256
void setRangeReference(std::string_view ref)
Set the range reference of the table.
Definition XLTables.cpp:225
XLTable(const XLTable &other)=default
The copy constructor.
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:132
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:79
XLXmlFile()=default
Default constructor.
Definition IXLCellProvider.hpp:24
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
XLTableCollection XLTables
Definition XLTables.hpp:379
Definition XLTables.hpp:35
std::string slicerStyle
Definition XLTables.hpp:42
uint32_t width
Definition XLTables.hpp:38
int32_t offsetX
Definition XLTables.hpp:40
std::vector< std::string > selectedItems
Definition XLTables.hpp:43
uint32_t height
Definition XLTables.hpp:39
std::string caption
Definition XLTables.hpp:37
int32_t offsetY
Definition XLTables.hpp:41
std::string name
Definition XLTables.hpp:36