libxls是C语言开源库用来读取excel 文档(后缀名:.xls),xlsLib写Excel文件,可以在 Linux 上使用。
读取excel文件:libxls-1.4.0.zip
安装方法:
./configure
make
make install
sudo cp -r -v /usr/local/libxls/include/libxls/ /usr/include
sudo cp -r -v /usr/local/libxls/include/xlx.h /usr/include
sudo cp -r -v /usr/local/libxls/lib/ /usr
//示例:
#include <stdio.h>
#include <stdlib.h>
#include <xls.h>
#include <unistd.h>
/////////////////////////////////////////////////
int main(int argc, char **argv)
{
xlsWorkBook *pWb;
xlsWorkSheet *pWs;
struct st_row_data *row;
int r,c;
char buf[512], result[512];
if (argc < 2)
{
sprintf(stderr, "please input the xml file.");
return EXIT_FAILURE;
}
// open workbook, choose standard coversion
pWb = xls_open(argv[1], "UTF-8");
if (NULL == pWb)
{
fprintf(stderr, "File not found!\n");
return EXIT_FAILURE;
}
// open and parse the first sheet
pWs = xls_getWorkSheet(pWb, 0);
xls_parseWorkSheet(pWs);
// process all rows of the first sheet
for (r=0; r<=pWs->rows.lastrow; r++)
{
row = &pWs->rows.row[r];
for (c=0; c<=pWs->rows.lastcol; c++)
{
if (row->cells.cell[c].str != NULL)
{
printf("%s\t",row->cells.cell[c].str);
}
}
printf("\n");
}
// close workSheet
xls_close_WS(pWs);
// close workbook
xls_close_WB(pWb);
return 0;
}
编译:gcc -o readXls readXls.c -lxlsreader
--------------------------------------------------------------------
生成excel文件:xlslib-package-2.5.0.zip
安装方法:
./configure
make
make install
示例:
#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++ helloxls.cpp -lxls -I /usr/local/include/xlslib/ -I /usr/local/include/ -L /usr/local/lib/ -o helloxls
libxls-1.4.0.zip与xlslib-package-2.5.0.zip可以到Linux公社资源站下载:
------------------------------------------分割线------------------------------------------
具体下载目录在 /2017年资料/2月/25日/Linux 上使用libxls读和使用xlsLib写Excel文件的方法/
------------------------------------------分割线------------------------------------------