linux c++读取excel文件的库

本文介绍了libxls库用于读取xls文件的步骤,包括下载、安装和示例代码。同时提到了libxl库,它支持读取xls及xlsx文件,但有容量限制,以及如何配置和使用libxl库进行文件读取。最后提及了正在研究中的OpenXLSX库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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还在研究中

这是曾经做过的一个项目时写的一个动态,用c++来操作Excel,包括以下功能: //===================================mainly function============================ //FuncName: xls_create //function: create excel server //return value: return 1 if success, otherwise 0 _export bool xls_create(); //FuncName: xls_new_file/xls_new_file2 //function: create an excel file (from a template) //parameter: strTemplate ->excel template file name,eg: "template.xlt" or "d:\teplate.xls" // strFileName ->the file name used to save excel file as,eg: "Temp.xls" // openfile ->1: open excel file after writen it,otherwize don't open it //return value: return the excel fileID which created from template, between 1 and 256 if success, or zero otherwise _export int xls_new_file(char* szTemplate, char* szFileName, int openfile); _export int xls_new_file2(char* szFileName, int openfile); //FuncName: xls_open_file //function: Open an exist excel file //parameter:the file name will to be opened //return value: return the fileID, if open file success,otherwize return 0 _export int xls_open_file(char* szFileName); //FuncName: xls_write_xxx //function: write a double value to xls file //parameter: fileID ->the excel file ID which we will write the value into,between 1 and 256 // sheetID ->the sheet index of the excel file which we will write the value into,between 1 and 256 // crow ->the row index which we will write the value into, not less 1 // ccol ->the column index which we will write the value into, not less 1 // value ->the double value which we will write into excel //Note: if you want to input enter key in excel file,you may use \n in your str //return value: return 1 if success, otherwise 0 _export int xls_write_str(int fileID, int sheetID, int crow, int ccol, char* value); _export int xls_write_dbl(int fileID, int sheetID, int crow, int ccol, double value); _export int xls_write_int(int fileID, int sheetID, int crow, int
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值