Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2721
  • Last Modified:

How to read a .xls file?

I need to open an excel file and then read each row of the file, how do I do this?
0
prasen120998
Asked:
prasen120998
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
Need to be in pure C?
There are many examples in Visual C++:
http://www.codeproject.com/database/cspreadsheet.asp
http://www.codeproject.com/cpp/miniexcel.asp
0
 
Jaime OlivaresSoftware ArchitectCommented:
More:
http://www.codeproject.com/database/excel_odbc.asp

Also you can save a spreadsheet in Comma-Separated-Values format (.csv) and read as a simple text easily.
0
 
grg99Commented:
Above you'll see several ways to do it, mostly very cumbersome.

It's not easy to directly read the file, as it's ina rather convoluted BIFF format.  Plus there are about 8 different Excel file formats, it seems each version of Excel has changed things around a bit.

If you gotta do it, get the BIFF document from www.msdn.com and read up on the various file formats.  Plan to spend a good week writing code.

Alternatives include:  send DDE commands to a copy of Excel, have Excel write out a comma-delimited text file, or forget the whole thing.

0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ladweinCommented:
The best possibility (as jaime olivares already said) is to save the Excel file as .csv (with ';'  or 'tab' as field separator.)
Then you could simply read the file character by character to the end of each line. (line end has ascii code 10 or 13 - whatever excel uses; tab has ascii code 9)

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

main()
{
   FILE *in;
   in = fopen ("yourfile.csv","r");
   while ((c=getc(in))!=EOF)  //read to EndOfFile
   {
      i=c;      //get ASCII value of current letter
      if (i!=10)      //read to end of line
      {
         if (i!=9) //read to next tab
         {
            ...
         }
         else
         {
            ...
         }
      }
      else //end of line reached
      {
         ...
      }
   }
   fclose(in);
}

Maybe you could tell what you want to do with the Excel File (Copy its content to the console, copy to another file, ...)

Michael
0
 
prasen120998Author Commented:
The excel file would be provided by another system, & my program needs to read that file and create an output
0
 
moduloCommented:
PAQed with points refunded (50)

modulo
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now