libxls作为开源库,支持在Linux C/C++ 环境下解析(读)xls文件,github提供了源码:
https://github.com/libxls/libxls
但是github的源码需要一堆辅助工具,才能够编译出libxls的C静态库,因此琢磨了下手动编译libxls的静态库/动态库方法,可以摆脱辅助工具缺失乃至版本问题导致的抓狂,尤其适合不方便联网的内网开发环境。
将fuzz、src、include目录下所有文件拷贝到一个目录下,修改*.c和*.h文件中指定的include文件路径为当前目录下
然后将locale.c、endian.c、fuzz_xls.c、ole.c、xls.c、xlstool.c编译成相应的.o文件
locale.h文件中需要添加语句“#include
gcc -c locale.c
gcc -c endian.c
gcc -c ole.c
gcc -c xls.c
gcc -c xlstool.c
gcc -c fuzz_xls.c -std=c99 (fuzz_xls.c源文件需要指定c99标准)
选择test.c编译成.o文件
gcc -c test.c
将所有.o文件链接在一起生成可执行demo程序
gcc locale.o endian.o fuzz_xls.o ole.o xls.o xlstool.o test.o -o demo
也可以将locale.o、endian.o、fuzz_xls.o、ole.o、xls.o、xlstool.o编译成静态库
ar -rc libxls.a locale.o endian.o fuzz_xls.o ole.o xls.o xlstool.o
gcc -c test.c -L . -lxls -o demo
或者将locale.o、endian.o、fuzz_xls.o、ole.o、xls.o、xlstool.o编译成动态库
gcc -fPIC -shared locale.o endian.o fuzz_xls.o ole.o xls.o xlstool.o -o libxls.so
demo程序链接库
gcc test.c -L . -lxls -o demo
如果想要在C++代码中使用libxls,需要将XlsReader.cpp和XlsReader.h编译后链接到程序
g++ XlsReader.cpp main.cpp -L. -lxls -o demo -std=c++11
编写一个xls文件中获取指定id行的信息的demo:
#include#include #include #include #include #include "XlsReader.h" using namespace std; using namespace xls; int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: ./demo " << endl; exit(1); } string width, depth; int id = 3; flaot size[2] = {0}; string file = string(argv[1]); WorkBook foo(file); cellContent cell = foo.GetCell(0, 1, 2); for (int sheetNum = 0; sheetNum < foo.GetSheetCount; ++ sheetNum) //遍历xls所有sheet的列表 { if ( "sheet 1" != foo.GetSheetName(sheetNum) ) //选择sheet 1 continue; cout << "find sheet 1." << endl; foo.InitIterator(sheetNum); while (ture) { cellContent c = foo.GetNextCell(); if (c.type == cellBlank) break; if (c.row == 1) continue; if (c.col == 1) { if (stoi(c.str) == id) { width.assign(foo.GetCell(sheetNum, c.row, 3).str); depth.assign(foo.GetCell(sheetNum, c.row, 4).str); break; //找到后即退出 } } } } size[0] = atof(width.c_str()); size[1] = atof(depth.c_str()); cout << "Die No. : " << id << ", width: " << size[0] << ", depth: " << size[1] << endl; return 0; }



