streaming_workbook_writer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) 2017-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. #include <fstream>
  24. #include <xlnt/cell/cell.hpp>
  25. #include <xlnt/packaging/manifest.hpp>
  26. #include <xlnt/utils/optional.hpp>
  27. #include <xlnt/workbook/streaming_workbook_writer.hpp>
  28. #include <xlnt/workbook/workbook.hpp>
  29. #include <xlnt/worksheet/worksheet.hpp>
  30. #include <detail/implementations/cell_impl.hpp>
  31. #include <detail/implementations/worksheet_impl.hpp>
  32. #include <detail/serialization/open_stream.hpp>
  33. #include <detail/serialization/vector_streambuf.hpp>
  34. #include <detail/serialization/xlsx_producer.hpp>
  35. namespace xlnt {
  36. streaming_workbook_writer::streaming_workbook_writer()
  37. {
  38. }
  39. streaming_workbook_writer::~streaming_workbook_writer()
  40. {
  41. close();
  42. }
  43. void streaming_workbook_writer::close()
  44. {
  45. if (producer_)
  46. {
  47. producer_.reset(nullptr);
  48. stream_buffer_.reset(nullptr);
  49. }
  50. }
  51. cell streaming_workbook_writer::add_cell(const cell_reference &ref)
  52. {
  53. return producer_->add_cell(ref);
  54. }
  55. worksheet streaming_workbook_writer::add_worksheet(const std::string &title)
  56. {
  57. return producer_->add_worksheet(title);
  58. }
  59. void streaming_workbook_writer::open(std::vector<std::uint8_t> &data)
  60. {
  61. stream_buffer_.reset(new detail::vector_ostreambuf(data));
  62. stream_.reset(new std::ostream(stream_buffer_.get()));
  63. open(*stream_);
  64. }
  65. void streaming_workbook_writer::open(const std::string &filename)
  66. {
  67. stream_.reset(new std::ofstream());
  68. xlnt::detail::open_stream(static_cast<std::ofstream &>(*stream_), filename);
  69. open(*stream_);
  70. }
  71. #ifdef _MSC_VER
  72. void streaming_workbook_writer::open(const std::wstring &filename)
  73. {
  74. stream_.reset(new std::ofstream());
  75. xlnt::detail::open_stream(static_cast<std::ofstream &>(*stream_), filename);
  76. open(*stream_);
  77. }
  78. #endif
  79. void streaming_workbook_writer::open(const xlnt::path &filename)
  80. {
  81. stream_.reset(new std::ofstream());
  82. xlnt::detail::open_stream(static_cast<std::ofstream &>(*stream_), filename.string());
  83. open(*stream_);
  84. }
  85. void streaming_workbook_writer::open(std::ostream &stream)
  86. {
  87. workbook_.reset(new workbook());
  88. producer_.reset(new detail::xlsx_producer(*workbook_));
  89. producer_->open(stream);
  90. producer_->current_worksheet_ = new detail::worksheet_impl(workbook_.get(), 1, "Sheet1");
  91. producer_->current_cell_ = new detail::cell_impl();
  92. producer_->current_cell_->parent_ = producer_->current_worksheet_;
  93. }
  94. } // namespace xlnt