这2个是gnu的项目,我把它们修改下并且结合在一起作数据处理。
注意:
编译程序前现设好库和头文件 做好相应的连接
libxls 0.20 版本
test.c 载入xls文件 输出txt文件 输入内容和输入内容都写在程序中
sudo gcc test.c -o test -I /usr/include/libxls/ -L /usr/lib/libxls -lxlsreader
xlslib-1.0 使用的是 1.0版本
wb.c 载入 txt文件 输入 xls文件 输入内容和输出内容都些在程序中
sudo gcc wb.c -o wb -I /usr/include/xlslib/ -L /usr/lib/ -lxls
程序不支持中文
wb.c
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <xlslib/xlslib.h>
int
main (int argc, char *argv[])
{
workbook *w;
worksheet *ws;
int ret;
FILE *fp; //***
char str; //***
int rows = 0; //***
int cols = 0; //***
char tmp[255];
int i = 0;
w = newWorkbook ();
ws = callWorkbookSheet (w, "xlslib1");//打开要写入的
//if(!(fp=fopen("/opt/libxls-0.2.0/test.txt","r")))
if (!(fp = fopen ("/opt/libxls-0.2.0/test/test.txt", "r")))//打开读入文件
{
printf ("cannot open file/n");
exit (1);
}
while ((str = getc (fp)) != EOF) //检测文件是不结束
{
if (str != '/t' && str != '/n') //不是tab,字符组合
{
printf ("%i/n", i);
printf ("%c", str);
tmp[i] = str;
// strcpy(&tmp[i],str);
i++;
}
//是tab 则输出到表格并让指针归0
else
{
tmp[i] = '/0';
printf ("第%d行/n", rows);
printf ("第%d列/n", cols);
printf ("%s", tmp);
callWorksheetLabel (ws, rows, cols, tmp, 0);
cols++;
i = 0;
}
//检测文件是不换行
if (str == '/n')
{
rows++;
cols = 0;
} //如果还行,行数+1,列数规0
}
fclose (fp);
/* callWorksheetNumber(ws, 1, 1, 1.0, FMT_GENERAL, 0);
callWorksheetNumber(ws, 2, 1, 1.0, FMT_GENERAL, 0);
callWorksheetNumber(ws, 3, 1, 1.0, FMT_GENERAL, 0);
callWorksheetLabel(ws, 4, 1, "=SUM(A1:A3)", 0);
*/
ret = callWorkbookDump (w, "fooper.xls");
printf ("saved it ret=%d!/n", ret);
deleteWorkbook (w);
printf ("deleted it!/n");
return 0;
}
test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <libxls/xls.h>
int
main ()
{
xlsWorkBook *pWB;
xlsWorkSheet *pWS;
FILE *f;
int i;
struct st_row_data *row;
WORD t, tt;
pWB = xls_open ("files/test2.xls", "ASCII"); // "KOI8-R"打开要转换的xls文件
if (pWB != NULL)
{
f = fopen ("test.txt", "w"); //打开要写入的文件
for (i = 0; i < pWB->sheets.count; i++)
printf ("Sheet N%i (%s) pos %i/n", i, pWB->sheets.sheet[i].name,
pWB->sheets.sheet[i].filepos);
pWS = xls_getWorkSheet (pWB, 0);
xls_parseWorkSheet (pWS);
for (t = 0; t <= pWS->rows.lastrow; t++) //对xls行处理
{
row = &pWS->rows.row[t];
for (tt = 0; tt <= pWS->rows.lastcol; tt++) //列处理
{
if (!row->cells.cell[tt].ishiden)
{
if (row->cells.cell[tt].colspan)
fprintf (f, " colspan=%i", row->cells.cell[tt].colspan);
if (row->cells.cell[tt].rowspan)
fprintf (f, " rowspan=%i", row->cells.cell[tt].rowspan);
if (row->cells.cell[tt].str != NULL
&& row->cells.cell[tt].str[0] != '/0')
fprintf (f, "%s", row->cells.cell[tt].str);
fprintf (f, "/t");
}
}
fprintf (f, "/n");
}
printf ("Count of rows: %i/n", pWS->rows.lastrow);
printf ("Max col: %i/n", pWS->rows.lastcol);
printf ("Count of sheets: %i/n", pWB->sheets.count);
fclose (f);
xls_showBookInfo (pWB);
}
return 0;
}