How to read/write to an MS-Excel file??

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

How to read/write to an MS-Excel file??

 
0
  #1
Dec 29th, 2007
How to output to an excel file and how to read from it??

i have done the writing part...to use "\t" for moving to next cell in same row and "\n" to move to next row...

and is there any special format to save that excel file..because when i save it with the extension .xls then it says every time i try to open it...
the file is not known with this extension check whether it is corrupted.
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to read/write to an MS-Excel file??

 
0
  #2
Dec 29th, 2007
.xls files are a proprietary (that's secret) format of microsoft.

For something portable, the best is "Comma Separated Values", or CSV.

So you would have say
1,2,3
hello,world
"and now, the news","for today"

If you save this text file with the extension .csv, then excel should be able to read it just fine.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: How to read/write to an MS-Excel file??

 
0
  #3
Dec 29th, 2007
ok
that was about the extension part..thanks..
now what about reading and writing in a excel file(.cvs)
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to read/write to an MS-Excel file??

 
0
  #4
Dec 29th, 2007
> now what about reading and writing in a excel file(.cvs)
The same as you would for any other text file.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: How to read/write to an MS-Excel file??

 
0
  #5
Dec 29th, 2007
no i m not able to read from it with an ifstream fin("filename.cvs") object...

i told you how i m doing the writing part....
as excel file hav entries in cells so we need to advance the file pointer to next cell after reading one...how to do that...

we can do that by fout<<"\t" while writing with an object ofstream fout("filename.cvs")
Life is about being happy...
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How to read/write to an MS-Excel file??

 
0
  #6
Dec 29th, 2007
Yes,
fout << field << "," << anotherField << endl;
So rather than using "\t" , use "," . It's that simple.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to read/write to an MS-Excel file??

 
0
  #7
Dec 29th, 2007
post your code. The cells should be separated by comms, spaces or tabs. Each row separated with '\n'. I would use a combination of stringstream and getline() to read the file and separate its cells.

Something like this which assumes cells are separated with white space. A little different when separated by commas.
  1. std::string line;
  2. std::ifstream in("filename here");
  3. while( getline(line) )
  4. {
  5. vector<string> cells;
  6. stringstream str(line);
  7. std::string cel;
  8. while( str >> cel)
  9. {
  10. cells.push_back(cel);
  11. }
  12. // now do something with the individual cells
  13. }
Last edited by Ancient Dragon; Dec 29th, 2007 at 12:48 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: jacki is an unknown quantity at this point 
Solved Threads: 2
jacki jacki is offline Offline
Newbie Poster

Re: How to read/write to an MS-Excel file??

 
0
  #8
Jan 5th, 2008
You cannot read and write .xls file using file manipulation functions in c/c++ because it is pre-formatted by excel. However, you can do such on a .csv file since it is just a in a "plain text" format like .txt. You can verify this by comparing the content of a .xls and .csv file using notepad. I`m telling you, you cannot read the content of .xls file.

However, if you are using c++ object oriented builder like borland c++ or visual c++, you can do what you want to do in the file. But you have to study how it work because the functions is very much different to the traditional c/c++. Take a look at this one for example, http://www.jcmiras.net/jcm/item/45/. You have to study object oriented functions like olepropertyget, variant, etc. In short, its hard but worthwhile.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to read/write to an MS-Excel file??

 
0
  #9
Jan 5th, 2008
>>I`m telling you, you cannot read the content of .xls file.
It could be done but with great difficulty and probably very risky and for those reasons I wouldn't do it. The Microsoft Office SDK would be the best and safest solution but it too requires quite a bit of programming experience.
Last edited by Ancient Dragon; Jan 5th, 2008 at 11:57 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 109
Reputation: rajatC is an unknown quantity at this point 
Solved Threads: 7
rajatC's Avatar
rajatC rajatC is offline Offline
Junior Poster

Re: How to read/write to an MS-Excel file??

 
0
  #10
Jan 6th, 2008
Originally Posted by Salem View Post
Yes,
fout << field << "," << anotherField << endl;
So rather than using "\t" , use "," . It's that simple.
will "," advance the entry of anotherField to the next cell???
Life is about being happy...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC