diff --git a/source/detail/excel_serializer.cpp b/source/detail/excel_serializer.cpp index 3c7e5069..7f3bcd4c 100644 --- a/source/detail/excel_serializer.cpp +++ b/source/detail/excel_serializer.cpp @@ -321,28 +321,26 @@ void excel_serializer::write_data(bool /*as_template*/) void excel_serializer::write_worksheets() { - std::size_t index = 0; + std::size_t index = 1; - for (auto ws : workbook_) + for (const auto ws : workbook_) { - for (auto relationship : workbook_.get_relationships()) + auto target = "worksheets/sheet" + std::to_string(index++) + ".xml"; + + for (const auto &rel : workbook_.get_relationships()) { - if (relationship.get_type() == relationship::type::worksheet && - workbook::index_from_ws_filename(relationship.get_target_uri()) == index) - { - worksheet_serializer serializer_(ws); - std::string ws_filename = (relationship.get_target_uri().substr(0, 3) != "xl/" ? "xl/" : "") + relationship.get_target_uri(); - std::ostringstream ss; - pugi::xml_document worksheet_xml; - serializer_.write_worksheet(worksheet_xml); - worksheet_xml.save(ss); - archive_.writestr(ws_filename, ss.str()); + if (rel.get_target_uri() != target) continue; - break; - } + worksheet_serializer serializer_(ws); + std::string ws_filename = (rel.get_target_uri().substr(0, 3) != "xl/" ? "xl/" : "") + rel.get_target_uri(); + std::ostringstream ss; + pugi::xml_document worksheet_xml; + serializer_.write_worksheet(worksheet_xml); + worksheet_xml.save(ss); + archive_.writestr(ws_filename, ss.str()); + + break; } - - index++; } } diff --git a/source/detail/workbook_serializer.cpp b/source/detail/workbook_serializer.cpp index 8c55e85a..97805e4f 100644 --- a/source/detail/workbook_serializer.cpp +++ b/source/detail/workbook_serializer.cpp @@ -277,10 +277,11 @@ void workbook_serializer::write_workbook(pugi::xml_document &xml) const auto sheets_node = root_node.append_child("sheets"); auto defined_names_node = root_node.append_child("definedNames"); + std::size_t index = 1; for (const auto ws : workbook_) { - auto target = "worksheets/sheet" + std::to_string(ws.get_id()) + ".xml"; + auto target = "worksheets/sheet" + std::to_string(index++) + ".xml"; for (const auto &rel : workbook_.get_relationships()) { @@ -301,6 +302,8 @@ void workbook_serializer::write_workbook(pugi::xml_document &xml) const "'" + ws.get_title() + "'!" + range_reference::make_absolute(ws.get_auto_filter()).to_string(); defined_name_node.text().set(name.c_str()); } + + break; } }