0

does anyone know how I can write to a xls that puts each number in different cells? for example,

vector<int> nums;
nums.push_back(1);
nums.push_back(2);
nums.push_back(3);

ofstream myfile;
myfile.open("C:/example.xls")
myfile << nums[1] << "  " <<  nums[2] << "  "  nums[3] << endl;

myfile.close()

how do i put three numbers in different cells, but not all in one cell?

thanx

3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by Salem
0

You cannot write directly to an *.xls file and expect Excel to read it because *.xls files are not plain text files -- they contain a lot of formatting information that is readable only by Excel program.

The easiest way to do it is to creates *.csv files which Excel can import. Fieids (cells) are commma or sometimes tab separated, with CR/LF between each row. Using your example you might write it like this: Text should be surrounded with quotes (see first example below)

myfile << "\"Hello World\"," <<  nums[1] << "," <<  nums[2] << ","  nums[3] << "\n";

or
myfile << nums[1] << "\t" <<  nums[2] << "\t"  nums[3] << "\n";
0

hi, thanx! putting in the comma and quotes are certainly not the most pleasant things to do, but it does the trick!!

0

> putting in the comma and quotes are certainly not the most pleasant things to do
Wrap it up in a class :)
Probably not a bad idea if you need to do this a lot.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.