The C program in Ubuntu uses the libxls-1.2.1 library to read the content of the Excel table

Source: Internet
Author: User

Previously, the log said that the xlslib database can only write Excel tables, but it cannot be read, but reading is a frequently needed operation ,,,

So with today's libxls, this is specifically used to read Excel tables.

Source code can be downloaded here: http://sourceforge.net/projects/libxls/

Download, decompress, and compile and install

./Configre

Make

Sudo make install

Because the directory of the header file and the library file is not in the correct position, you need to manually copy them to the system path.

Sudo CP-r-V/usr/local/libxls/include/libxls // usr/include
Sudo CP-r-V/usr/local/libxls/lib // USR

Usage:

Including header files: # include <libxls/XLS. h>

Related functions and types used:

Xlsworkbook * PWB;

Xlsworksheet * PWS;
Struct st_row_data * row;

Open an Excel table

PWB = xls_open (argv [1], "UTF-8 ");

Read the 1st page of the table

PWS = xls_getworksheet (PWB, 0 );
Xls_parseworksheet (PWS );

Get the content of a row (r)

Row = & PWS-> rows. Row [R];

Get the text of a column

Row-> cells. Cell [3]. Str

Close

Xls_close_ws (PWS );
Xls_close_wb (PWB );

Compile command: gcc-O readxls. C-lxlsreader

A simple example of readxls. C:

#include <stdio.h>#include <stdlib.h>#include <libxls/xls.h>#include <unistd.h>/////////////////////////////////////////////////void strrpl(char *result, char *str, char *old_str, char *new_str);/////////////////////////////////////////////////int main(int argc, char **argv) {    xlsWorkBook *pWb;    xlsWorkSheet *pWs;    struct st_row_data *row;    int r,c;    char buf[512], result[512];    if (argc < 2) {        sprintf(stderr, "please input the xml file.");        return EXIT_FAILURE;    }    // open workbook, choose standard coversion    pWb = xls_open(argv[1], "UTF-8");    if (NULL == pWb) {        fprintf(stderr, "File not found!\n");        return EXIT_FAILURE;    }    // open and parse the first sheet    pWs = xls_getWorkSheet(pWb, 0);    xls_parseWorkSheet(pWs);        // process all rows of the first sheet    for (r=0; r<=pWs->rows.lastrow; r++) {        row = &pWs->rows.row[r];                for (c=0; c<=pWs->rows.lastcol; c++) {            if (row->cells.cell[c].str != NULL) {                printf("%\t",                        row->cells.cell[c].str);            }        }        printf("\n");    }        // close workSheet    xls_close_WS(pWs);    // close workbook    xls_close_WB(pWb);    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.