How to read .xls sheet from C program?

Discussion in 'C Programming' started by Rajshekhar, May 26, 2005.

  1. Rajshekhar

    Rajshekhar Guest

    hi all,
    i would like to know how to read .xls file using fopen & getc
    combination
    (usual procedure thats follwd for read file)..
    any ptrs or any function that anybody can suggest??

    Rajshekhar
    Rajshekhar, May 26, 2005
    #1
    1. Advertising

  2. Rajshekhar

    Michael Mair Guest

    Rajshekhar wrote:
    > hi all,
    > i would like to know how to read .xls file using fopen & getc
    > combination
    > (usual procedure thats follwd for read file)..
    > any ptrs or any function that anybody can suggest??


    Your request has nothing to do with the C language itself,
    thus it is not within the scope of this newsgroup. If you had
    implemented a function to parse a XSL file and it would
    not work, you could come here and ask.

    <OT>
    If you want to do it yourself, get the XML and XSL specs from
    w3.org and parse the files accordingly; I would try implementing
    it using a state machine.
    Or: You can look for libraries which can parse XML files and write
    your own XSL stuff.
    Or: You can look for libraries directly handling XSLT or XSL:FO.
    </OT>

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, May 26, 2005
    #2
    1. Advertising

  3. Rajshekhar

    Richard Bos Guest

    "Rajshekhar" <> wrote:

    > i would like to know how to read .xls file using fopen & getc
    > combination
    > (usual procedure thats follwd for read file)..
    > any ptrs or any function that anybody can suggest??


    Yes. You don't want to use getc() on Excel files at all, because they're
    binary, not text files. What you want to do is fopen(xlsfile, "rb"), and
    then use fread() on it. Actually, strictly speaking you can use getc(),
    it just isn't as clean.
    As for the file format, <http://www.wotsit.org/> is the usual
    recommendation.

    Richard
    Richard Bos, May 26, 2005
    #3
  4. Rajshekhar

    Mike Wahler Guest

    "Rajshekhar" <> wrote in message
    news:...
    > hi all,
    > i would like to know how to read .xls file using fopen & getc
    > combination


    #include <stdio.h>

    int main()
    {
    FILE *fp = fopen("file.xls", "rb");
    if(fp)
    {
    while(getc(fp) != EOF)
    ;
    if(!feof(fp))
    puts("Error reading input");
    }
    else
    puts("Cannot open input");

    return 0;
    }

    -Mike
    Mike Wahler, May 26, 2005
    #4
  5. Rajshekhar

    Michael Mair Guest

    Michael Mair wrote:
    > Rajshekhar wrote:
    >
    >> hi all,
    >> i would like to know how to read .xls file using fopen & getc
    >> combination
    >> (usual procedure thats follwd for read file)..
    >> any ptrs or any function that anybody can suggest??

    >
    >
    > Your request has nothing to do with the C language itself,
    > thus it is not within the scope of this newsgroup. If you had
    > implemented a function to parse a XSL file and it would
    > not work, you could come here and ask.
    >
    > <OT>
    > If you want to do it yourself, get the XML and XSL specs from
    > w3.org and parse the files accordingly; I would try implementing
    > it using a state machine.
    > Or: You can look for libraries which can parse XML files and write
    > your own XSL stuff.
    > Or: You can look for libraries directly handling XSLT or XSL:FO.
    > </OT>
    >
    > Cheers
    > Michael


    Gaaa... No newsgroups before caffeine (xsl<->xls).

    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, May 26, 2005
    #5
  6. Rajshekhar

    CBFalconer Guest

    Mike Wahler wrote:
    > "Rajshekhar" <> wrote in message
    >
    >> i would like to know how to read .xls file using fopen & getc
    >> combination

    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > FILE *fp = fopen("file.xls", "rb");
    > if(fp)
    > {
    > while(getc(fp) != EOF)
    > ;
    > if(!feof(fp))
    > puts("Error reading input");
    > }
    > else
    > puts("Cannot open input");
    >
    > return 0;
    > }


    Just as an illustration of what I consider clear code, consider the
    following reformatting, which preserves the input (ch) and a handy
    place (continue) to insert user code:

    #include <stdio.h>

    int main(void)
    {
    FILE *fp;
    int ch;

    if (!(fp = fopen("file.xls", "rb") puts("Cannot open input");
    else {
    while (EOF != (ch = getc(fp))) {
    continue;
    }
    if (!feof(fp)) puts("Error reading input");
    }
    return 0;
    }

    although my taste would relegate the entire while loop area to a
    separate function, as in:

    #include <stdio.h>

    int process(FILE *fp)
    {
    int ch;

    while (EOF != (ch = getc(fp))) {
    continue;
    }
    return feof(fp);
    }

    int main(void)
    {
    FILE *fp;

    if (!(fp = fopen("file.xls", "rb") puts("Cannot open input");
    else if (!process{fp) puts("Error reading input");
    return 0;
    }

    There are now three versions above. Consider how you would modify
    each one to use command line specified files, or a list of files,
    or to display some subset of the file information, assuming you had
    never seen the code before, and that you want to leave the code
    clearly readable.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, May 26, 2005
    #6
  7. Rajshekhar

    Paul Mesken Guest

    On 25 May 2005 23:24:44 -0700, "Rajshekhar" <>
    wrote:

    >hi all,
    >i would like to know how to read .xls file using fopen & getc
    >combination
    >(usual procedure thats follwd for read file)..
    >any ptrs or any function that anybody can suggest??


    This is all very off-topic, of course :)

    If you want to play around with the file itself then follow Richard's
    suggestion but if you just want to read from and write to Excel files
    from within your application (to make reports, for example) or even
    launch Excel in your application then use OLE automation, it's far
    less work.

    Here are some links :

    http://support.microsoft.com/kb/196776
    http://support.microsoft.com/default.aspx?scid=kb;en-us;216686

    It's all very much a C++ thing. There's also a mysterious C API :

    http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/office97/html/SF7D9.asp

    Since I use Borland Builder, I use VCL functions like
    GetActiveOleObject, OlePropertySet, etc. A search for these on Google
    (adding "Excel" to the search) will return Builder (and Delphi) code
    examples (that's how I learned to work with Excel from within my
    applications :)
    Paul Mesken, May 26, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Garg
    Replies:
    1
    Views:
    357
    Oliver Wong
    May 11, 2006
  2. Terry chan

    Read xls

    Terry chan, Jun 30, 2003, in forum: C Programming
    Replies:
    5
    Views:
    5,556
    Emmanuel Delahaye
    Jul 1, 2003
  3. gvs
    Replies:
    0
    Views:
    403
  4. Replies:
    1
    Views:
    1,297
    John Machin
    Jan 13, 2009
  5. crjunk

    Automatically Close PopUp Window Containing XLS Sheet

    crjunk, Jul 8, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    229
    Saravana
    Jul 10, 2003
Loading...

Share This Page