一、example.cpp
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <wchar.h>
//#include <locale.h>
#include "libxl.h"
using namespace libxl;
using namespace std;
#define FREE_PTR(p) (if(!p){free(p);p = NULL;})
//char *w2c(const wchar_t *pw);
char* getCellValue(char *sCont,int i,int j,Sheet *sheet,Book *book);
int main()
{
// Book* book = xlCreateBook();
Book* book = xlCreateXMLBook();
cout << "create book succ" << endl;
char sErrMsg[1024]; //错误信息
int iCellType = 0; //单元格格式
int iTotalRow = 0; //总行数
int iTotalCol = 0; //总列数
char sCont[1024]; //缓存
const char *p = NULL; //指针
//wchar_t *pWstr_content = NULL; //宽字符指针
//Returns the first row in the sheet that contains a used cell.
int iFirstRow = 0; //第一个有效的单元行
//Returns the zero-based index of the row after the last row in the sheet that contains a used cell.
int iLastRow = 0; //最后一个有效的单元行
//Returns the first column in the sheet that contains a used cell.
int iFirstCol = 0; //第一个有效的单元列
//Returns the zero-based index of the column after the last column in the sheet that contains a used cell.
int iLastCol = 0;
if(book)
{
cout << "load xls begin" << endl;
if(book->load("example.xlsx"))
{
cout << "load xls succ" << endl;
Sheet* sheet = book->getSheet(0);
if(sheet)
{
iFirstRow = sheet->firstRow();
iLastRow = sheet->lastRow();
iFirstCol = sheet->firstCol();
iLastCol = sheet->lastCol();
cout << "iFirstRow=" << iFirstRow << endl;
cout << "iLastRow=" << iLastRow << endl;
cout << "iFirstCol=" << iFirstCol << endl;
cout << "iLastCol=" << iLastCol << endl;
//单元格对象
//libxl::CellType celltype = sheet->cellType(3999,0);
//cout << "单元格格式" << celltype << endl;
//const char* s = sheet->readStr(2, 1);
//#if 0
for (int i=iFirstRow; i < iLastRow;i++)
{
for(int j = iFirstCol; j < iLastCol; j++)
{
memset(sCont,0x00,sizeof(sCont));
// pWstr_content = NULL;
//const char* pWstr_content = sheet->readStr(i,j);
//p = w2c(pWstr_content);
//cout << "p len = " << strlen(p) << endl;
//sprintf(sCont,"%s",p);
//FREE_PTR(p)
getCellValue(sCont,i,j,sheet,book);
/*
printf("第[%d]行,第[%d]列,单元格类型为:%d;\n",i+1,j+1,sheet->cellType(i,j));
p = sheet->readStr(i,j);
if(!p)
{
cout << " 第" << i+1 << "行,第" << j+1 <<"列数据为空,errmsg="\
<< book->errorMessage() << endl;
}
else
{
sprintf(sCont,"%s",p);
printf("第[%d]行,第[%d]列数据为:%s;",i+1,j+1,sCont);
}*/
}
printf("\n");
}
//#endif
/* const char* s = sheet->readStr(1, 1);
if(s)
{
std::cout << s << std::endl;
}
else
{
// memset(sErrMsg,0x00,sizeof(sErrMsg));
// sprintf(sErrMsg,"%s",book->errorMessage() );
cout << "sErrMsg=" << book->errorMessage() << endl;
}*/
//std:: cout << "s = " << s << endl;
/*
double d = sheet->readNum(3, 1);
std::cout << d << std::endl;
int year, month, day;
book->dateUnpack(sheet->readNum(4, 1), &year, &month, &day);
std::cout << year << "-" << month << "-" << day << std::endl;*/
}
else
{
cout << "sErrMsg=" << book->errorMessage() << endl;
}
}
else
{
cout << "sErrMsg=" << book->errorMessage() << endl;
}
cout << "release" << endl;
book->release();
}
else
{
cout << "sErrMsg=" << book->errorMessage() << endl;
}
return 0;
}
/*
*function: w2c
*notes: 宽字符串转ascii字符
*@pw: 宽字符
*返回: char*
*作者:xukai
*联系电话:13814066290
* */
char *w2c(const wchar_t *pw)
{
setlocale(LC_ALL,"zh_CN.utf8");
if(!pw)
return NULL;
size_t size= wcslen(pw)*sizeof(wchar_t);
char *pc;
if(!(pc = (char*)malloc(size)))
{
printf("malloc fail");
return NULL;
}
wcstombs(pc,pw,size);
return pc;
}
/*
*function: getCellValue
*notes: 获取单元格内容
* @str: 返回内容
* @i:行
* @j:列
* @book excel操作对象
* @sheet 单元格对象
*返回: char*
*作者:xukai
*联系电话:13814066290
* */
char* getCellValue(char *sCont,int i,int j,Sheet *sheet,Book *book)
{
const char *p = NULL;
//CellType {CELLTYPE_EMPTY, CELLTYPE_NUMBER, CELLTYPE_STRING, CELLTYPE_BOOLEAN, CELLTYPE_BLANK, CELLTYPE_ERROR};
CellType cellType = sheet->cellType(i,j);
// printf("cellType = %d\n",cellType);
double dVal = 0.00;
printf("第[%d]行,第[%d]列,单元格类型为:%d;\n",i+1,j+1,sheet->cellType(i,j));
switch(cellType)
{
case CELLTYPE_STRING:
p = sheet->readStr(i,j);
if(!p)
{
cout << " 第" << i+1 << "行,第" << j+1 <<"列数据为空,errmsg="\
<< book->errorMessage() << endl;
}
else
{
sprintf(sCont,"%s",p);
printf("第[%d]行,第[%d]列数据为:%s;",i+1,j+1,sCont);
}
break;
case CELLTYPE_NUMBER:
dVal = sheet->readNum(i,j);
sprintf(sCont,"%lf",dVal);
printf("第[%d]行,第[%d]列数据为:%s;",i+1,j+1,sCont);
break;
default:
sprintf(sCont,"%s","");
}
}
二、libxl api参考
http://libxl.com/documentation.html
三、makefile文件
使用的是libxl的example下面c++程序示例makefile
read: read.cpp
$(CC) -g -o read read.cpp $(CFLAGS)
三、EXCEL 测试
EXCEL 第一行为 说明 比如 姓名 年龄 工作
接下来的行列为对应数据
四、可以进行gdb调试
原来的makefile没有加 -g选项
五、问题
设置了单元格格式为文本,但是如果单元格是纯数字,单元格格式还是会被解析为NUMBER型