Skip to content

Speed up XLSX load and save by caching values rather than calling XLNT functions for each row. #2387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Libs/Project/ExcelProjectReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class ExcelProjectReader::Container {
auto rows = ws.rows(false);
auto headers = rows[0];

for (int i = ws.lowest_row(); i < ws.highest_row(); i++) {
int highest_row = ws.highest_row();
for (int i = ws.lowest_row(); i < highest_row; i++) {
StringMap map;
bool empty = true;
for (int h = 0; h < headers.length(); h++) {
Expand Down
20 changes: 17 additions & 3 deletions Libs/Project/ExcelProjectWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ExcelProjectWriter.h"

#include <Logging.h>

#include <xlnt/workbook/workbook_view.hpp>
#include <xlnt/xlnt.hpp>

Expand Down Expand Up @@ -28,8 +29,8 @@ static int get_index_for_column(xlnt::worksheet& ws, const std::string& name) {
}

//---------------------------------------------------------------------------
static void set_value(xlnt::worksheet& ws, int column, int subject_id, const std::string& value) {
ws.cell(xlnt::cell_reference(column, subject_id)).value(value);
static void set_value(xlnt::worksheet& ws, int column, int row, const std::string& value) {
ws.cell(xlnt::cell_reference(column, row)).value(value);
}

//---------------------------------------------------------------------------
Expand All @@ -38,18 +39,31 @@ static void set_value(xlnt::worksheet& ws, const std::string& column_name, int s
set_value(ws, column_index, subject_id + 2, value); // +1 for header, +1 for 1-indexed
}

//---------------------------------------------------------------------------
static void set_subject_value(xlnt::worksheet& ws, int column_index, int subject_id, const std::string& value) {
set_value(ws, column_index, subject_id + 2, value); // +1 for header, +1 for 1-indexed
}

//---------------------------------------------------------------------------
static void store_subjects(Project& project, xlnt::workbook& wb) {
xlnt::worksheet ws = wb.sheet_by_index(0);
ws.title("data");

// cache column indices for performance, get_index_for_column can be expensive
std::map<std::string, int> column_map;

auto subjects = project.get_subjects();
for (int i = 0; i < subjects.size(); i++) {
auto subject = subjects[i];
auto map = ProjectUtils::convert_subject_to_map(&project, subject.get());

for (auto& [key, value] : map) {
set_value(ws, key, i, value);
if (column_map.find(key) == column_map.end()) {
int column_index = get_index_for_column(ws, key);
column_map[key] = column_index;
}

set_subject_value(ws, column_map[key], i, value);
}
}
}
Expand Down
Loading