How to read XLS files without EXCEL®

Here is a free DLL you can use to read XLS files.


Features:

Updates

Example of use:

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

#include "xlsread.h"

                        /*====== callback function for processing cells ====*/
int put(int row, int col, char *str, double val)
{
    printf("     row %d - col %d : ", row, col) ;       /* print all cells  */ 
    if (str)
        printf("[%d] \"%.40s\"\n", strlen(str), str) ;
    else
        printf("%g\n", val) ;
    return 0 ;
}

                        /*================================ main program ====*/
int main( int argc, char *argv[ ])
{

    char *filein[] = { "file1.xls", "file2.xls", "" } ;     /* file list    */

    char *err_msg ;
    WORKBOOK *wkbook ;
    int i = 0 ;

    for (i = 0 ; strlen(filein[i]) ; i++)                   /* scan files   */
    {
        printf("\n==== %s\n", filein[i]) ;
        wkbook = XLS_WorkbookNew(filein[i], &err_msg) ;     /* get workbook */
        if (wkbook)
        {
            unsigned long j = 0;
            for (;;)                                        /* scan sheets  */
            {
                SHEET *sheet = XLS_SheetNew(wkbook, &j, &err_msg) ;
                if (sheet)
                {
                    printf("   Sheet %s", sheet->name) ;    /* show name &  */
                    printf(" - rows %d to %d - cols %d to %d\n",    /* size */
                            sheet->rowFirst, sheet->rowLast, 
                            sheet->colFirst, sheet->colLast) ;

                    XLS_RowsScan(sheet, 0, put) ;           /* scan sheet   */
                    XLS_SheetDelete(&sheet) ;
                }
                else
                    break ;
            }
            XLS_WorkbookDelete(&wkbook) ;
        }
        else
            printf(">>>> %s\n", err_msg) ;
    }
}

Explanations:


For each file, one must create a workbook and then, it isq possible to scan sheets.

The main sheet properties are name, rowFirst, rowLast, colFirst and colLast as used above.

The dive in a sheet, call XLS_RowsScan().

This function requires a callback, that is a function which will be called for each cell.

It is put() in the above example.


Prototype: int put(int row, int col, char *str, double val)


Input parameters are:


Return value:


Download XLSREAD


The zip file contains xlsread.h, .lib and .dll.

Build tool: MS VC++ 6


Contact:
Henri Serindat
hserindat@numericable.fr