FOSSredder 0.5.0
Loading...
Searching...
No Matches
XlntTableWriterAdapter.cpp
Go to the documentation of this file.
1
5
7
9
10#include <exception>
11#include <utility>
12
13#include <xlnt/xlnt.hpp>
14
15namespace infra::xlsx_writer {
16
17XlntTableWriterAdapter::XlntTableWriterAdapter(std::shared_ptr<core::ports::diagnostics::IErrorReporter> errorReporter)
18 : errorReporter_(std::move(errorReporter))
19{
20}
21
22bool XlntTableWriterAdapter::writeTable(const std::filesystem::path& outputPath,
23 const std::vector<std::vector<std::string>>& rows,
24 const std::string& worksheetTitle) const
25{
26 try {
27 xlnt::workbook workbook;
28 xlnt::worksheet worksheet = workbook.active_sheet();
29 worksheet.title(worksheetTitle);
30
31 for (size_t row = 0; row < rows.size(); ++row) {
32 const auto& columns = rows[row];
33 for (size_t column = 0; column < columns.size(); ++column) {
34 auto cell = worksheet.cell(static_cast<int>(column + 1), static_cast<int>(row + 1));
35 if (!columns[column].empty() && columns[column].front() == '=') {
36 cell.formula(columns[column].substr(1));
37 } else {
38 cell.value(columns[column]);
39 }
40 }
41 }
42
43 workbook.save(outputPath.string());
44 return true;
45 } catch (...) {
46 core::errors::reportException(errorReporter_.get(),
48 "infra::xlsx_writer::XlntTableWriterAdapter::writeTable",
49 std::current_exception());
50 return false;
51 }
52}
53
54} // namespace infra::xlsx_writer
Provides shared helpers for reporting structured errors and exceptions.
Declares the XLSX writer adapter used by export features.
bool writeTable(const std::filesystem::path &outputPath, const std::vector< std::vector< std::string > > &rows, const std::string &worksheetTitle) const override
Writes rows into a worksheet.
XlntTableWriterAdapter(std::shared_ptr< core::ports::diagnostics::IErrorReporter > errorReporter)
void reportException(core::ports::diagnostics::IErrorReporter *reporter, ErrorSeverity severity, const char *origin, std::exception_ptr exception)
Report an exception through the reporter's default exception handling.