How to read .xls sheet from C program?

R

Rajshekhar

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
 
M

Michael Mair

Rajshekhar said:
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
 
R

Richard Bos

Rajshekhar said:
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
 
M

Mike Wahler

Rajshekhar said:
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
 
M

Michael Mair

Michael said:
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).
 
C

CBFalconer

Mike said:
#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.
 
P

Paul Mesken

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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top