c - 使用libxls在C中读取excel文件

标签 c xls

我正在尝试创建一个使用 libxls 读取 excel 文件的应用程序。 我已经下载了 libxls 但我不知道如何使用它。

有人可以告诉我一个从 XLS 文件读取的最小程序

最佳答案

如果您的意思是 libxls :

源存档中有测试,可以用作示例代码。 这是libxls-1.4.0/test/test2.c的内容:

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of libxls -- A multiplatform, C/C++ library
 * for parsing Excel(TM) files.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2004 Komarov Valery
 * Copyright 2006 Christophe Leitienne
 * Copyright 2008-2012 David Hoerl
 *
 */

// THIS FILE LETS YOU QUICKLY FEED A .xls FILE FOR PARSING

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "libxls/xls.h"

int main(int argc, char *argv[])
{
    xlsWorkBook* pWB;
    xlsWorkSheet* pWS;
    unsigned int i;

    if(argc != 2) {
        printf("Need file arg\n");
        exit(0);
    }

    struct st_row_data* row;
    WORD t,tt;
    pWB=xls_open(argv[1],"UTF-8");

    if (pWB!=NULL)
    {
        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);

printf("pWS->rows.lastrow %d\n", pWS->rows.lastrow);
        for (t=0;t<=pWS->rows.lastrow;t++)
        {
printf("DO IT");
            row=&pWS->rows.row[t];
            xls_showROW(row);
            for (tt=0;tt<=pWS->rows.lastcol;tt++)
            {
                xls_showCell(&row->cells.cell[tt]);
            }
        }
        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);

        xls_showBookInfo(pWB);
    } else {
        printf("pWB == NULL\n");
    }
    return 0;
}

如果您的意思是 libxl :

下面您将看到一个用 C 语言编写的从 http://www.libxl.com/ 读取数据的示例。 。 此外,他们还提供documentationmore examples在他们的主页上。

BookHandle book = xlCreateBook();
if(book) 
{
    if(xlBookLoad(book, L"example.xls")) 
    {
        SheetHandle sheet = xlBookGetSheet(book, 0);
        if(sheet)
        {
            double d;
            const wchar_t* s = xlSheetReadStr(sheet, 2, 1, NULL);

            if(s) wprintf(L"%s\n", s);

            d = xlSheetReadNum(sheet, 3, 1, NULL);
            printf("%g\n", d);
        }
    }        
    xlBookRelease(book);
}

关于c - 使用libxls在C中读取excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45159281/

相关文章:

c++ - 除非存在特定的 printf 语句,否则 fwrite 不会将缓冲区写入标准输出

c - 对于矩阵初始化,openmp 比串行慢

php - 将数据库导出到 xls,价格格式为美元符号和逗号

ruby-on-rails - 使用 Ruby Spreadsheet gem,我可以将单元格格式设置为货币吗?

c# - VS 代码 C# - System.NotSupportedException : No data is available for encoding 1252

c - 如何在 C 中按字典顺序对二维字符数组进行排序?

c - 如果输入错误,则双重询问输入

c++ - 将否定的 double 转换为 int

django 使用链接下载 csv 文件

php - 上传并插入xls文件到mysql?