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

Recommended Answers

All 3 Replies

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.