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.

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

ok
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 one...how 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)
    {
        cells.push_back(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, http://www.jcmiras.net/jcm/item/45/. 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.

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

#include<iostream>
#include<fstream>
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++)
    {
            srand(i);
            a[i]=rand()%100;
            fout<<a[i]<<",";
            if(i==10)
            fout<<"\n";
   }
    
    ifstream fin("excel.csv");
    for(int i=0 ; i<20 ; i++)
    {
            fin>>b[i];
            cout<<b[i]<<"\n";      
    }    
    fout.close();
    system(("excel.csv"));    
    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...

1995017202
1995290759
-2
1995383879
1995383116
1088
1096
2896288
0
4198400
0
4198400
0
2293004
28
2292980
0
4198400
1995374640
1996608169

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
XL=Variant::CreateObject(”excel.application”);

//Set the Excel Application as invisible once you have opened it
XL.OlePropertySet(”Visible”,false);

//Get the workbooks while has a path stored in “file” variable and open it.
XL.OlePropertyGet(”Workbooks”).OleProcedure(”Open”,file);

//Get the Sheet which has a title “Sheet1″
v0=XL.OlePropertyGet(”Sheets”,”Sheet1″);

//Get the Cells of that particular Sheet.
v1=v0.OlePropertyGet(”Cells”);

AND SO ON........

I already gave you an example (for writing excel file) in my previous post. Now, this one is for reading; http://www.jcmiras.net/jcm/item/4/

Borland offers a 30-day trial edition of Borland C++ Builder 6.0 (BCB), http://www.codegear.com/downloads/free/cppbuilder. 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.