How to output to an excel file and how to read from it??

i have done the writing 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.

.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
"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.

that was about the extension part..thanks..
now what about reading and writing in a excel file(.cvs)

> now what about reading and writing in a excel file(.cvs)
The same as you would for any other text file.

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 to do that...

we can do that by fout<<"\t" while writing with an object ofstream fout("filename.cvs")

Yes, fout << field << "," << anotherField << endl; So rather than using "\t" , use "," . It's that simple.

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.

std::string line;
std::ifstream in("filename here");
while( getline(line) )
    vector<string> cells;
    stringstream str(line);
    std::string cel;
    while( str >> cel)
    // now do something with the individual cells

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, You have to study object oriented functions like olepropertyget, variant, etc. In short, its hard but worthwhile.

>>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.

Not to mention giving MS a shed-load of money :)

Yes, fout << field << "," << [B]anotherField [/B]<< endl; So rather than using "\t" , use "," . It's that simple.

will "," advance the entry of anotherField to the next cell???

ok fine..i tried it and it is working thanks for the help...i am successfully able to output to the file but there is still problem while reading it....
here is my code....

using namespace std;
int main()
    ofstream fout("excel.csv");     //CSV== comma separated  value files
    int a[20],b[20];
    for(int i=0 ; i<20 ; i++)
    ifstream fin("excel.csv");
    for(int i=0 ; i<20 ; i++)
    return 0;

the second for loop is for taking input from the file....but outputting the data read from the file at the console it gives...


while the data in the excel file is...

38 41 45 48 51 54 58 61 64 68 71
74 77 81 84 87 90 94 97 0

You can't read it like that.

then how?? kindly elaborate. I really want to learn this.

I already told you that you cannot use the traditional c++ code to read .xls file.

For Borland c++ builder, reading xls file should be like this,
//declaration of variables of type variant
Variant XL,v0,v1,vcell;

//a string where you will temporarily put the content of a single Cell
AnsiString tmp;

//create an object which is an excel application and store it to XL

//Set the Excel Application as invisible once you have opened it

//Get the workbooks while has a path stored in “file” variable and open it.

//Get the Sheet which has a title “Sheet1″

//Get the Cells of that particular Sheet.

AND SO ON........

I already gave you an example (for writing excel file) in my previous post. Now, this one is for reading;

Borland offers a 30-day trial edition of Borland C++ Builder 6.0 (BCB), BCB can handle MS office based applications like excel, and word.

Yep. I used BCB in my previous projects. Though I only explored its capability on excel, i know it can also handle word processing since it is discussed on its help file.

This question has already been answered. Start a new discussion instead.