C++类库LibXL从Excel读取数据的教程

这个例子从工作表的所有单元格中读取数据,检测单元格的类型并打印其数值。

LibXL是一个轻量级的Excel类库,支持各类平台运行,如需使用先行点击这里下载:

这个例子从工作表的所有单元格中读取数据,检测单元格的类型并打印其数值:

#include <iostream>#include "libxl.h"using namespace libxl;int main(){   Book* book = xlCreateBook();   if(book->load(L"input.xls"))   {      Sheet* sheet = book->getSheet(0);      if(sheet)      {         for(int row = sheet->firstRow(); row < sheet->lastRow(); ++row)         {            for(int col = sheet->firstCol(); col < sheet->lastCol(); ++col)            {               CellType cellType = sheet->cellType(row, col);               std::wcout << "(" << row << ", " << col << ") = ";               if(sheet->isFormula(row, col))               {                  const wchar_t* s = sheet->readFormula(row, col);                  std::wcout << (s s : L"null") << " [formula]";               }               else               {                  switch(cellType) //数据处理                  {                     case CELLTYPE_EMPTY: std::wcout << "[empty]"; break;                     case CELLTYPE_NUMBER:                     {                        double d = sheet->readNum(row, col);                        std::wcout << d << " [number]";                        break;                     }                     case CELLTYPE_STRING:                     {                        const wchar_t* s = sheet->readStr(row, col);                        std::wcout << (s s : L"null") << " [string]";                        break;                     }                     case CELLTYPE_BOOLEAN:                     {                        bool b = sheet->readBool(row, col);                        std::wcout << (b "true" : "false") << " [boolean]";                        break;                     }                     case CELLTYPE_BLANK: std::wcout << "[blank]"; break;                     case CELLTYPE_ERROR: std::wcout << "[error]"; break;                  }               }               std::wcout << std::endl;            }         }      }   }  // 为了应用的性能,记得释放资源呀   book->release();   return 0;}

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年1月3日
下一篇 2022年1月3日

相关推荐

发表回复

登录后才能评论