How to read data from an excel table in C?

0

I know how to read data and write to .txt files with C code through fscanf(), fprintf() functions and so on. But now I would like to know if there are C functions or libraries that allow me to treat data from a excel table, where I can access columns and rows, if anyone knows, could you let me know?

    
asked by anonymous 28.03.2018 / 14:37

1 answer

2

There is the LibXL library, but it is paid for.

Example: Generate a spreadsheet from scratch

#include "libxl.h"


int main()
{
    BookHandle book = xlCreateBook(); // xlCreateXMLBook()
    if(book) 
    {
        SheetHandle sheet = xlBookAddSheet(book, L"Sheet1");
        if(sheet) 
        {
            xlSheetWriteStr(sheet, 2, 1, L"Hello, World !", NULL);
            xlSheetWriteNum(sheet, 3, 1, 1000, NULL);
        }
        xlBookSave(book, L"example.xls");
        xlBookRelease(book);
    }
    return 0;
}

A free open source alternative is the XLSX I / O library.

Example

//open .xlsx file for reading
xlsxioreader xlsxioread;
if ((xlsxioread = xlsxioread_open(filename)) == NULL) {
  fprintf(stderr, "Error opening .xlsx file\n");
  return 1;
}

//list available sheets
xlsxioreadersheetlist sheetlist;
const char* sheetname;
printf("Available sheets:\n");
if ((sheetlist = xlsxioread_sheetlist_open(xlsxioread)) != NULL) {
  while ((sheetname = xlsxioread_sheetlist_next(sheetlist)) != NULL) {
    printf(" - %s\n", sheetname);
  }
  xlsxioread_sheetlist_close(sheetlist);
}

//read values from first sheet
char* value;
printf("Contents of first sheet:\n");
xlsxioreadersheet sheet = xlsxioread_sheet_open(xlsxioread, NULL, XLSXIOREAD_SKIP_EMPTY_ROWS);
while (xlsxioread_sheet_next_row(sheet)) {
  while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
    printf("%s\t", value);
    free(value);
  }
  printf("\n");
}
xlsxioread_sheet_close(sheet);

//clean up
xlsxioread_close(xlsxioread);
    
28.03.2018 / 16:11