admin
2022-09-30 1a16b19acb23a4f28bafd01f3ed80fb225a96c3e
app/ExcelUtil.cpp
@@ -2,6 +2,8 @@
#include "ExcelUtil.h"
#include "json/json.h"
#include "xlslib.h"
#include <libxls/xlsstruct.h>
using namespace xls;
using namespace xlslib_core;
void ExcelUtil::save(list<TradeData> data, string path) {
@@ -94,3 +96,40 @@
    wb.Dump(path);
}
void ExcelUtil::read(string path)
{
    xlsWorkBook* pWB = NULL;
    // 工作表
    xlsWorkSheet* pWS = NULL;
    // 单元格
    xlsCell* cell = NULL;
    int sheetIndex;
    int row, col;
    // 打开文件
    pWB = xls_open(path.c_str(), "UTF-8");
    if (!pWB) {
        throw string("Open File Error!");
    }
    // 解析xls文件,这个不要忘了
    xls_parseWorkBook(pWB);
    fprintf(stderr, "Sheet count:%d\n", pWB->sheets.count);
    pWS = xls_getWorkSheet(pWB, 0);
    // 解析工作表
    xls_parseWorkSheet(pWS);
    fprintf(stderr, "Sheet %d name: %s\n", sheetIndex, (char*)pWB->sheets.sheet[sheetIndex].name);
    fprintf(stderr, "Sheet Data:\n");
    // 每行
    for (row = 0; row < pWS->rows.lastrow; ++row) {
        cell = xls_cell(pWS, row, 1);
        if (cell && cell->str) {
                fprintf(stderr, "%s", (char*)cell->str);
        }
    }
}