xlnt - community edition
workbook.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024-2025 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 
221  worksheet create_sheet_with_rel(const std::string &title, const relationship &rel);
222 
227  worksheet copy_sheet(worksheet worksheet);
228 
233  worksheet copy_sheet(worksheet worksheet, std::size_t index);
234 
239  worksheet active_sheet();
240 
245  void active_sheet(std::size_t index);
246 
252  worksheet sheet_by_title(const std::string &title);
253 
259  const worksheet sheet_by_title(const std::string &title) const;
260 
265  worksheet sheet_by_index(std::size_t index);
266 
271  const worksheet sheet_by_index(std::size_t index) const;
272 
277  worksheet sheet_by_id(std::size_t id);
278 
283  const worksheet sheet_by_id(std::size_t id) const;
284 
290  bool sheet_hidden_by_index(std::size_t index) const;
291 
295  bool contains(const std::string &title) const;
296 
300  std::size_t index(worksheet worksheet) const;
301 
305  void move_sheet(worksheet worksheet, std::size_t newIndex);
306 
307  // remove worksheets
308 
312  void remove_sheet(worksheet worksheet);
313 
318  void clear();
319 
320  // iterators
321 
325  iterator begin();
326 
332  iterator end();
333 
337  const_iterator begin() const;
338 
344  const_iterator end() const;
345 
349  const_iterator cbegin() const;
350 
356  const_iterator cend() const;
357 
361  void apply_to_cells(std::function<void(cell)> f);
362 
367  std::vector<std::string> sheet_titles() const;
368 
372  std::size_t sheet_count() const;
373 
374  // Metadata Properties
375 
379  bool has_core_property(xlnt::core_property type) const;
380 
385  std::vector<xlnt::core_property> core_properties() const;
386 
391 
395  void core_property(xlnt::core_property type, const variant &value);
396 
400  bool has_extended_property(xlnt::extended_property type) const;
401 
406  std::vector<xlnt::extended_property> extended_properties() const;
407 
412 
416  void extended_property(xlnt::extended_property type, const variant &value);
417 
421  bool has_custom_property(const std::string &property_name) const;
422 
427  std::vector<std::string> custom_properties() const;
428 
432  variant custom_property(const std::string &property_name) const;
433 
437  void custom_property(const std::string &property_name, const variant &value);
438 
444  calendar base_date() const;
445 
450  void base_date(calendar base_date);
451 
455  bool has_title() const;
456 
460  std::string title() const;
461 
465  void title(const std::string &title);
466 
470  void abs_path(const std::string &path);
471 
475  void arch_id_flags(const std::size_t flags);
476 
477  // Named Ranges
478 
482  std::vector<xlnt::named_range> named_ranges() const;
483 
487  void create_named_range(const std::string &name, worksheet worksheet, const range_reference &reference);
488 
492  void create_named_range(const std::string &name, worksheet worksheet, const std::string &reference_string);
493 
497  bool has_named_range(const std::string &name) const;
498 
502  class range named_range(const std::string &name);
503 
507  void remove_named_range(const std::string &name);
508 
509  // Serialization/Deserialization
510 
515  void save(std::vector<std::uint8_t> &data) const;
516 
521  void save(std::vector<std::uint8_t> &data, const std::string &password) const;
522 
523 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
524  void save(std::vector<std::uint8_t> &data, std::u8string_view password) const;
529 #endif
530 
535  void save(const std::string &filename) const;
536 
541  void save(const std::string &filename, const std::string &password) const;
542 
543 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
544  void save(std::u8string_view filename) const;
549 
554  void save(std::u8string_view filename, std::u8string_view password) const;
555 #endif
556 
557 #ifdef _MSC_VER
558  void save(const std::wstring &filename) const;
563 
568  void save(const std::wstring &filename, const std::string &password) const;
569 #endif
570 
575  void save(const xlnt::path &filename) const;
576 
581  void save(const xlnt::path &filename, const std::string &password) const;
582 
583 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
584  void save(const xlnt::path &filename, std::u8string_view password) const;
589 #endif
590 
594  void save(std::ostream &stream) const;
595 
600  void save(std::ostream &stream, const std::string &password) const;
601 
602 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
603  void save(std::ostream &stream, std::u8string_view password) const;
608 #endif
609 
614  void load(const std::vector<std::uint8_t> &data);
615 
620  void load(const std::vector<std::uint8_t> &data, const std::string &password);
621 
622 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
623  void load(const std::vector<std::uint8_t> &data, std::u8string_view password);
628 #endif
629 
634  void load(const std::string &filename);
635 
640  void load(const std::string &filename, const std::string &password);
641 
642 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
643  void load(std::u8string_view filename);
648 
653  void load(std::u8string_view filename, std::u8string_view password);
654 #endif
655 
656 
657 #ifdef _MSC_VER
658  void load(const std::wstring &filename);
663 
668  void load(const std::wstring &filename, const std::string &password);
669 #endif
670 
675  void load(const xlnt::path &filename);
676 
681  void load(const xlnt::path &filename, const std::string &password);
682 
683 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
684  void load(const xlnt::path &filename, std::u8string_view password);
689 #endif
690 
695  void load(std::istream &stream);
696 
701  void load(std::istream &stream, const std::string &password);
702 
703 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
704  void load(std::istream &stream, std::u8string_view password);
709 #endif
710 
711  // View
712 
716  bool has_view() const;
717 
721  workbook_view view() const;
722 
726  void view(const workbook_view &view);
727 
728  // Properties
729 
733  bool has_code_name() const;
734 
738  std::string code_name() const;
739 
743  void code_name(const std::string &code_name);
744 
748  bool has_file_version() const;
749 
753  std::string app_name() const;
754 
758  std::size_t last_edited() const;
759 
763  std::size_t lowest_edited() const;
764 
768  std::size_t rup_build() const;
769 
770  // Theme
771 
775  bool has_theme() const;
776 
780  const xlnt::theme &theme() const;
781 
785  void theme(const class theme &value);
786 
787  // Formats
788 
793  xlnt::format format(std::size_t format_index);
794 
799  const xlnt::format format(std::size_t format_index) const;
800 
804  xlnt::format create_format(bool default_format = false);
805 
810  void clear_formats();
811 
812  // Styles
813 
817  bool has_style(const std::string &name) const;
818 
822  class style style(const std::string &name);
823 
827  const class style style(const std::string &name) const;
828 
832  class style create_style(const std::string &name);
833 
837  class style create_builtin_style(std::size_t builtin_id);
838 
844  void clear_styles();
845 
849  void default_slicer_style(const std::string &value);
850 
854  std::string default_slicer_style() const;
855 
859  void enable_known_fonts();
860 
864  void disable_known_fonts();
865 
869  bool known_fonts_enabled() const;
870 
871  // Manifest
872 
876  class manifest &manifest();
877 
881  const class manifest &manifest() const;
882 
883  // shared strings
884 
891  std::size_t add_shared_string(const rich_text &shared, bool allow_duplicates = false);
892 
896  const rich_text &shared_strings(std::size_t index) const;
897 
902  std::vector<rich_text> &shared_strings();
903 
908  const std::vector<rich_text> &shared_strings() const;
909 
910  // Thumbnail
911 
916  void thumbnail(const std::vector<std::uint8_t> &thumbnail,
917  const std::string &extension, const std::string &content_type);
918 
922  const std::vector<std::uint8_t> &thumbnail() const;
923 
927  const std::unordered_map<std::string, std::vector<std::uint8_t>>& binaries() const;
928 
929  // Calculation properties
930 
934  bool has_calculation_properties() const;
935 
940 
944  void calculation_properties(const class calculation_properties &props);
945 
951  bool compare(const workbook &other, bool compare_by_reference) const;
952 
953  // Operators
954 
959  workbook &operator=(const workbook &other) = default;
960 
965  workbook &operator=(workbook &&other) = default;
966 
970  worksheet operator[](const std::string &name);
971 
975  worksheet operator[](std::size_t index);
976 
981  bool operator==(const workbook &rhs) const;
982 
987  bool operator!=(const workbook &rhs) const;
988 
989 private:
990  friend class streaming_workbook_reader;
991  friend class worksheet;
992  friend class detail::xlsx_consumer;
993  friend class detail::xlsx_producer;
994  friend struct detail::worksheet_impl;
995 
1000  workbook(std::shared_ptr<detail::workbook_impl> impl);
1001 
1006  workbook(std::weak_ptr<detail::workbook_impl> impl);
1007 
1011  void set_impl(std::shared_ptr<detail::workbook_impl> impl);
1012 
1016  template <typename T>
1017  void construct(const xlnt::path &file, const T &password);
1018 
1022  template <typename T>
1023  void construct(std::istream &data, const T &password);
1024 
1029  template <typename T>
1030  void save_internal(std::vector<std::uint8_t> &data, const T &password) const;
1031 
1036  template <typename T>
1037  void save_internal(const T &filename) const;
1038 
1043  template <typename T>
1044  void save_internal(const T &filename, const T &password) const;
1045 
1050  template <typename T>
1051  void save_internal(const xlnt::path &filename, const T &password) const;
1052 
1057  template <typename T>
1058  void save_internal(std::ostream &stream, const T &password) const;
1059 
1064  template <typename T>
1065  void load_internal(const std::vector<std::uint8_t> &data, const T &password);
1066 
1071  template <typename T>
1072  void load_internal(const T &filename);
1073 
1078  template <typename T>
1079  void load_internal(const T &filename, const T &password);
1080 
1085  template <typename T>
1086  void load_internal(const xlnt::path &filename, const T &password);
1087 
1092  template <typename T>
1093  void load_internal(std::istream &stream, const T &password);
1094 
1099  detail::workbook_impl &impl();
1100 
1105  const detail::workbook_impl &impl() const;
1106 
1112  void register_package_part(relationship_type type);
1113 
1120  void register_workbook_part(relationship_type type);
1121 
1128  void register_worksheet_part(worksheet ws, relationship_type type);
1129 
1133  void garbage_collect_formulae();
1134 
1138  void update_sheet_properties();
1139 
1143  void swap(workbook &other);
1144 
1148  void reorder_relationships();
1149 
1153  std::shared_ptr<detail::workbook_impl> d_;
1154 };
1155 
1156 } // 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:261
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:57
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