Connecting Tech Pros Worldwide Help | Site Map

read data from excel using c

Newbie
 
Join Date: Mar 2006
Posts: 3
#1: Mar 16 '06
Hi,

I am trying to read data from an excel file, using C. I have been advised that I must use arrays, but I am completely stuck onto how to do this.

Any help would be very much appreciated!

Thanks,

S
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,161
#2: Mar 16 '06

re: read data from excel using c


What is the basic approach you are using?

Do you have the format for an excel file?
Newbie
 
Join Date: Mar 2006
Posts: 3
#3: Mar 16 '06

re: read data from excel using c


Ummm,

I just need to extract data from an excel file from random. There will be a table with n columns and n rows, but i need to extract the data within this one table somehow, using arrays.

I've been lookin around the net, but havent got anywhere :(
Newbie
 
Join Date: Mar 2006
Posts: 8
#4: Mar 16 '06

re: read data from excel using c


cs21sd,

Excel tables can be redeclared as ODBC data sources in Windows 2000 and newer. Perhaps you could connect your excel sheet as ODBC source and then access the data via ODBC headers (expect that all newer/commercial compilers should have them, Borland Builder and MSVC do).

You can't access excel sheets directly (read the file itself), it's MS Office format which is not open. Afaik MS Office documents consit of objects/modules to support OLE and embed other office documents. So it's pretty unlikely you will access MS documents with yur own programm.

One more option (and the most simple one): write your application in VBA (visual basic for applications). This VB Version is embedded in MS Office and supports VERY many different options to manipulate Office Documents programmatically. I guess, this is the most adviceable choice.
Newbie
 
Join Date: Mar 2006
Posts: 8
#5: Mar 16 '06

re: read data from excel using c


Oh, uh,

Just a thought: if your excel sheet is defined as ODBC data source you can access it for read and write with virtually any programmatic option. I would write into and read from with PHP for example.

One more thought: if you maintain the tables not in MS-only XLS format but use CSV sheets, you can access them with everything you want, since they are text only.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,161
#6: Mar 16 '06

re: read data from excel using c


Quote:

Originally Posted by BugHunter

One more thought: if you maintain the tables not in MS-only XLS format but use CSV sheets, you can access them with everything you want, since they are text only.

Going on from this since a CSV value file is text it can easily be read into a program.

You have to read a table, the reason that you are being advised to use arrays is that a 2 dimensional array is very like a table in that you access it using 2 indexes which, in this case can be thought of as your row and column.

Do you understand what an array is?

You could easily read the file into an 2 dimensional array of strings. Of course in C a string is a 1 dimensional array of characters so what you need is a 3 dimensional array of characters delared something like

Expand|Select|Wrap|Line Numbers
  1. #define MAX_ROWS       10
  2. #define MAX_COLUMNS    10
  3. #define MAX_CELL_TEXT  20
  4.  
  5. char Table[MAX_COLUMNS][MAX_ROWS][MAX_CELL_TEXT+1];
  6.  
Note the plus 1 is to take account of the zero terminator that c uses in strings.

Then all you have to do is read the lines of the CSV file, parse each line into it's separate cells and plonk the contents of each cell into the correctentry in the array.
Newbie
 
Join Date: Mar 2006
Posts: 3
#7: Mar 16 '06

re: read data from excel using c


Thanks alot guys, I will try these now.

:)
Reply