2014-05-22 05:48:51 +08:00
|
|
|
#include <algorithm>
|
2014-06-11 05:12:15 +08:00
|
|
|
#include <array>
|
2014-06-05 06:42:17 +08:00
|
|
|
#include <fstream>
|
2014-05-21 22:20:30 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <pugixml.hpp>
|
|
|
|
|
2014-06-11 06:36:31 +08:00
|
|
|
#ifdef _WIN32
|
2014-06-11 05:12:15 +08:00
|
|
|
#include <Windows.h>
|
2014-06-11 06:36:31 +08:00
|
|
|
#endif
|
2014-06-11 05:12:15 +08:00
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
#include "workbook/workbook.hpp"
|
|
|
|
#include "common/exceptions.hpp"
|
|
|
|
#include "drawing/drawing.hpp"
|
|
|
|
#include "worksheet/range.hpp"
|
|
|
|
#include "reader/reader.hpp"
|
|
|
|
#include "common/relationship.hpp"
|
|
|
|
#include "worksheet/worksheet.hpp"
|
|
|
|
#include "writer/writer.hpp"
|
|
|
|
#include "common/zip_file.hpp"
|
|
|
|
#include "detail/cell_impl.hpp"
|
|
|
|
#include "detail/workbook_impl.hpp"
|
|
|
|
#include "detail/worksheet_impl.hpp"
|
2014-05-21 22:20:30 +08:00
|
|
|
|
2014-06-11 05:12:15 +08:00
|
|
|
static std::string CreateTemporaryFilename()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
std::array<TCHAR, MAX_PATH> buffer;
|
|
|
|
DWORD result = GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data());
|
|
|
|
if(result > MAX_PATH)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("buffer is too small");
|
|
|
|
}
|
|
|
|
if(result == 0)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("GetTempPath failed");
|
|
|
|
}
|
|
|
|
std::string directory(buffer.begin(), buffer.begin() + result);
|
|
|
|
return directory + "xlnt.xlsx";
|
|
|
|
#else
|
|
|
|
return "/tmp/xlsx.xlnt";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
namespace xlnt {
|
2014-05-31 06:42:25 +08:00
|
|
|
namespace detail {
|
2014-06-16 01:06:47 +08:00
|
|
|
workbook_impl::workbook_impl() : active_sheet_index_(0), date_1904_(false)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
|
|
|
|
}
|
2014-05-31 06:42:25 +08:00
|
|
|
} // namespace detail
|
2014-05-30 08:52:14 +08:00
|
|
|
|
2014-06-16 01:06:47 +08:00
|
|
|
workbook::workbook() : d_(new detail::workbook_impl())
|
2014-05-30 08:52:14 +08:00
|
|
|
{
|
2014-06-16 01:06:47 +08:00
|
|
|
create_sheet("Sheet");
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
workbook::~workbook()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
workbook::iterator::iterator(workbook &wb, std::size_t index) : wb_(wb), index_(index)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-11 05:12:15 +08:00
|
|
|
workbook::iterator::iterator(const iterator &rhs) : wb_(rhs.wb_), index_(rhs.index_)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
worksheet workbook::iterator::operator*()
|
|
|
|
{
|
|
|
|
return wb_[index_];
|
|
|
|
}
|
|
|
|
|
|
|
|
workbook::iterator &workbook::iterator::operator++()
|
|
|
|
{
|
|
|
|
index_++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
workbook::iterator workbook::iterator::operator++(int)
|
|
|
|
{
|
|
|
|
iterator old(wb_, index_);
|
|
|
|
++*this;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool workbook::iterator::operator==(const iterator &comparand) const
|
|
|
|
{
|
|
|
|
return index_ == comparand.index_ && wb_ == comparand.wb_;
|
|
|
|
}
|
|
|
|
|
|
|
|
workbook::const_iterator::const_iterator(const workbook &wb, std::size_t index) : wb_(wb), index_(index)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-11 05:12:15 +08:00
|
|
|
workbook::const_iterator::const_iterator(const const_iterator &rhs) : wb_(rhs.wb_), index_(rhs.index_)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
const worksheet workbook::const_iterator::operator*()
|
|
|
|
{
|
|
|
|
return wb_.get_sheet_by_index(index_);
|
|
|
|
}
|
|
|
|
|
|
|
|
workbook::const_iterator &workbook::const_iterator::operator++()
|
|
|
|
{
|
|
|
|
index_++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
workbook::const_iterator workbook::const_iterator::operator++(int)
|
|
|
|
{
|
|
|
|
const_iterator old(wb_, index_);
|
|
|
|
++*this;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool workbook::const_iterator::operator==(const const_iterator &comparand) const
|
|
|
|
{
|
|
|
|
return index_ == comparand.index_ && wb_ == comparand.wb_;
|
|
|
|
}
|
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
worksheet workbook::get_sheet_by_name(const std::string &name)
|
|
|
|
{
|
2014-06-13 05:04:37 +08:00
|
|
|
for(auto &impl : d_->worksheets_)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-06-13 05:04:37 +08:00
|
|
|
if(impl.title_ == name)
|
|
|
|
{
|
|
|
|
return worksheet(&impl);
|
|
|
|
}
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2014-05-30 08:52:14 +08:00
|
|
|
|
|
|
|
return worksheet();
|
|
|
|
}
|
|
|
|
|
|
|
|
worksheet workbook::get_sheet_by_index(std::size_t index)
|
|
|
|
{
|
|
|
|
return worksheet(&d_->worksheets_[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const worksheet workbook::get_sheet_by_index(std::size_t index) const
|
|
|
|
{
|
|
|
|
return worksheet(&d_->worksheets_.at(index));
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
worksheet workbook::get_active_sheet()
|
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
return worksheet(&d_->worksheets_[d_->active_sheet_index_]);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
bool workbook::has_named_range(const std::string &name) const
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
for(auto worksheet : *this)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
if(worksheet.has_named_range(name))
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
worksheet workbook::create_sheet()
|
2014-06-16 01:06:47 +08:00
|
|
|
{
|
2014-05-21 22:20:30 +08:00
|
|
|
std::string title = "Sheet1";
|
|
|
|
int index = 1;
|
2014-05-30 08:52:14 +08:00
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
while(get_sheet_by_name(title) != nullptr)
|
|
|
|
{
|
|
|
|
title = "Sheet" + std::to_string(++index);
|
|
|
|
}
|
2014-06-10 12:29:49 +08:00
|
|
|
|
2014-06-14 03:05:24 +08:00
|
|
|
d_->worksheets_.push_back(detail::worksheet_impl(this, title));
|
|
|
|
return worksheet(&d_->worksheets_.back());
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void workbook::add_sheet(xlnt::worksheet worksheet)
|
|
|
|
{
|
|
|
|
for(auto ws : *this)
|
|
|
|
{
|
|
|
|
if(worksheet == ws)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("worksheet already in workbook");
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:52:14 +08:00
|
|
|
|
|
|
|
d_->worksheets_.emplace_back(*worksheet.d_);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void workbook::add_sheet(xlnt::worksheet worksheet, std::size_t index)
|
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
add_sheet(worksheet);
|
2014-05-31 06:42:25 +08:00
|
|
|
std::swap(d_->worksheets_[index], d_->worksheets_.back());
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int workbook::get_index(xlnt::worksheet worksheet)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for(auto ws : *this)
|
|
|
|
{
|
|
|
|
if(worksheet == ws)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
throw std::runtime_error("worksheet isn't owned by this workbook");
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
void workbook::create_named_range(const std::string &name, worksheet range_owner, const range_reference &reference)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
auto match = get_sheet_by_name(range_owner.get_title());
|
|
|
|
if(match != nullptr)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
match.create_named_range(name, reference);
|
|
|
|
return;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
throw std::runtime_error("worksheet isn't owned by this workbook");
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
void workbook::remove_named_range(const std::string &name)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
for(auto ws : *this)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
if(ws.has_named_range(name))
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
ws.remove_named_range(name);
|
2014-05-21 22:20:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
throw std::runtime_error("named range not found");
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
range workbook::get_named_range(const std::string &name)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
for(auto ws : *this)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
if(ws.has_named_range(name))
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
return ws.get_named_range(name);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-30 08:52:14 +08:00
|
|
|
|
|
|
|
throw std::runtime_error("named range not found");
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 05:04:37 +08:00
|
|
|
bool workbook::load(const std::istream &stream)
|
|
|
|
{
|
|
|
|
std::string temp_file = CreateTemporaryFilename();
|
|
|
|
|
|
|
|
std::ofstream tmp;
|
|
|
|
tmp.open(temp_file, std::ios::out | std::ios::binary);
|
|
|
|
tmp << stream.rdbuf();
|
|
|
|
tmp.close();
|
|
|
|
load(temp_file);
|
|
|
|
std::remove(temp_file.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-05 06:42:17 +08:00
|
|
|
bool workbook::load(const std::vector<unsigned char> &data)
|
|
|
|
{
|
2014-06-11 05:12:15 +08:00
|
|
|
std::string temp_file = CreateTemporaryFilename();
|
|
|
|
|
|
|
|
std::ofstream tmp;
|
|
|
|
tmp.open(temp_file, std::ios::out | std::ios::binary);
|
|
|
|
for(auto c : data)
|
2014-06-05 06:42:17 +08:00
|
|
|
{
|
2014-06-11 05:12:15 +08:00
|
|
|
tmp.put(c);
|
2014-06-05 06:42:17 +08:00
|
|
|
}
|
2014-06-11 05:12:15 +08:00
|
|
|
tmp.close();
|
|
|
|
load(temp_file);
|
|
|
|
std::remove(temp_file.c_str());
|
|
|
|
return true;
|
2014-06-05 06:42:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool workbook::load(const std::string &filename)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
|
|
|
zip_file f(filename, file_mode::open);
|
|
|
|
//auto core_properties = read_core_properties();
|
|
|
|
//auto app_properties = read_app_properties();
|
|
|
|
auto root_relationships = reader::read_relationships(f.get_file_contents("_rels/.rels"));
|
|
|
|
auto content_types = reader::read_content_types(f.get_file_contents("[Content_Types].xml"));
|
|
|
|
|
|
|
|
auto type = reader::determine_document_type(root_relationships, content_types.second);
|
|
|
|
|
|
|
|
if(type != "excel")
|
|
|
|
{
|
|
|
|
throw std::runtime_error("unsupported document type: " + filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto workbook_relationships = reader::read_relationships(f.get_file_contents("xl/_rels/workbook.xml.rels"));
|
2014-06-14 05:06:23 +08:00
|
|
|
|
|
|
|
for(auto relationship : workbook_relationships)
|
|
|
|
{
|
|
|
|
create_relationship(relationship.first, relationship.second.first, relationship.second.second);
|
|
|
|
}
|
2014-05-21 22:20:30 +08:00
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
doc.load(f.get_file_contents("xl/workbook.xml").c_str());
|
|
|
|
|
|
|
|
auto root_node = doc.child("workbook");
|
2014-06-13 05:04:37 +08:00
|
|
|
|
|
|
|
auto workbook_pr_node = root_node.child("workbookPr");
|
|
|
|
d_->date_1904_ = workbook_pr_node.attribute("date1904") != nullptr && workbook_pr_node.attribute("date1904").as_int() != 0;
|
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
auto sheets_node = root_node.child("sheets");
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
clear();
|
2014-05-21 22:20:30 +08:00
|
|
|
|
|
|
|
std::vector<std::string> shared_strings;
|
|
|
|
if(f.has_file("xl/sharedStrings.xml"))
|
|
|
|
{
|
|
|
|
shared_strings = xlnt::reader::read_shared_string(f.get_file_contents("xl/sharedStrings.xml"));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto sheet_node : sheets_node.children("sheet"))
|
|
|
|
{
|
2014-06-14 05:06:23 +08:00
|
|
|
std::string relation_id = sheet_node.attribute("r:id").as_string();
|
2014-05-21 22:20:30 +08:00
|
|
|
auto ws = create_sheet(sheet_node.attribute("name").as_string());
|
|
|
|
std::string sheet_filename("xl/");
|
2014-06-14 05:06:23 +08:00
|
|
|
sheet_filename += get_relationship(relation_id).get_target_uri();
|
2014-05-21 22:20:30 +08:00
|
|
|
xlnt::reader::read_worksheet(ws, f.get_file_contents(sheet_filename).c_str(), shared_strings);
|
|
|
|
}
|
2014-06-05 06:42:17 +08:00
|
|
|
|
|
|
|
return true;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2014-06-14 05:06:23 +08:00
|
|
|
|
|
|
|
void workbook::create_relationship(const std::string &id, const std::string &target, const std::string &type)
|
|
|
|
{
|
|
|
|
d_->relationships_.push_back(relationship(type, id, target));
|
|
|
|
}
|
|
|
|
|
|
|
|
relationship workbook::get_relationship(const std::string &id) const
|
|
|
|
{
|
|
|
|
for(auto &rel : d_->relationships_)
|
|
|
|
{
|
|
|
|
if(rel.get_id() == id)
|
|
|
|
{
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("");
|
|
|
|
}
|
2014-06-13 05:04:37 +08:00
|
|
|
|
|
|
|
int workbook::get_base_year() const
|
|
|
|
{
|
|
|
|
return d_->date_1904_ ? 1904 : 1900;
|
|
|
|
}
|
2014-05-21 22:20:30 +08:00
|
|
|
|
|
|
|
void workbook::remove_sheet(worksheet ws)
|
|
|
|
{
|
2014-05-31 06:42:25 +08:00
|
|
|
auto match_iter = std::find_if(d_->worksheets_.begin(), d_->worksheets_.end(), [=](detail::worksheet_impl &comp) { return worksheet(&comp) == ws; });
|
2014-05-30 08:52:14 +08:00
|
|
|
|
|
|
|
if(match_iter == d_->worksheets_.end())
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
|
|
|
throw std::runtime_error("worksheet not owned by this workbook");
|
|
|
|
}
|
2014-05-30 08:52:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
d_->worksheets_.erase(match_iter);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
worksheet workbook::create_sheet(std::size_t index)
|
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
create_sheet();
|
|
|
|
|
|
|
|
if(index != d_->worksheets_.size())
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
std::swap(d_->worksheets_[index], d_->worksheets_.back());
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2014-05-30 08:52:14 +08:00
|
|
|
|
|
|
|
return worksheet(&d_->worksheets_[index]);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
worksheet workbook::create_sheet(std::size_t index, const std::string &title)
|
|
|
|
{
|
|
|
|
auto ws = create_sheet(index);
|
|
|
|
ws.set_title(title);
|
2014-05-30 08:52:14 +08:00
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
|
|
|
worksheet workbook::create_sheet(const std::string &title)
|
|
|
|
{
|
|
|
|
if(title.length() > 31)
|
|
|
|
{
|
2014-06-06 04:19:31 +08:00
|
|
|
throw sheet_title_exception(title);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(std::find_if(title.begin(), title.end(),
|
|
|
|
[](char c) { return c == '*' || c == ':' || c == '/' || c == '\\' || c == '?' || c == '[' || c == ']'; }) != title.end())
|
|
|
|
{
|
2014-06-06 04:19:31 +08:00
|
|
|
throw sheet_title_exception(title);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 06:42:25 +08:00
|
|
|
if(std::find_if(d_->worksheets_.begin(), d_->worksheets_.end(), [&](detail::worksheet_impl &ws) { return worksheet(&ws).get_title() == title; }) != d_->worksheets_.end())
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
|
|
|
throw std::runtime_error("sheet exists");
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
auto ws = create_sheet();
|
|
|
|
ws.set_title(title);
|
|
|
|
|
|
|
|
return ws;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
workbook::iterator workbook::begin()
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
return iterator(*this, 0);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-05-30 08:52:14 +08:00
|
|
|
workbook::iterator workbook::end()
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
return iterator(*this, d_->worksheets_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
workbook::const_iterator workbook::cbegin() const
|
|
|
|
{
|
|
|
|
return const_iterator(*this, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
workbook::const_iterator workbook::cend() const
|
|
|
|
{
|
|
|
|
return const_iterator(*this, d_->worksheets_.size());
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> workbook::get_sheet_names() const
|
|
|
|
{
|
|
|
|
std::vector<std::string> names;
|
2014-05-30 08:52:14 +08:00
|
|
|
|
|
|
|
for(auto ws : *this)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
|
|
|
names.push_back(ws.get_title());
|
|
|
|
}
|
2014-05-30 08:52:14 +08:00
|
|
|
|
2014-05-21 22:20:30 +08:00
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
worksheet workbook::operator[](const std::string &name)
|
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
return get_sheet_by_name(name);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-11 05:12:15 +08:00
|
|
|
worksheet workbook::operator[](std::size_t index)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
return worksheet(&d_->worksheets_[index]);
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void workbook::clear()
|
|
|
|
{
|
2014-05-30 08:52:14 +08:00
|
|
|
d_->worksheets_.clear();
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 06:42:17 +08:00
|
|
|
bool workbook::save(std::vector<unsigned char> &data)
|
|
|
|
{
|
2014-06-11 05:12:15 +08:00
|
|
|
auto temp_file = CreateTemporaryFilename();
|
|
|
|
save(temp_file);
|
|
|
|
std::ifstream tmp;
|
|
|
|
tmp.open(temp_file, std::ios::in | std::ios::binary);
|
|
|
|
auto char_data = std::vector<char>((std::istreambuf_iterator<char>(tmp)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
data = std::vector<unsigned char>(char_data.begin(), char_data.end());
|
|
|
|
tmp.close();
|
|
|
|
std::remove(temp_file.c_str());
|
|
|
|
return true;
|
2014-06-05 06:42:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool workbook::save(const std::string &filename)
|
2014-05-21 22:20:30 +08:00
|
|
|
{
|
|
|
|
zip_file f(filename, file_mode::create, file_access::write);
|
2014-06-14 05:06:23 +08:00
|
|
|
|
2014-06-16 01:06:47 +08:00
|
|
|
f.set_file_contents("[Content_Types].xml", writer::write_content_types(*this));
|
2014-05-21 22:20:30 +08:00
|
|
|
|
2014-06-16 01:06:47 +08:00
|
|
|
f.set_file_contents("_rels/.rels", writer::write_root_rels());
|
|
|
|
f.set_file_contents("xl/_rels/workbook.xml.rels", writer::write_workbook_rels(*this));
|
2014-06-14 05:06:23 +08:00
|
|
|
|
|
|
|
f.set_file_contents("xl/workbook.xml", writer::write_workbook(*this));
|
2014-06-05 06:42:17 +08:00
|
|
|
|
|
|
|
return true;
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool workbook::operator==(const workbook &rhs) const
|
|
|
|
{
|
2014-06-14 03:05:24 +08:00
|
|
|
return d_.get() == rhs.d_.get();
|
2014-05-21 22:20:30 +08:00
|
|
|
}
|
2014-06-16 01:06:47 +08:00
|
|
|
|
|
|
|
std::vector<relationship> xlnt::workbook::get_relationships() const
|
|
|
|
{
|
|
|
|
return d_->relationships_;
|
2014-05-22 07:17:56 +08:00
|
|
|
}
|
2014-06-16 01:06:47 +08:00
|
|
|
|
|
|
|
std::vector<content_type> xlnt::workbook::get_content_types() const
|
|
|
|
{
|
|
|
|
std::vector<content_type> content_types;
|
|
|
|
content_types.push_back({ true, "xml", "xml" });
|
|
|
|
content_types.push_back({ true, "rels", "xml" });
|
|
|
|
content_types.push_back({ false, "xl/workbook.xml", "xml" });
|
|
|
|
return content_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|