xlnt - community edition
workbook.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024-2026 xlnt-community
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE
22 //
23 // @license: http://www.opensource.org/licenses/mit-license.php
24 // @author: see AUTHORS file
25 
26 #pragma once
27 
28 #include <cstdint>
29 #include <functional>
30 #include <iterator>
31 #include <memory>
32 #include <string>
33 #include <unordered_map>
34 #include <vector>
35 
36 #include <xlnt/xlnt_config.hpp>
37 #include <xlnt/internal/features.hpp>
38 
39 #if XLNT_HAS_INCLUDE(<string_view>) && XLNT_HAS_FEATURE(U8_STRING_VIEW)
40  #include <string_view>
41 #endif
42 
43 namespace xlnt {
44 
45 enum class calendar;
46 enum class core_property;
47 enum class extended_property;
48 enum class relationship_type;
49 
50 class alignment;
51 class border;
52 class calculation_properties;
53 class cell;
54 class cell_style;
55 class color;
56 class const_worksheet_iterator;
57 class fill;
58 class font;
59 class format;
60 class rich_text;
61 class manifest;
62 class metadata_property;
63 class named_range;
64 class number_format;
65 class path;
66 class pattern_fill;
67 class protection;
68 class range;
69 class range_reference;
70 class relationship;
71 class streaming_workbook_reader;
72 class style;
73 class style_serializer;
74 class theme;
75 class variant;
76 class workbook_view;
77 class worksheet;
78 class worksheet_iterator;
79 class zip_file;
80 
81 struct datetime;
82 
83 namespace detail {
84 
85 struct stylesheet;
86 struct workbook_impl;
87 struct worksheet_impl;
88 class xlsx_consumer;
89 class xlsx_producer;
90 
91 } // namespace detail
92 
96 class XLNT_API workbook
97 {
98 public:
102  enum class clone_method
103  {
104  deep_copy,
105  shallow_copy
106  };
107 
113 
119 
125  using reverse_iterator = std::reverse_iterator<iterator>;
126 
132  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
133 
138  static workbook empty();
139 
140  // Constructors
141 
146  workbook();
147 
151  workbook(const xlnt::path &file);
152 
156  workbook(const xlnt::path &file, const std::string &password);
157 
158 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
159  workbook(const xlnt::path &file, std::u8string_view password);
163 #endif
164 
168  workbook(std::istream &data);
169 
173  workbook(std::istream &data, const std::string &password);
174 
175 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
176  workbook(std::istream &data, std::u8string_view password);
180 #endif
181 
185  workbook(workbook &&other) = default;
186 
191  workbook(const workbook &other) = default;
192 
198  ~workbook() = default;
199 
204  workbook clone(clone_method method) const;
205 
206  // Worksheets
207 
211  worksheet create_sheet();
212 
216  worksheet create_sheet(std::size_t index);
217 
223  worksheet create_sheet_with_rel(const std::string &title, const relationship &rel);
224 
232  worksheet copy_sheet(worksheet worksheet);
233 
241  worksheet copy_sheet(worksheet worksheet, std::size_t index);
242 
247  worksheet active_sheet();
248 
253  void active_sheet(std::size_t index);
254 
260  worksheet sheet_by_title(const std::string &title);
261 
267  const worksheet sheet_by_title(const std::string &title) const;
268 
273  worksheet sheet_by_index(std::size_t index);
274 
279  const worksheet sheet_by_index(std::size_t index) const;
280 
285  bool has_sheet_id(std::size_t id) const;
286 
294  worksheet sheet_by_id(std::size_t id);
295 
303  const worksheet sheet_by_id(std::size_t id) const;
304 
310  bool sheet_hidden_by_index(std::size_t index) const;
311 
315  bool contains(const std::string &title) const;
316 
321  std::size_t index(worksheet worksheet) const;
322 
329  void move_sheet(worksheet worksheet, std::size_t newIndex);
330 
331  // remove worksheets
332 
337  void remove_sheet(worksheet worksheet);
338 
343  void clear();
344 
345  // iterators
346 
350  iterator begin();
351 
357  iterator end();
358 
362  const_iterator begin() const;
363 
369  const_iterator end() const;
370 
374  const_iterator cbegin() const;
375 
381  const_iterator cend() const;
382 
386  void apply_to_cells(std::function<void(cell)> f);
387 
392  std::vector<std::string> sheet_titles() const;
393 
397  std::size_t sheet_count() const;
398 
399  // Metadata Properties
400 
404  bool has_core_property(xlnt::core_property type) const;
405 
410  std::vector<xlnt::core_property> core_properties() const;
411 
418 
422  void core_property(xlnt::core_property type, const variant &value);
423 
427  bool has_extended_property(xlnt::extended_property type) const;
428 
433  std::vector<xlnt::extended_property> extended_properties() const;
434 
441 
445  void extended_property(xlnt::extended_property type, const variant &value);
446 
450  bool has_custom_property(const std::string &property_name) const;
451 
456  std::vector<std::string> custom_properties() const;
457 
463  variant custom_property(const std::string &property_name) const;
464 
468  void custom_property(const std::string &property_name, const variant &value);
469 
475  calendar base_date() const;
476 
481  void base_date(calendar base_date);
482 
486  bool has_title() const;
487 
493  std::string title() const;
494 
498  void title(const std::string &title);
499 
503  void abs_path(const std::string &path);
504 
508  void arch_id_flags(const std::size_t flags);
509 
510  // Named Ranges
511 
515  std::vector<xlnt::named_range> named_ranges() const;
516 
520  void create_named_range(const std::string &name, worksheet worksheet, const range_reference &reference);
521 
525  void create_named_range(const std::string &name, worksheet worksheet, const std::string &reference_string);
526 
530  bool has_named_range(const std::string &name) const;
531 
537  class range named_range(const std::string &name);
538 
544  void remove_named_range(const std::string &name);
545 
546  // Serialization/Deserialization
547 
552  void save(std::vector<std::uint8_t> &data) const;
553 
558  void save(std::vector<std::uint8_t> &data, const std::string &password) const;
559 
560 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
561  void save(std::vector<std::uint8_t> &data, std::u8string_view password) const;
566 #endif
567 
572  void save(const std::string &filename) const;
573 
578  void save(const std::string &filename, const std::string &password) const;
579 
580 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
581  void save(std::u8string_view filename) const;
586 
591  void save(std::u8string_view filename, std::u8string_view password) const;
592 #endif
593 
594 #ifdef _MSC_VER
595  void save(const std::wstring &filename) const;
600 
605  void save(const std::wstring &filename, const std::string &password) const;
606 #endif
607 
612  void save(const xlnt::path &filename) const;
613 
618  void save(const xlnt::path &filename, const std::string &password) const;
619 
620 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
621  void save(const xlnt::path &filename, std::u8string_view password) const;
626 #endif
627 
631  void save(std::ostream &stream) const;
632 
637  void save(std::ostream &stream, const std::string &password) const;
638 
639 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
640  void save(std::ostream &stream, std::u8string_view password) const;
645 #endif
646 
654  void load(const std::vector<std::uint8_t> &data);
655 
663  void load(const std::vector<std::uint8_t> &data, const std::string &password);
664 
665 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
666  void load(const std::vector<std::uint8_t> &data, std::u8string_view password);
674 #endif
675 
683  void load(const std::string &filename);
684 
692  void load(const std::string &filename, const std::string &password);
693 
694 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
695  void load(std::u8string_view filename);
703 
711  void load(std::u8string_view filename, std::u8string_view password);
712 #endif
713 
714 
715 #ifdef _MSC_VER
716  void load(const std::wstring &filename);
724 
732  void load(const std::wstring &filename, const std::string &password);
733 #endif
734 
742  void load(const xlnt::path &filename);
743 
751  void load(const xlnt::path &filename, const std::string &password);
752 
753 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
754  void load(const xlnt::path &filename, std::u8string_view password);
762 #endif
763 
771  void load(std::istream &stream);
772 
780  void load(std::istream &stream, const std::string &password);
781 
782 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
783  void load(std::istream &stream, std::u8string_view password);
791 #endif
792 
793  // View
794 
798  bool has_view() const;
799 
805  workbook_view view() const;
806 
810  void view(const workbook_view &view);
811 
812  // Properties
813 
817  bool has_code_name() const;
818 
824  std::string code_name() const;
825 
829  void code_name(const std::string &code_name);
830 
834  bool has_file_version() const;
835 
839  void clear_file_version();
840 
844  bool has_app_name() const;
845 
851  const std::string &app_name() const;
852 
856  void app_name(const std::string &app_name);
857 
861  bool has_last_edited() const;
862 
868  const std::string &last_edited_str() const;
869 
876  std::size_t last_edited() const;
877 
882  void last_edited(const std::string &last_edited);
883 
888  void last_edited(std::size_t last_edited);
889 
893  bool has_lowest_edited() const;
894 
900  const std::string &lowest_edited_str() const;
901 
908  std::size_t lowest_edited() const;
909 
914  void lowest_edited(const std::string &lowest_edited);
915 
920  void lowest_edited(std::size_t lowest_edited);
921 
925  bool has_rup_build() const;
926 
927  // <summary>
932  const std::string &rup_build_str() const;
933 
940  std::size_t rup_build() const;
941 
946  void rup_build(const std::string &rup_build);
947 
952  void rup_build(std::size_t rup_build);
953 
954  // Theme
955 
959  bool has_theme() const;
960 
966  const xlnt::theme &theme() const;
967 
971  void theme(const class theme &value);
972 
973  // Formats
974 
979  xlnt::format format(std::size_t format_index);
980 
985  const xlnt::format format(std::size_t format_index) const;
986 
990  xlnt::format create_format(bool default_format = false);
991 
996  void clear_formats();
997 
1001  std::size_t format_count() const;
1002 
1003  // Styles
1004 
1008  bool has_style(const std::string &name) const;
1009 
1015  class style style(const std::string &name);
1016 
1022  const class style style(const std::string &name) const;
1023 
1027  class style create_style(const std::string &name);
1028 
1034  class style create_builtin_style(std::size_t builtin_id);
1035 
1041  void clear_styles();
1042 
1046  void default_slicer_style(const std::string &value);
1047 
1051  std::string default_slicer_style() const;
1052 
1056  void enable_known_fonts();
1057 
1061  void disable_known_fonts();
1062 
1066  bool known_fonts_enabled() const;
1067 
1068  // Manifest
1069 
1073  class manifest &manifest();
1074 
1078  const class manifest &manifest() const;
1079 
1080  // shared strings
1081 
1088  std::size_t add_shared_string(const rich_text &shared, bool allow_duplicates = false);
1089 
1093  const rich_text &shared_strings(std::size_t index) const;
1094 
1099  std::vector<rich_text> &shared_strings();
1100 
1105  const std::vector<rich_text> &shared_strings() const;
1106 
1107  // Thumbnail
1108 
1113  void thumbnail(const std::vector<std::uint8_t> &thumbnail,
1114  const std::string &extension, const std::string &content_type);
1115 
1119  bool has_thumbnail() const;
1120 
1126  const std::vector<std::uint8_t> &thumbnail() const;
1127 
1131  const std::unordered_map<std::string, std::vector<std::uint8_t>>& binaries() const;
1132 
1133  // Calculation properties
1134 
1138  bool has_calculation_properties() const;
1139 
1143  void clear_calculation_properties();
1144 
1151 
1155  void calculation_properties(const class calculation_properties &props);
1156 
1162  bool compare(const workbook &other, bool compare_by_reference) const;
1163 
1164  // Operators
1165 
1170  workbook &operator=(const workbook &other) = default;
1171 
1176  workbook &operator=(workbook &&other) = default;
1177 
1183  worksheet operator[](const std::string &name);
1184 
1190  worksheet operator[](std::size_t index);
1191 
1196  bool operator==(const workbook &rhs) const;
1197 
1202  bool operator!=(const workbook &rhs) const;
1203 
1204 private:
1205  friend class cell;
1206  friend class streaming_workbook_reader;
1207  friend class worksheet;
1208  friend class detail::xlsx_consumer;
1209  friend class detail::xlsx_producer;
1210  friend struct detail::worksheet_impl;
1211 
1224  xlnt::format clone_format_from(const xlnt::format &source_format);
1225 
1230  bool owns_format(const class format &fmt) const;
1231 
1236  workbook(std::shared_ptr<detail::workbook_impl> impl);
1237 
1242  workbook(std::weak_ptr<detail::workbook_impl> impl);
1243 
1247  void set_impl(std::shared_ptr<detail::workbook_impl> impl);
1248 
1252  template <typename T>
1253  void construct(const xlnt::path &file, const T &password);
1254 
1258  template <typename T>
1259  void construct(std::istream &data, const T &password);
1260 
1265  template <typename T>
1266  void save_internal(std::vector<std::uint8_t> &data, const T &password) const;
1267 
1272  template <typename T>
1273  void save_internal(const T &filename) const;
1274 
1279  template <typename T>
1280  void save_internal(const T &filename, const T &password) const;
1281 
1286  template <typename T>
1287  void save_internal(const xlnt::path &filename, const T &password) const;
1288 
1293  template <typename T>
1294  void save_internal(std::ostream &stream, const T &password) const;
1295 
1303  template <typename T>
1304  void load_internal(const std::vector<std::uint8_t> &data, const T &password);
1305 
1313  template <typename T>
1314  void load_internal(const T &filename);
1315 
1323  template <typename T>
1324  void load_internal(const T &filename, const T &password);
1325 
1333  template <typename T>
1334  void load_internal(const xlnt::path &filename, const T &password);
1335 
1343  template <typename T>
1344  void load_internal(std::istream &stream, const T &password);
1345 
1350  detail::workbook_impl &impl();
1351 
1356  const detail::workbook_impl &impl() const;
1357 
1363  void register_package_part(relationship_type type);
1364 
1371  void register_workbook_part(relationship_type type);
1372 
1379  void register_worksheet_part(worksheet ws, relationship_type type);
1380 
1384  void garbage_collect_formulae();
1385 
1389  void update_sheet_properties();
1390 
1394  void swap(workbook &other);
1395 
1399  void reorder_relationships();
1400 
1404  void default_format(const xlnt::format& format);
1405 
1409  std::shared_ptr<detail::workbook_impl> d_;
1410 };
1411 
1412 } // namespace xlnt
core_property
Every core property in a workbook must be one of these types.
Definition: metadata_property.hpp:34
A range is a 2D collection of cells with defined extens that can be iterated upon.
Definition: range.hpp:53
Represents an association between a source Package or part, and a target object which can be a part o...
Definition: relationship.hpp:103
Represents an object that can have variable type.
Definition: variant.hpp:40
Definition: cell_reference.hpp:288
A workbook can be opened in multiple windows with different views. This class represents a particular...
Definition: workbook_view.hpp:37
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:36
extended_property
Every extended property in a workbook must be one of these types.
Definition: metadata_property.hpp:56
std::reverse_iterator< iterator > reverse_iterator
typedef for the iterator used for iterating through this workbook (non-const) in a range-based for lo...
Definition: workbook.hpp:125
A worksheet is a 2D array of cells starting with cell A1 in the top-left corner and extending indefin...
Definition: worksheet.hpp:75
A theme is a combination of fonts, colors, and effects. This isn&#39;t really supported yet...
Definition: theme.hpp:35
Describes a unit of data in a worksheet at a specific coordinate and its associated properties...
Definition: cell.hpp:83
Encapsulates zero or more formatted text runs where a text run is a string of text with the same defi...
Definition: rich_text.hpp:41
An iterator which is used to iterate over the worksheets in a workbook.
Definition: worksheet_iterator.hpp:44
Encapsulates a path that points to location in a filesystem.
Definition: path.hpp:43
relationship_type
All package relationships must be one of these defined types.
Definition: relationship.hpp:54
Workbook file properties relating to calculations.
Definition: calculation_properties.hpp:36
Describes the formatting of a particular cell.
Definition: format.hpp:58
An iterator which is used to iterate over the worksheets in a const workbook.
Definition: worksheet_iterator.hpp:154
A 2D range of cells in a worksheet that is referred to by name. ws->range("A1:B2") could be replaced ...
Definition: named_range.hpp:42
workbook is the container for all other parts of the document.
Definition: streaming_workbook_reader.hpp:55
The manifest keeps track of all files in the OOXML package and their type and relationships.
Definition: manifest.hpp:41
A range_reference describes a rectangular area of a worksheet with positive width and height defined ...
Definition: range_reference.hpp:36
Describes a style which has a name and can be applied to multiple individual formats. In Excel this is a "Cell Style".
Definition: style.hpp:55
std::reverse_iterator< const_iterator > const_reverse_iterator
typedef for the iterator used for iterating through this workbook (const) in a range-based for loop i...
Definition: workbook.hpp:132
workbook is the container for all other parts of the document.
Definition: workbook.hpp:96
calendar
An enumeration of possible base dates. Dates in Excel are stored as days since this base date...
Definition: calendar.hpp:35
clone_method
The method for cloning workbooks.
Definition: workbook.hpp:102