xlsx_producer.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #pragma once
  24. #include <cstdint>
  25. #include <iostream>
  26. #include <memory>
  27. #include <type_traits>
  28. #include <vector>
  29. #include <xlnt/utils/numeric.hpp>
  30. #include <detail/constants.hpp>
  31. #include <detail/external/include_libstudxml.hpp>
  32. namespace xml {
  33. class serializer;
  34. } // namespace xml
  35. namespace xlnt {
  36. class border;
  37. class cell;
  38. class cell_reference;
  39. class color;
  40. class fill;
  41. class font;
  42. class path;
  43. class relationship;
  44. class rich_text;
  45. class streaming_workbook_writer;
  46. class variant;
  47. class workbook;
  48. class worksheet;
  49. namespace detail {
  50. class ozstream;
  51. struct cell_impl;
  52. struct worksheet_impl;
  53. /// <summary>
  54. /// Handles writing a workbook into an XLSX file.
  55. /// </summary>
  56. class xlsx_producer
  57. {
  58. public:
  59. xlsx_producer(const workbook &target);
  60. ~xlsx_producer();
  61. void write(std::ostream &destination);
  62. void write(std::ostream &destination, const std::string &password);
  63. private:
  64. friend class xlnt::streaming_workbook_writer;
  65. void open(std::ostream &destination);
  66. cell add_cell(const cell_reference &ref);
  67. worksheet add_worksheet(const std::string &title);
  68. /// <summary>
  69. /// Write all files needed to create a valid XLSX file which represents all
  70. /// data contained in workbook.
  71. /// </summary>
  72. void populate_archive(bool streaming);
  73. void begin_part(const path &part);
  74. void end_part();
  75. // Package Parts
  76. void write_content_types();
  77. void write_property(const std::string &name, const variant &value, const std::string &ns, bool custom, std::size_t pid);
  78. void write_core_properties(const relationship &rel);
  79. void write_extended_properties(const relationship &rel);
  80. void write_custom_properties(const relationship &rel);
  81. void write_image(const path &image_path);
  82. void write_binary(const path &binary_path);
  83. // SpreadsheetML-Specific Package Parts
  84. void write_workbook(const relationship &rel);
  85. // Workbook Relationship Target Parts
  86. void write_connections(const relationship &rel);
  87. void write_custom_xml_mappings(const relationship &rel);
  88. void write_external_workbook_references(const relationship &rel);
  89. void write_pivot_table(const relationship &rel);
  90. void write_shared_string_table(const relationship &rel);
  91. void write_shared_workbook_revision_headers(const relationship &rel);
  92. void write_shared_workbook(const relationship &rel);
  93. void write_shared_workbook_user_data(const relationship &rel);
  94. void write_styles(const relationship &rel);
  95. void write_theme(const relationship &rel);
  96. void write_volatile_dependencies(const relationship &rel);
  97. void write_chartsheet(const relationship &rel);
  98. void write_dialogsheet(const relationship &rel);
  99. void write_worksheet(const relationship &rel);
  100. // Sheet Relationship Target Parts
  101. void write_comments(const relationship &rel, worksheet ws, const std::vector<cell_reference> &cells);
  102. void write_vml_drawings(const relationship &rel, worksheet ws, const std::vector<cell_reference> &cells);
  103. void write_drawings(const relationship &rel, worksheet ws);
  104. // Other Parts
  105. void write_custom_property();
  106. void write_unknown_parts();
  107. void write_unknown_relationships();
  108. // Helpers
  109. /// <summary>
  110. /// Some XLSX producers write booleans as "true" or "false" while others use "1" and "0".
  111. /// Both are valid, but we can use this method to write either depending on the producer
  112. /// we're trying to match.
  113. /// </summary>
  114. std::string write_bool(bool boolean) const;
  115. void write_relationships(const std::vector<xlnt::relationship> &relationships, const path &part);
  116. void write_color(const xlnt::color &color);
  117. void write_border(const xlnt::border &b);
  118. void write_fill(const xlnt::fill &f);
  119. void write_font(const xlnt::font &f);
  120. void write_table_styles();
  121. void write_colors(const std::vector<xlnt::color> &colors);
  122. void write_rich_text(const std::string &ns, const xlnt::rich_text &text);
  123. template<typename T>
  124. void write_element(const std::string &ns, const std::string &name, T value, bool preserve_whitespace = false)
  125. {
  126. write_start_element(ns, name);
  127. write_characters(value, preserve_whitespace);
  128. write_end_element(ns, name);
  129. }
  130. void write_start_element(const std::string &name);
  131. void write_start_element(const std::string &ns, const std::string &name);
  132. void write_end_element(const std::string &name);
  133. void write_end_element(const std::string &ns, const std::string &name);
  134. void write_namespace(const std::string &ns, const std::string &prefix);
  135. // std::string attribute name
  136. // not integer or float type
  137. template <typename T, typename = typename std::enable_if<!std::is_convertible<T, double>::value>::type>
  138. void write_attribute(const std::string &name, T value)
  139. {
  140. current_part_serializer_->attribute(name, value);
  141. }
  142. template <typename T, typename std::enable_if<std::is_floating_point<T>::value, T>::type* = nullptr>
  143. void write_attribute(const std::string &name, T value)
  144. {
  145. current_part_serializer_->attribute(name, converter_.serialise(value));
  146. }
  147. template <typename T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
  148. void write_attribute(const std::string &name, T value)
  149. {
  150. current_part_serializer_->attribute(name, std::to_string(value));
  151. }
  152. // qname attribute name
  153. // not integer or float type
  154. template <typename T, typename = typename std::enable_if<!std::is_convertible<T, double>::value>::type>
  155. void write_attribute(const xml::qname &name, T value)
  156. {
  157. current_part_serializer_->attribute(name, value);
  158. }
  159. template <typename T, typename std::enable_if<std::is_floating_point<T>::value, T>::type* = nullptr>
  160. void write_attribute(const xml::qname &name, T value)
  161. {
  162. current_part_serializer_->attribute(name, converter_.serialise(value));
  163. }
  164. template <typename T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
  165. void write_attribute(const xml::qname &name, T value)
  166. {
  167. current_part_serializer_->attribute(name, std::to_string(value));
  168. }
  169. template <typename T>
  170. void write_characters(T characters, bool preserve_whitespace = false)
  171. {
  172. if (preserve_whitespace)
  173. {
  174. write_attribute(xml::qname(constants::ns("xml"), "space"), "preserve");
  175. }
  176. current_part_serializer_->characters(characters);
  177. }
  178. /// <summary>
  179. /// A reference to the workbook which is the object of read/write operations.
  180. /// </summary>
  181. const workbook &source_;
  182. std::unique_ptr<ozstream> archive_;
  183. std::unique_ptr<xml::serializer> current_part_serializer_;
  184. std::unique_ptr<std::streambuf> current_part_streambuf_;
  185. std::ostream current_part_stream_;
  186. bool streaming_ = false;
  187. std::unique_ptr<detail::cell_impl> streaming_cell_;
  188. detail::cell_impl *current_cell_;
  189. detail::worksheet_impl *current_worksheet_;
  190. detail::number_serialiser converter_;
  191. };
  192. } // namespace detail
  193. } // namespace xlnt