linux 上使用libxls读和使用xlslib写excel的方法简介
读取excel文件:libxls-1.4.0.zip
下载地址:http://sourceforge.net/projects/libxls/
安装方法:
./configure
make
make install
//示例:
建立文件readXls.cpp, 代码如下:
#include <stdexcept>
#include <xls.h>
#include <iostream>
using namespace xls;
using namespace std;
/////////////////////////////////////////////////
int main(int argc, char **argv)
{
if (argc < 2)
{
cerr << "please input the excel file." << endl;
return 1;
}
xlsWorkBook* pWorkBook = xls_open(argv[1], "UTF-8");
if (NULL == pWorkBook)
{
cerr << "file is not excel" << endl;
return 1;
}
xlsWorkSheet* pWorkSheet = xls_getWorkSheet(pWorkBook, 0);
xls_parseWorkSheet(pWorkSheet);
for (int r=0; r<=pWorkSheet->rows.lastrow; r++)
{
xlsRow* row = &pWorkSheet->rows.row[r];
for (int c=0; c<pWorkSheet->rows.lastcol; c++)
{
BYTE* pCurCellInfo = row->cells.cell[c].str;
if (NULL != pCurCellInfo)
{
cout << pCurCellInfo;
getchar();
}
}
cout << endl;
}
xls_close_WS(pWorkSheet);
xls_close_WB(pWorkBook);
return 0;
}
编译:g++ readXls.cpp -o readXls -I/usr/local/libxls/include -L/usr/local/libxls/lib -lxlsreader
如果运行时出现:error while loading shared libraries: libxxx.so.1: cannot open shared
这表示:系统不知道xxx.so放在哪个目录下,这时候就要在/etc/ld.so.conf中加入xxx.so所在的目录
解决方法:
1.在/etc/ld.so.conf中加入【动态库所在路劲】,保存之后,再运行:/sbin/ldconfig –v更新一下配置即可。如libxls的动态库路径是 /usr/local/libxsl/lib
2.在/etc/ld.so.conf.d/下新建一个.conf文件,并在其中加入【动态库所在路劲】就可以了,在运行/sbin/ldconfig。
参考文档:
http://blog.csdn.net/yao_guet/article/details/7326065
http://blog.csdn.net/zhangqiu1989/article/details/8822853
api文档可参考:
http://www.codeweblog.com/libxls%E4%BD%BF%E7%94%A8/
/////////////////////////////////////////////////////////
生成excel文件:xlslib-package-2.5.0.zip
下载地址:http://sourceforge.net/projects/xlslib/
安装方法:
./configure
make
make install
示例:
建立文件writeXls.cpp,写入如下代码:
#include <string.h>
#include <xlslib/xlslib.h>
using namespace xlslib_core;
using namespace std;
int main (int argc, char *argv[])
{
workbook wb;
xf_t* xf = wb.xformat();
worksheet* ws;
ws = wb.sheet("sheet1");
string label = "Hello, World!";
ws->label(1,2,label,xf); // 从0开始数,第1行,第2列,即C3
wb.Dump("workbook.xls");
return 0;
}
编译:
g++ writeXls.cpp -lxls -I /usr/local/include/xlslib/ -I /usr/local/include/ -L /usr/local/lib/ -o writeXls
参考文档:http://ju.outofmemory.cn/entry/106483
声明:此博客都是参考别人的,为了自己方便使用,总结了一下!谢谢各位博主
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2015-11-10 Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置
2014-11-10 Qt解析XML文件(QXmlStreamReader)
2014-11-10 Qt XML读取写入操作
2014-11-10 QT QXmlStreamWriter用法小结
2014-11-10 QtXML 举例