简体   繁体   English

从C读取Excel文件

[英]Reading an excel file from C

I used C program to write into an excel file and it worked for both (.xls & .xlsx) 我用C程序写入一个excel文件,它对(.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. 现在,我正在尝试使用C程序读取从计算机(不是从C)创建的excel文件,但是我得到的只是乱码...对于.xls and .xlsx都没有意义,所以我决定使用.csv格式有效,但只能读取Excel工作表的第一行。 Please how do I go about this? 请我怎么做?

I have resolved the issue, the .csv format works fine the problem was with my code. 我已经解决了这个问题, .csv格式可以很好地解决我的代码存在的问题。 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 感谢您的贡献。下次,我将确保更好地查看我的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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