简体   繁体   中英

Reading an excel file from C

I used C program to write into an excel file and it worked for both (.xls & .xlsx)

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

int main(int argc, char const *argv[])
{
    FILE * fpointer;
    fpointer = fopen ("cfile.xls","w");

    fprintf(fpointer, "Name \t MIT 801\t MIT 802\t MIT 803\t MIT 805\t MIT 821 \n Haphyz\t 90\t 89\t 99\t 95\t 96\n");

    fclose(fpointer);


}

I also tried to read from it and it also worked:

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

int main(int argc, char const *argv[])
{
    FILE * fpointer;

    fpointer = fopen ("cfile.txt","r");

    char Creader[200];
    while (!feof(fpointer)){
        fgets (Creader,200,fpointer);
        puts (Creader);

    }
        fclose(fpointer);

    return 0;
}

Now I'm trying to use C program to read an excel file that was created from the computer (not from C) but all I get is gibberish...nothing meaningful for both .xls and .xlsx then I decided to use the .csv format which worked but was only able to read only the first line of the excel sheet. Please how do I go about this?

I have resolved the issue, the .csv format works fine the problem was with my code. I closed the file inside the loop so the program stops reading after the first line .

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


int main(int argc, char const *argv[])
{

    FILE * fpointer;    
    fpointer = fopen("Book1.csv","r");
    char spreadSheet[200];

    while(!feof(fpointer)){
    fgets(spreadSheet,200,fpointer);
    fprintf(stderr, "%s\n", spreadSheet); 

    fclose(fpointer);

    }


    return 0;
}

There is no way this would return an error message ,so it printed out what I instructed it to .

Thanks for your contributions.I'll make sure to look at my code better next time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM