xlnt
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 class xlsx_consumer;
88 class xlsx_producer;
89 
90 } // namespace detail
91 
95 class XLNT_API workbook
96 {
97 public:
103 
109 
115  using reverse_iterator = std::reverse_iterator<iterator>;
116 
122  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
123 
128  static workbook empty();
129 
130  // Constructors
131 
136  workbook();
137 
141  workbook(const xlnt::path &file);
142 
146  workbook(const xlnt::path &file, const std::string &password);
147 
148 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
149  workbook(const xlnt::path &file, std::u8string_view password);
153 #endif
154 
158  workbook(std::istream &data);
159 
163  workbook(std::istream &data, const std::string &password);
164 
165 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
166  workbook(std::istream &data, std::u8string_view password);
170 #endif
171 
175  workbook(workbook &&other);
176 
180  workbook(const workbook &other);
181 
187  ~workbook();
188 
189  // Worksheets
190 
194  worksheet create_sheet();
195 
199  worksheet create_sheet(std::size_t index);
200 
204  worksheet create_sheet_with_rel(const std::string &title, const relationship &rel);
205 
210  worksheet copy_sheet(worksheet worksheet);
211 
216  worksheet copy_sheet(worksheet worksheet, std::size_t index);
217 
222  worksheet active_sheet();
223 
228  void active_sheet(std::size_t index);
229 
235  worksheet sheet_by_title(const std::string &title);
236 
242  const worksheet sheet_by_title(const std::string &title) const;
243 
248  worksheet sheet_by_index(std::size_t index);
249 
254  const worksheet sheet_by_index(std::size_t index) const;
255 
260  worksheet sheet_by_id(std::size_t id);
261 
266  const worksheet sheet_by_id(std::size_t id) const;
267 
273  bool sheet_hidden_by_index(std::size_t index) const;
274 
278  bool contains(const std::string &title) const;
279 
283  std::size_t index(worksheet worksheet);
284 
285  // remove worksheets
286 
290  void remove_sheet(worksheet worksheet);
291 
296  void clear();
297 
298  // iterators
299 
303  iterator begin();
304 
310  iterator end();
311 
315  const_iterator begin() const;
316 
322  const_iterator end() const;
323 
327  const_iterator cbegin() const;
328 
334  const_iterator cend() const;
335 
339  void apply_to_cells(std::function<void(cell)> f);
340 
345  std::vector<std::string> sheet_titles() const;
346 
350  std::size_t sheet_count() const;
351 
352  // Metadata Properties
353 
357  bool has_core_property(xlnt::core_property type) const;
358 
363  std::vector<xlnt::core_property> core_properties() const;
364 
369 
373  void core_property(xlnt::core_property type, const variant &value);
374 
378  bool has_extended_property(xlnt::extended_property type) const;
379 
384  std::vector<xlnt::extended_property> extended_properties() const;
385 
390 
394  void extended_property(xlnt::extended_property type, const variant &value);
395 
399  bool has_custom_property(const std::string &property_name) const;
400 
405  std::vector<std::string> custom_properties() const;
406 
410  variant custom_property(const std::string &property_name) const;
411 
415  void custom_property(const std::string &property_name, const variant &value);
416 
422  calendar base_date() const;
423 
428  void base_date(calendar base_date);
429 
433  bool has_title() const;
434 
438  std::string title() const;
439 
443  void title(const std::string &title);
444 
448  void abs_path(const std::string &path);
449 
453  void arch_id_flags(const std::size_t flags);
454 
455  // Named Ranges
456 
460  std::vector<xlnt::named_range> named_ranges() const;
461 
465  void create_named_range(const std::string &name, worksheet worksheet, const range_reference &reference);
466 
470  void create_named_range(const std::string &name, worksheet worksheet, const std::string &reference_string);
471 
475  bool has_named_range(const std::string &name) const;
476 
480  class range named_range(const std::string &name);
481 
485  void remove_named_range(const std::string &name);
486 
487  // Serialization/Deserialization
488 
493  void save(std::vector<std::uint8_t> &data) const;
494 
499  void save(std::vector<std::uint8_t> &data, const std::string &password) const;
500 
501 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
502  void save(std::vector<std::uint8_t> &data, std::u8string_view password) const;
507 #endif
508 
513  void save(const std::string &filename) const;
514 
519  void save(const std::string &filename, const std::string &password) const;
520 
521 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
522  void save(std::u8string_view filename) const;
527 
532  void save(std::u8string_view filename, std::u8string_view password) const;
533 #endif
534 
535 #ifdef _MSC_VER
536  void save(const std::wstring &filename) const;
541 
546  void save(const std::wstring &filename, const std::string &password) const;
547 #endif
548 
553  void save(const xlnt::path &filename) const;
554 
559  void save(const xlnt::path &filename, const std::string &password) const;
560 
561 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
562  void save(const xlnt::path &filename, std::u8string_view password) const;
567 #endif
568 
572  void save(std::ostream &stream) const;
573 
578  void save(std::ostream &stream, const std::string &password) const;
579 
580 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
581  void save(std::ostream &stream, std::u8string_view password) const;
586 #endif
587 
592  void load(const std::vector<std::uint8_t> &data);
593 
598  void load(const std::vector<std::uint8_t> &data, const std::string &password);
599 
600 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
601  void load(const std::vector<std::uint8_t> &data, std::u8string_view password);
606 #endif
607 
612  void load(const std::string &filename);
613 
618  void load(const std::string &filename, const std::string &password);
619 
620 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
621  void load(std::u8string_view filename);
626 
631  void load(std::u8string_view filename, std::u8string_view password);
632 #endif
633 
634 
635 #ifdef _MSC_VER
636  void load(const std::wstring &filename);
641 
646  void load(const std::wstring &filename, const std::string &password);
647 #endif
648 
653  void load(const xlnt::path &filename);
654 
659  void load(const xlnt::path &filename, const std::string &password);
660 
661 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
662  void load(const xlnt::path &filename, std::u8string_view password);
667 #endif
668 
673  void load(std::istream &stream);
674 
679  void load(std::istream &stream, const std::string &password);
680 
681 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
682  void load(std::istream &stream, std::u8string_view password);
687 #endif
688 
689  // View
690 
694  bool has_view() const;
695 
699  workbook_view view() const;
700 
704  void view(const workbook_view &view);
705 
706  // Properties
707 
711  bool has_code_name() const;
712 
716  std::string code_name() const;
717 
721  void code_name(const std::string &code_name);
722 
726  bool has_file_version() const;
727 
731  std::string app_name() const;
732 
736  std::size_t last_edited() const;
737 
741  std::size_t lowest_edited() const;
742 
746  std::size_t rup_build() const;
747 
748  // Theme
749 
753  bool has_theme() const;
754 
758  const xlnt::theme &theme() const;
759 
763  void theme(const class theme &value);
764 
765  // Formats
766 
771  xlnt::format format(std::size_t format_index);
772 
777  const xlnt::format format(std::size_t format_index) const;
778 
782  xlnt::format create_format(bool default_format = false);
783 
788  void clear_formats();
789 
790  // Styles
791 
795  bool has_style(const std::string &name) const;
796 
800  class style style(const std::string &name);
801 
805  const class style style(const std::string &name) const;
806 
810  class style create_style(const std::string &name);
811 
815  class style create_builtin_style(std::size_t builtin_id);
816 
822  void clear_styles();
823 
827  void default_slicer_style(const std::string &value);
828 
832  std::string default_slicer_style() const;
833 
837  void enable_known_fonts();
838 
842  void disable_known_fonts();
843 
847  bool known_fonts_enabled() const;
848 
849  // Manifest
850 
854  class manifest &manifest();
855 
859  const class manifest &manifest() const;
860 
861  // shared strings
862 
869  std::size_t add_shared_string(const rich_text &shared, bool allow_duplicates = false);
870 
874  const rich_text &shared_strings(std::size_t index) const;
875 
880  std::vector<rich_text> &shared_strings();
881 
886  const std::vector<rich_text> &shared_strings() const;
887 
888  // Thumbnail
889 
894  void thumbnail(const std::vector<std::uint8_t> &thumbnail,
895  const std::string &extension, const std::string &content_type);
896 
900  const std::vector<std::uint8_t> &thumbnail() const;
901 
905  const std::unordered_map<std::string, std::vector<std::uint8_t>>& binaries() const;
906 
907  // Calculation properties
908 
912  bool has_calculation_properties() const;
913 
918 
922  void calculation_properties(const class calculation_properties &props);
923 
924  // Operators
925 
930  workbook &operator=(workbook other);
931 
935  worksheet operator[](const std::string &name);
936 
940  worksheet operator[](std::size_t index);
941 
946  bool operator==(const workbook &rhs) const;
947 
952  bool operator!=(const workbook &rhs) const;
953 
954 private:
955  friend class streaming_workbook_reader;
956  friend class worksheet;
957  friend class detail::xlsx_consumer;
958  friend class detail::xlsx_producer;
959 
964  workbook(detail::workbook_impl *impl);
965 
969  template <typename T>
970  void construct(const xlnt::path &file, const T &password);
971 
975  template <typename T>
976  void construct(std::istream &data, const T &password);
977 
982  template <typename T>
983  void save_internal(std::vector<std::uint8_t> &data, const T &password) const;
984 
989  template <typename T>
990  void save_internal(const T &filename) const;
991 
996  template <typename T>
997  void save_internal(const T &filename, const T &password) const;
998 
1003  template <typename T>
1004  void save_internal(const xlnt::path &filename, const T &password) const;
1005 
1010  template <typename T>
1011  void save_internal(std::ostream &stream, const T &password) const;
1012 
1017  template <typename T>
1018  void load_internal(const std::vector<std::uint8_t> &data, const T &password);
1019 
1024  template <typename T>
1025  void load_internal(const T &filename);
1026 
1031  template <typename T>
1032  void load_internal(const T &filename, const T &password);
1033 
1038  template <typename T>
1039  void load_internal(const xlnt::path &filename, const T &password);
1040 
1045  template <typename T>
1046  void load_internal(std::istream &stream, const T &password);
1047 
1052  detail::workbook_impl &impl();
1053 
1058  const detail::workbook_impl &impl() const;
1059 
1065  void register_package_part(relationship_type type);
1066 
1073  void register_workbook_part(relationship_type type);
1074 
1081  void register_worksheet_part(worksheet ws, relationship_type type);
1082 
1086  void garbage_collect_formulae();
1087 
1091  void update_sheet_properties();
1092 
1096  void swap(workbook &other);
1097 
1101  void reorder_relationships();
1102 
1106  std::unique_ptr<detail::workbook_impl> d_;
1107 };
1108 
1109 } // 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:55
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:262
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:37
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:115
A worksheet is a 2D array of cells starting with cell A1 in the top-left corner and extending indefin...
Definition: worksheet.hpp:77
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:84
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:34
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:56
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:122
workbook is the container for all other parts of the document.
Definition: workbook.hpp:95
calendar
An enumeration of possible base dates. Dates in Excel are stored as days since this base date...
Definition: calendar.hpp:35