一、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型

linux下读取excel文件相关推荐

  1. linux系统读取excel文件是否存在,小弟我用poi读excel,在window下没有有关问题,但把程序放到linux上时,读取客户端的excel文件报错,不...

    我用poi读excel,在window下没有问题,但把程序放到linux上时,读取客户端的excel文件报错,不知道如何解决 我用poi读excel,在window下没有问题,但把程序放到linux上 ...

  2. linux 操作excel文件,Linux下输出excel文件

    今日提供给产品需求,需excel文件,故总结要点如下: 1.默认间隔是space_20 2.修改为TAB \t _09 awk '  BEGIN { OFS="\t"} ;{ $1 ...

  3. linux c++读取excel文件的库

    1.libxls 该库只能读取xls文件 首先下载源码,地址:https://github.com/libxls/libxls 下载其Releases版本,如图所示,下载libxls-1.6.2.ta ...

  4. linux 下 读取某个文件的某一行或者某几行

    wc -l  a.txt  统计a.txt 行数 查看文件a.txt的第190行到196行, sed -n '190,196p' a.txt 如果查看某一行用 sed -n '190,1p' a.tx ...

  5. Linux生成xlsx格式文件,linux下生成excel文件

    ps: od命令系统默认的显示方式是八进制,这也是该命令的名称由来(Octal Dump).但这不是最有用的显示方式,用ASCII码和十六进制组合的方式能提供更有价值的信息输出. 它们对于访问或可视地 ...

  6. python怎么读取excel文件-Python读写Excel文件方法介绍

    一.读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文件. 首先,打开workbook: 复制代码 代码如下: import xlrd ...

  7. Qt4 Linux下读写excel

    背景 最近项目中涉及到excel的读写,因为是在Linux上操作,而且是Qt4的版本,所以相对有些麻烦.之前我们用的是直接通过python去读写excel,把数据存储到中间文件.然后读中间文件进行操作 ...

  8. linux c编程获取excel文件内容,c读取excel文件内容

    如何用C语言实现读取excel文件中的数据呢? 要做的是在Linux环境下使用C语言实现快速读取excel文件并保存成文本格式基本思路 基础实现方法同上篇文章<直接通过ODBC读.写Excel表 ...

  9. python批量读取文件夹中的所有excel文件-python遍历文件夹下所有excel文件

    大数据处理经常要用到一堆表格,然后需要把数据导入一个list中进行各种算法分析,简单讲一下自己的做法: 1.如何读取excel文件 网上的版本很多,在xlrd模块基础上,找到一些源码: import ...

最新文章

  1. Windows中的一个类似路由器命令的命令
  2. 那些让你起飞的计算机基础知识
  3. Duplicate standby database from active database
  4. win10怎么把c盘锁住_老司机教你win10下怎么清理c盘
  5. golang错误:The process cannot access the file because it is being used by another process
  6. visual studio能统计代码规模吗_描述统计之用——熟悉数据
  7. 【数据结构】--章节2.3----线性表的链式表示和实现
  8. multisim10元件库介绍
  9. C++写OpenCV图像灰度化
  10. TX-LCN分布式事务之TCC模式
  11. Apple中文社区平台的 Mac 用户群体
  12. 深究embedding层
  13. 幼儿课外活动游戏_这15个户外游戏,让你组织活动收放自如
  14. C语言源代码转变为可执行程序的过程
  15. 初识结构体之座机电话号码
  16. tor 安装及使用教程
  17. DX11 游戏开发笔记 (二) DX11 基础框架三角形 下
  18. vue移动端下拉刷新、上拉加载
  19. 约瑟夫环问题(数学递推法)
  20. Android端 WebP图片压缩与传输的一点探索

热门文章

  1. CAD二次开发——1.环境搭建
  2. WL2803E25-5/TR超低压差 低压静态电流 高PSRR CMOS LDO
  3. 年薪30W的程序员,都在哪些平台兼职接私活?
  4. 记一次gitbook的安装
  5. 关于翻译的两篇好文章
  6. Redis——Redis_Jedis实践
  7. Experiment 3. Stack and Queue
  8. 【js的window.onload和jquery的.ready()什么区别】
  9. android 空间动态,Android手机QQ空间新版:玩转GIF动态说说
  10. IT小白如何从职场新人到行业翘楚,看一个IT高管的发展路线