98 lines
2.2 KiB
C++
98 lines
2.2 KiB
C++
#include "dl_file_xlsx.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <OpenXLSX.hpp>
|
|
|
|
#include "io/dl_log.h"
|
|
#include "io/dl_file.h"
|
|
#include "memory/dl_code_cvt.h"
|
|
|
|
namespace dl::FileXlsx
|
|
{
|
|
|
|
bool Read(std::string_view path_name, std::string_view i_sheet_name, Vector2<std::string, size_t>& ret)
|
|
{
|
|
assert(ret.IsEmpty());
|
|
|
|
LogLevelSet l{ LogSystem::Level::ERR };
|
|
|
|
using namespace OpenXLSX;
|
|
XLDocument doc;
|
|
|
|
try
|
|
{
|
|
doc.open(std::string{ path_name });
|
|
}
|
|
catch (...)
|
|
{
|
|
log_err("文件解析失败:{}", path_name);
|
|
return false;
|
|
}
|
|
|
|
XLWorkbook workbook = doc.workbook();
|
|
std::vector<std::string> vec_sheet = workbook.worksheetNames();
|
|
for (auto& sheet_name : vec_sheet)
|
|
{
|
|
if(!i_sheet_name.empty() && sheet_name != i_sheet_name)
|
|
continue;
|
|
//log("Sheet: " + sheet_name);
|
|
XLWorksheet sheet = workbook.worksheet(sheet_name);
|
|
uint16_t count_x = sheet.columnCount();
|
|
if (count_x >= 16384)
|
|
{
|
|
log_err("文件列数 >= 16384,请检查文件:{}", path_name);
|
|
return false;
|
|
}
|
|
uint32_t count_y = sheet.rowCount();
|
|
if (count_y >= 16384)
|
|
{
|
|
log_err("文件行数 >= 16384,请检查文件:{}", path_name);
|
|
return false;
|
|
}
|
|
ret.Resize(toSizeT(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)
|
|
{
|
|
const XLCell& cell = sheet.cell(y + 1, x + 1);
|
|
const XLCellValueProxy& value = cell.value();
|
|
const XLValueType& type = value.type();
|
|
switch (type)
|
|
{
|
|
case OpenXLSX::XLValueType::Empty:
|
|
line[x] = {};
|
|
break;
|
|
case OpenXLSX::XLValueType::Boolean:
|
|
line[x] = std::to_string(value.get<bool>());
|
|
break;
|
|
case OpenXLSX::XLValueType::Integer:
|
|
line[x] = std::to_string(value.get<int>());
|
|
break;
|
|
case OpenXLSX::XLValueType::Float:
|
|
line[x] = std::to_string(value.get<double>());
|
|
break;
|
|
case OpenXLSX::XLValueType::String:
|
|
line[x] = value.get<std::string>();
|
|
break;
|
|
case OpenXLSX::XLValueType::Error:
|
|
line[x] = {};
|
|
log_err("类型错误!");
|
|
break;
|
|
default:
|
|
assert(0);
|
|
line[x] = {};
|
|
break;
|
|
}
|
|
//log_info("{}", line[x]);
|
|
}
|
|
ret.PushBack(line);
|
|
}
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} |