Files
dl_old/dl/io/dl_file_xlsx.cpp
T
2023-08-27 19:20:33 +08:00

56 lines
1.2 KiB
C++

#include "dl_file_xlsx.h"
#include <cassert>
#include <OpenXLSX.hpp>
#include "io/dl_log.h"
#include "io/dl_file.h"
namespace dl::FileXlsx
{
/**
* @brief 读取xlsx文件,拆解为每行
* @param[in] path_name 路径名称
* @retval 如果失败,会输出日志
*
* @note 出于性能考虑不判断是否为utf8编码,而是使用其他函数提前转换
*/
bool Read(std::string_view path_name, Vector2<std::string>& ret)
{
assert(ret.IsEmpty());
using namespace OpenXLSX;
g_log->PushLevel(LogSystem::Level::TEST);
XLDocument doc;
doc.open(std::string{ path_name });
XLWorkbook workbook = doc.workbook();
std::vector<std::string> vec_sheet = workbook.worksheetNames();
for (auto& sheet_name : vec_sheet)
{
log("Sheet: " + sheet_name);
XLWorksheet sheet = workbook.worksheet(sheet_name);
uint16_t count_x = sheet.columnCount();
uint32_t count_y = sheet.rowCount();
ret.Resize(toSize(count_x, 0));
for (uint32_t y = 0; y < count_y; ++y)
{
std::vector<std::string> line;
line.resize(count_x);
for (uint16_t x = 0; x < count_x; ++x)
{
line[x] = sheet.cell(y + 1, x + 1).value().get<std::string>();
}
ret.PushBack(line);
}
break;
}
g_log->Pop();
return true;
}
}