1、libxls 该库只能读取xls文件
首先下载源码,地址:https://github.com/libxls/libxls
下载其Releases版本,如图所示,下载libxls-1.6.2.tar.gz
下载完成后解压,然后安装,以下内容都在解压后的文件夹中进行
[root@localhost home]# tar -zxvf libxls-1.6.2.tar.gz
[root@localhost home]# cd libxls-1.6.2
[root@localhost libxls-1.6.2]# ./configure --prefix=/usr/local
[root@localhost libxls-1.6.2]# make
[root@localhost libxls-1.6.2]# make install
库安装完成,但是有可能会出现函数找不到定义等类似错误,这是因为动态库的问题,需打开/etc/ld.so.conf文件加入动态库(libxlsreader.so)所在的位置,在安装阶段将文件安装在了/usr/local中,所以文件在/usr/local/lib里,讲路径/usr/local/lib添加在etc/ld.so.conf即可
[root@localhost libxls-1.6.2]# vim /etc/ld.so
样本demo
#include<xls.h>
using namespace xls;
int xlsRead(const char *fileName)
{
xlsWorkBook * pWB=NULL;
xlsWorkSheet *pWS=NULL;
xlsRow * rowstr=NULL;
xls_error_t *res;
int sheetIndex;
int row,col;
pWB= xls_open_file(fileName,"UTF-8",res);
if(!pWB)
{
cout<<filePathName<<"open error"<<endl;
return -1;
}
//resolution xls file
*res=xls_parseWorkBook(pWB);
if(*res>0)
{
cout<<res<<endl;
return -2;
}
//read sheet
for(sheetIndex=0;sheetIndex<pWB->sheets.count;++sheetIndex)
{
pWS=xls_getWorkSheet(pWB,sheetIndex);
*res=xls_parseWorkSheet(pWS);
cout<<pWB->sheets.sheet[sheetIndex].name<<endl;
rowstr= xls_row(pWS,0);
cout<< rowstr->cells.cell->str<<endl;
}
return 0;
}
2、libxl 该库可读取xls及xlsx文件,但容量有限
库下载地址:https://www.libxl.com/download.html
根据自己系统下载,图上内容,应为免费内容能解析的大小
解压后,其文件内容如下图,因为使用的是C++,所以在/usr/local/建一个文件夹mkdir libxl,将include_cpp的内容全部移到/usr/local/libxl中,cp -r /home/libxl-4.1.1/include_cpp/. /usr/local/libxl,将libxl.so文件挪到/usr/lib64/中libxl.so在lib64文件夹中,cp /home/libxl-4.1.1/lib64/libxl.so /usr/lib64
然后在程序中加入动态库libxl.so即可,其demo如下
#include<libxl/libxl.h>
using namespace libxl;
int xlsRead()
{
Book* book = xlCreateXMLBook();//读取xlsx文件用xlCreateXMLBook,如果读取xls使用xlCreateBook
if(book)
{
if(book->load("文件路径.xlsx"))
{
Sheet* sheet = book->getSheet(0);//获取sheet内容
const char* name =book->getSheetName(0);//获取sheet名字
cout<<name<<endl;
if(sheet)
{
const char* s = sheet->readStr(0, 4);//0为列,4为行
std::cout << s << std::endl;
}
}
book->release();
}
return 0;
}
3、OpenXLSX还在研究中