Linux libxls和xlslib读写Excel文档

这里要讨论的是两款开源库 libxlsxlslib,前者用与 Excel,后者用于 .所以可以在Linux或Windows上使用

libxls(读Excel

获取libxls

可以在 官网 获取源代码, 目前最新版为 1.4.0

unzip libxls-1.4.0.zip
cd libxls
./configure
make
make install

注意默认安装到 /usr/local 目录下,所以需要自己手动复制相关文件到系统目录下

cp -r /usr/local/libxls/include/* /usr/include
cp -r /usr/local/libxls/lib/* /usr

至此基本上可以了

例子

test.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <xls.h>
using namespace std;
using namespace xls;
int main(int argc,char *argv[]) {
//版本信息
cout<<"version:"<<xls::xls_getVersion()<<endl;
xlsWorkBook *pwb;
xlsWorkSheet *pws;
//打开一个excel表格
pwb=xls_open("test.xls","UTF-8");
if(!pwb){
cout<<"open failed!"<<endl;
exit(EXIT_SUCCESS);
}
xlsSummaryInfo *summaryInfo=xls_summaryInfo(pwb);
if(summaryInfo){
if(summaryInfo->title)cout<<"title:"<<summaryInfo->title<<endl;
if(summaryInfo->manager)cout<<"manager:"<<summaryInfo->manager<<endl;
if(summaryInfo->lastAuthor)cout<<"lastAuthor:"<<summaryInfo->lastAuthor<<endl;
if(summaryInfo->keywords)cout<<"keywords:"<<summaryInfo->keywords<<endl;
if(summaryInfo->company)cout<<"company:"<<summaryInfo->company<<endl;
if(summaryInfo->comment)cout<<"comment:"<<summaryInfo->comment<<endl;
if(summaryInfo->appName)cout<<"appName:"<<summaryInfo->appName<<endl;
if(summaryInfo->subject)cout<<"subject:"<<summaryInfo->subject<<endl;
}
cout<<"charset:"<<pwb->charset<<endl;
cout<<"font name:"<<pwb->fonts.font->name<<endl;
cout<<"font bold:"<<pwb->fonts.font->bold<<endl;
cout<<"当前表名:"<<pwb->sheets.sheet->name<<endl;
cout<<"总表数:"<<pwb->sheets.count<<endl<<endl;
//获取第一张表单 索引从0开始
pws= xls_getWorkSheet(pwb,0);
//开始解析表单
xls_parseWorkSheet(pws);
cout<<"行数:"<<pws->rows.lastrow+1<<endl;
cout<<"列数:"<<pws->rows.lastcol<<endl;
//遍历
for (int i = 0; i < pws->rows.lastrow+1; ++i) {
//xlsRow = st_row::st_row_data
//获取每一行的数据
st_row::st_row_data row= pws->rows.row[i];
for (int j = 0; j < pws->rows.lastcol; ++j) {
if(row.cells.cell[j].str) {
cout << (char*)row.cells.cell[j].str << "\t";
}
}
cout<<endl;
}
//关闭
xls_close_WS(pws);
xls_close_WB(pwb);

return 0;
}

注意,由于我使用 clion 编写代码,所以在链接库时在 CMakeLists.txt 最后一行添加 target_link_libraries(libxls_read libxlsreader.so) 其中 libxls_read 为项目名.
或者直接 g++ test.cpp -o libxls_read -lxlsreader

测试:
假设有一个Excel文件
img

执行程序输出

version:1.4.0
标题:我是标题
keywords:我是关键字
comment:这是一个测试
subject:我是主题
charset:UTF-8
font name:文泉驿正黑
font bold:400
当前表名:第一张表单
总表数:1

行数:4
列数:5
刘备 关羽 张飞 曹操 刘禅
曹丕 赵云 孙权 黄盖 曹植
貂蝉 张角 孔明 周瑜 小乔
马超

xlslib(写Excel

获取xlslib

可以从 http://sourceforge.net/projects/xlslib/ 获取 xlslib并编译安装,同样的默认也是安装到 /usr/local/include ,需手动复制到系统目录下

cp -r /usr/local/include/xlslib /usr/include/

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <xlslib/xlslib.h>
using namespace xlslib_core;
using namespace std;
int main() {
workbook wb;
string label;
printf("Version: %s\n",wb.version());
//设置字体
font_t *t=wb.font("Ubuntu");
t->SetColor(CLR_RED);
t->SetItalic(true);
t->SetHeight(20*15);
// t->SetBoldStyle(BOLDNESS_BOLD);
xf_t *xf= wb.xformat();
xf->SetFillBGColor(CLR_GREEN);
xf->SetFillFGColor(CLR_RED);
xf->SetFont(t);
//第一张表单
worksheet *ws=wb.sheet("one");
//第二张表单
worksheet* ws2=wb.sheet("two");
ws->MakeActive();
//设置列宽度,行高度
ws->defaultColwidth(15);
ws->defaultRowHeight(25);
//6行
for (int i = 0; i < 6; ++i) {
//6列
for (int j = 0; j <6 ; ++j) {
char buf[20]={0};
sprintf(buf,"%d",i*j);
label=buf;
// 写入
cell_t * cell= ws->label(i,j,label,xf);
//或者 ws->number(i,j,i*j,xf);
//设置字体阴影
cell->fontshadow(true);
//设置单元格文本对齐
cell->halign(HALIGN_CENTER);
cell->valign(VALIGN_CENTER);
//设置行高度
ws->rowheight(i,20*15);
}
}
range *ran=ws->rangegroup(1,1,1,1);
ran->cellcolor(CLR_GREEN);
//保存到excel文件
wb.Dump("mynew.xls");
return 0;
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!