| | |
| | | #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) { |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | |