xlnt/source/serialization/excel_serializer.cpp

274 lines
8.7 KiB
C++
Raw Normal View History

2015-10-31 18:54:41 +08:00
#include <algorithm>
2015-10-31 18:52:59 +08:00
#include <iterator>
2015-11-02 01:31:29 +08:00
#include <xlnt/serialization/excel_serializer.hpp>
#include <xlnt/serialization/manifest_serializer.hpp>
#include <xlnt/serialization/relationship_serializer.hpp>
#include <xlnt/serialization/shared_strings_serializer.hpp>
#include <xlnt/serialization/style_serializer.hpp>
#include <xlnt/serialization/theme_serializer.hpp>
#include <xlnt/serialization/workbook_serializer.hpp>
#include <xlnt/serialization/worksheet_serializer.hpp>
#include <xlnt/serialization/xml_document.hpp>
#include <xlnt/serialization/xml_node.hpp>
#include <xlnt/serialization/xml_serializer.hpp>
2015-11-03 22:06:01 +08:00
#include <xlnt/packaging/document_properties.hpp>
#include <xlnt/packaging/manifest.hpp>
#include <xlnt/utils/exceptions.hpp>
2015-10-21 11:30:10 +08:00
#include <xlnt/workbook/workbook.hpp>
2015-10-30 07:37:07 +08:00
#include <xlnt/worksheet/worksheet.hpp>
#include <detail/constants.hpp>
2015-10-21 11:30:10 +08:00
namespace {
std::string::size_type find_string_in_string(const std::string &string, const std::string &substring)
2015-10-21 11:30:10 +08:00
{
std::string::size_type possible_match_index = string.find(substring.at(0));
while (possible_match_index != std::string::npos)
2015-10-21 11:30:10 +08:00
{
if (string.substr(possible_match_index, substring.size()) == substring)
2015-10-21 11:30:10 +08:00
{
return possible_match_index;
}
2015-10-21 11:30:10 +08:00
possible_match_index = string.find(substring.at(0), possible_match_index + 1);
}
2015-10-21 11:30:10 +08:00
return possible_match_index;
}
2015-10-30 07:37:07 +08:00
bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xlnt::workbook &wb)
2015-10-24 02:42:36 +08:00
{
wb.set_guess_types(guess_types);
wb.set_data_only(data_only);
2015-11-03 05:45:05 +08:00
if(!archive.has_file(xlnt::constants::ArcContentTypes()))
2015-11-02 12:52:19 +08:00
{
throw xlnt::invalid_file_exception("missing [Content Types].xml");
}
2015-10-30 07:37:07 +08:00
xlnt::manifest_serializer ms(wb.get_manifest());
2015-11-03 03:22:13 +08:00
xlnt::xml_document manifest_xml;
2015-11-03 05:45:05 +08:00
manifest_xml.from_string(archive.read(xlnt::constants::ArcContentTypes()));
2015-11-03 03:22:13 +08:00
ms.read_manifest(manifest_xml);
if (ms.determine_document_type() != "excel")
2015-10-24 02:42:36 +08:00
{
2015-11-02 12:52:19 +08:00
throw xlnt::invalid_file_exception("package is not an OOXML SpreadsheetML");
2015-10-24 02:42:36 +08:00
}
2015-10-24 02:42:36 +08:00
wb.clear();
2015-11-02 12:52:19 +08:00
2015-11-03 05:45:05 +08:00
if(archive.has_file(xlnt::constants::ArcCore()))
2015-11-02 12:52:19 +08:00
{
xlnt::workbook_serializer workbook_serializer_(wb);
2015-11-03 03:22:13 +08:00
xlnt::xml_document core_properties_xml;
2015-11-03 05:45:05 +08:00
core_properties_xml.from_string(archive.read(xlnt::constants::ArcCore()));
2015-11-03 03:22:13 +08:00
workbook_serializer_.read_properties_core(core_properties_xml);
2015-11-02 12:52:19 +08:00
}
2015-11-04 07:26:33 +08:00
xlnt::relationship_serializer relationship_serializer_(archive);
auto workbook_relationships = relationship_serializer_.read_relationships(xlnt::constants::ArcWorkbook());
for (const auto &relationship : workbook_relationships)
2015-10-24 02:42:36 +08:00
{
wb.create_relationship(relationship.get_id(), relationship.get_target_uri(), relationship.get_type());
}
2015-11-03 03:22:13 +08:00
xlnt::xml_document xml;
2015-11-03 05:45:05 +08:00
xml.from_string(archive.read(xlnt::constants::ArcWorkbook()));
2015-10-31 06:54:04 +08:00
auto root_node = xml.get_child("workbook");
2015-10-31 06:54:04 +08:00
auto workbook_pr_node = root_node.get_child("workbookPr");
wb.get_properties().excel_base_date =
(workbook_pr_node.has_attribute("date1904") && workbook_pr_node.get_attribute("date1904") != "0")
? xlnt::calendar::mac_1904
: xlnt::calendar::windows_1900;
2015-11-03 05:45:05 +08:00
if(archive.has_file(xlnt::constants::ArcSharedString()))
2015-10-31 06:54:04 +08:00
{
std::vector<std::string> shared_strings;
2015-11-03 03:22:13 +08:00
xlnt::xml_document shared_strings_xml;
2015-11-03 05:45:05 +08:00
shared_strings_xml.from_string(archive.read(xlnt::constants::ArcSharedString()));
2015-11-03 06:25:10 +08:00
xlnt::shared_strings_serializer::read_shared_strings(shared_strings_xml, shared_strings);
2015-11-02 12:52:19 +08:00
for (auto shared_string : shared_strings)
{
wb.add_shared_string(shared_string);
}
2015-10-31 06:54:04 +08:00
}
2015-10-30 07:37:07 +08:00
xlnt::style_serializer style_reader_(wb);
2015-11-03 03:22:13 +08:00
xlnt::xml_document style_xml;
2015-11-03 05:45:05 +08:00
style_xml.from_string(archive.read(xlnt::constants::ArcStyles()));
2015-11-03 03:22:13 +08:00
style_reader_.read_stylesheet(style_xml);
2015-10-31 06:54:04 +08:00
auto sheets_node = root_node.get_child("sheets");
for (auto sheet_node : sheets_node.get_children())
2015-10-24 02:42:36 +08:00
{
2015-10-30 07:37:07 +08:00
auto rel = wb.get_relationship(sheet_node.get_attribute("r:id"));
auto ws = wb.create_sheet(sheet_node.get_attribute("name"), rel);
2015-11-03 05:45:05 +08:00
2015-11-02 12:52:19 +08:00
//TODO: this is really bad
auto ws_filename = (rel.get_target_uri().substr(0, 3) != "xl/" ? "xl/" : "") + rel.get_target_uri();
auto sheet_type = wb.get_manifest().get_override_type(ws_filename);
2015-11-03 05:45:05 +08:00
if(rel.get_type() != xlnt::relationship::type::worksheet)
{
continue;
}
2015-11-02 12:52:19 +08:00
2015-10-30 07:37:07 +08:00
xlnt::worksheet_serializer worksheet_serializer(ws);
2015-11-03 03:22:13 +08:00
xlnt::xml_document worksheet_xml;
worksheet_xml.from_string(archive.read(ws_filename));
worksheet_serializer.read_worksheet(worksheet_xml);
2015-10-24 02:42:36 +08:00
}
2015-10-30 07:37:07 +08:00
return true;
2015-10-24 02:42:36 +08:00
}
2015-10-21 11:30:10 +08:00
} // namespace
namespace xlnt {
const std::string excel_serializer::central_directory_signature()
2015-10-21 11:30:10 +08:00
{
return "\x50\x4b\x05\x06";
}
std::string excel_serializer::repair_central_directory(const std::string &original)
2015-10-21 11:30:10 +08:00
{
2015-10-30 07:37:07 +08:00
auto pos = find_string_in_string(original, central_directory_signature());
if (pos != std::string::npos)
2015-10-21 11:30:10 +08:00
{
return original.substr(0, pos + 22);
}
2015-10-21 11:30:10 +08:00
return original;
}
2015-10-30 07:37:07 +08:00
bool excel_serializer::load_stream_workbook(std::istream &stream, bool guess_types, bool data_only)
2015-10-21 11:30:10 +08:00
{
2015-11-02 12:52:19 +08:00
std::vector<std::uint8_t> bytes;
2015-11-03 05:45:05 +08:00
//TODO: inefficient?
2015-11-02 12:52:19 +08:00
while (stream.good())
{
2015-11-03 05:45:05 +08:00
bytes.push_back(static_cast<std::uint8_t>(stream.get()));
2015-11-02 12:52:19 +08:00
}
2015-10-30 07:37:07 +08:00
return load_virtual_workbook(bytes, guess_types, data_only);
2015-10-21 11:30:10 +08:00
}
bool excel_serializer::load_workbook(const std::string &filename, bool guess_types, bool data_only)
2015-10-21 11:30:10 +08:00
{
2015-10-24 02:42:36 +08:00
try
{
2015-10-30 07:37:07 +08:00
archive_.load(filename);
2015-10-24 02:42:36 +08:00
}
catch (std::runtime_error)
2015-10-24 02:42:36 +08:00
{
throw invalid_file_exception(filename);
}
2015-10-31 06:54:04 +08:00
return ::load_workbook(archive_, guess_types, data_only, workbook_);
2015-10-24 02:42:36 +08:00
}
2015-10-21 11:30:10 +08:00
2015-10-30 07:37:07 +08:00
bool excel_serializer::load_virtual_workbook(const std::vector<std::uint8_t> &bytes, bool guess_types, bool data_only)
2015-10-24 02:42:36 +08:00
{
2015-10-30 07:37:07 +08:00
archive_.load(bytes);
2015-10-31 06:54:04 +08:00
return ::load_workbook(archive_, guess_types, data_only, workbook_);
2015-10-21 11:30:10 +08:00
}
2015-10-31 06:54:04 +08:00
excel_serializer::excel_serializer(workbook &wb) : workbook_(wb)
2015-10-30 01:46:56 +08:00
{
}
2015-10-31 06:54:04 +08:00
void excel_serializer::write_data(bool /*as_template*/)
2015-10-30 01:46:56 +08:00
{
2015-11-04 07:26:33 +08:00
relationship_serializer relationship_serializer_(archive_);
relationship_serializer_.write_relationships(workbook_.get_root_relationships(), "");
relationship_serializer_.write_relationships(workbook_.get_relationships(), constants::ArcWorkbook());
2015-10-30 07:37:07 +08:00
xml_document properties_app_xml;
2015-10-31 06:54:04 +08:00
workbook_serializer workbook_serializer_(workbook_);
2015-11-03 05:45:05 +08:00
archive_.writestr(constants::ArcApp(), xml_serializer::serialize(workbook_serializer_.write_properties_app()));
archive_.writestr(constants::ArcCore(), xml_serializer::serialize(workbook_serializer_.write_properties_core()));
2015-10-30 07:37:07 +08:00
theme_serializer theme_serializer_;
2015-11-03 05:45:05 +08:00
archive_.writestr(constants::ArcTheme(), theme_serializer_.write_theme(workbook_.get_loaded_theme()).to_string());
archive_.writestr(
2015-11-03 05:45:05 +08:00
constants::ArcSharedString(),
2015-11-03 06:25:10 +08:00
xml_serializer::serialize(xlnt::shared_strings_serializer::write_shared_strings(workbook_.get_shared_strings())));
2015-11-03 05:45:05 +08:00
archive_.writestr(constants::ArcWorkbook(), xml_serializer::serialize(workbook_serializer_.write_workbook()));
2015-10-31 06:54:04 +08:00
style_serializer style_serializer_(workbook_);
2015-11-03 05:45:05 +08:00
archive_.writestr(constants::ArcStyles(), style_serializer_.write_stylesheet().to_string());
2015-10-31 06:54:04 +08:00
manifest_serializer manifest_serializer_(workbook_.get_manifest());
2015-11-03 05:45:05 +08:00
archive_.writestr(constants::ArcContentTypes(), manifest_serializer_.write_manifest().to_string());
2015-10-30 07:37:07 +08:00
write_worksheets();
2015-10-30 01:46:56 +08:00
}
2015-10-30 07:37:07 +08:00
void excel_serializer::write_worksheets()
2015-10-30 01:46:56 +08:00
{
std::size_t index = 0;
for (auto ws : workbook_)
2015-10-30 01:46:56 +08:00
{
for (auto relationship : workbook_.get_relationships())
2015-10-30 01:46:56 +08:00
{
if (relationship.get_type() == relationship::type::worksheet &&
workbook::index_from_ws_filename(relationship.get_target_uri()) == index)
2015-10-30 01:46:56 +08:00
{
2015-10-30 07:37:07 +08:00
worksheet_serializer serializer_(ws);
std::string ws_filename = (relationship.get_target_uri().substr(0, 3) != "xl/" ? "xl/" : "") + relationship.get_target_uri();
2015-10-31 06:54:04 +08:00
archive_.writestr(ws_filename, serializer_.write_worksheet().to_string());
2015-10-30 01:46:56 +08:00
break;
}
}
2015-10-30 01:46:56 +08:00
index++;
}
}
2015-10-30 07:37:07 +08:00
void excel_serializer::write_external_links()
{
}
2015-10-30 07:37:07 +08:00
bool excel_serializer::save_stream_workbook(std::ostream &stream, bool as_template)
2015-10-30 01:46:56 +08:00
{
2015-10-30 07:37:07 +08:00
write_data(as_template);
archive_.save(stream);
2015-10-30 07:37:07 +08:00
return true;
2015-10-30 01:46:56 +08:00
}
bool excel_serializer::save_workbook(const std::string &filename, bool as_template)
2015-10-30 01:46:56 +08:00
{
2015-10-30 07:37:07 +08:00
write_data(as_template);
archive_.save(filename);
2015-10-30 01:46:56 +08:00
return true;
}
2015-10-30 07:37:07 +08:00
bool excel_serializer::save_virtual_workbook(std::vector<std::uint8_t> &bytes, bool as_template)
2015-10-30 01:46:56 +08:00
{
2015-10-30 07:37:07 +08:00
write_data(as_template);
archive_.save(bytes);
2015-10-30 07:37:07 +08:00
return true;
2015-10-30 01:46:56 +08:00
}
2015-10-21 11:30:10 +08:00
} // namespace xlnt