linux下读取excel文件

3 篇文章 0 订阅

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

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Qt是一款跨平台的图形用户界面开发框架,可以在Windows、Linux和MacOS等多种操作系统中运行。在使用Qt进行Linux开发时,如果需要读取Excel文件中的数据,可以使用一些开源的库来实现。 最常用的库是libxls,它支持读取Microsoft Excel文件的内容,并提供了一个简单的API来实现这个过程。使用该库需要在Linux系统中安装libxls-dev包,并在Qt项目的.pro文件中加入libxls的链接库。 Qt开发人员在使用libxls库时,需要先创建一个xlsWorkBook对象,然后通过xls_open()函数来打开Excel文件。在使用xls_read()函数读取Excel文件中的数据时,需要指定数据所在的工作表、行数和列数。读取每个单元格数据后,可以通过XLS_WORD(xls_cell*)和XLS_RK(xls_cell*)等函数来获取具体的数值。最后,需要通过xls_close()函数来关闭Excel文件。 除了libxls外,另外一个流行的库是QXlsx,它是一个纯Qt实现的Excel读写库,可以在Windows、Linux和MacOS中使用。使用QXlsx时,需要在Qt项目中加入QXlsx.pri文件,并在代码中创建QXlsx::Document对象来读取Excel文件中的数据。读取数据时,可以通过sheet()、cellAt()和read()等函数来指定具体的工作表、单元格和数据格式。 综上所述,Qt可以通过使用开源库来实现Linux读取Excel,并提供了一些简单的API来操作Excel文件中的数据。此外,Qt还可通过QXlsx等纯Qt实现的库来读写Excel文件,方便快捷。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值