How do I print a vector in an Excel file?

Pages: 12
Hello,

Im using vectors in some functions and I would like to print the content of the vectors in an Excel file. Can anybody help? I have vectors of 1200 items and would like that each item be stored in a different cell in my Excel file. Is that possible?

Thank you
Save the vector items separed by a tab ( '\t' ) in an .xls file
Probably the fastest way is to just output the contents with something like std::cout <<vector[i]<<std::endl; and redirect the output to a file by calling the program with something like program>output.txt
Then you can just copy the contents to the spreadsheet and they'll be in a single column.
Or save the vector items separated by a comma, use newlines to specify a new row, and in a .csv file.
Ok, I dont get it (im not very good in C++)

I figured I had to save the vector items separated by a tab in a .xls file... but I dont know how.

What function do I use? fprintf?
How do I include all the 1200 items of my vector? I do a loop?
Where do I specify what file the data will go into?

Thanks
Seems that you have to read this: http://www.cplusplus.com/doc/tutorial/files.html

the '\t' char is a tab, to print all the vector elements make a loop like this:
1
2
3
4
ofstream myfile("file.xls");
int vsize = myvector.size();
for(int n; n<vsize; n++)
    myfile << myvector[n] << '\t';

Notice that this code will print all the items on the same row, to go to the new row use char '\r'
Last edited on
You need to store the output of your program in a file (that can be a .txt or a .csv file). This can be done using ofstream or, if you're using windows, with command line (I would recommend the last one, because its simple. When you would run a normal program like this: "myProgram.exe", type "myProgram.exe > myFile.txt" instead).

After your output is in a file, you can import it in excel. Open excel, and use Data>Import. Select your file and follow the wizard.

Hope this helps.
You may want to read this:
http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html
>Intro to loops
>>9. How to present CSV data as graphs

Ps. There are (of course) other ways to do it, but this is the easiest.
Last edited on
(That's an ofstream)
Yes, sorry, you're right. Chanced it :)
Isn't using an ofstream easier? In just two lines the file is filled:
1
2
ofstream file("myfile");
file << "something" << "something else";
Last edited on
Yes, maybe it is, at least when you know what it is and how to use it. But like OP said: he's a beginner, so i would stay away from filestreams. Aldo, you may be right. It isnt hard to understand.
Thanks for the help, it is now running properly!

Now, is there a way to create/open those files with a variable in the name? What the program does right now is that it creates "file.xls" and fills it with the required data. If I were to put that code in a loop so that it creates a new file with a different filename instead of overwriting that file.xls (or instead of adding the data at the end "ios::app"), how would I do that?

1
2
3
4
5
6
7
for(...)
{
ofstream myfile2;
myfile2.open ("file_variable.xls");
myfile2 << rec.nas << '\t' << '\r';
myfile2.close();
}


The desired output would be say file_john.xls, file_mary.xls and so on until the loop reaches its endpoint.
Do you mean something like this?

1
2
string name = "John";
file.open( ("file_"+name+".xls").c_str() );
Last edited on
Yes, would this work as is?

Let me check if this runs.
I bet this is a newbie error, but why doesn't it compile? I did put those at the beginning
1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>
#include "math.h"
#include <cstring>
#include <sstream>
#include <string>
#include "AssuranceColl.h"
#include "CalculDlg.h"
#include "Titre.h"
#include "FolderBrowse.h" 


yet string is said to be undeclared identifier
type this underneath your includes

using namespace std;

However you're going to have to change some of your other includes if you declare this namespace (i.e. <iostream> instead of <iostream.h>).

To get around that don't include the line above and use this instead: std::string name = "John";
Last edited on
To get around what? There is no problem using iostream (not .h) and using namespace std;, and there isnt much difference between using using namespace std; and std:: when you dont have several namespaces.

Besides that, I dont believe that is the problem, aldo I agree he should include <iostream>, not <iostream.h> (->older version)
To get around renaming your includes.

Some compilers are picky about how you include your includes (with or without .h).

And they sometimes change their preference when you include the using namespace std; statement.
Um... No...
The standard is all C++ headers are included without extension. You must have meant "some compilers are shitty and let you include non-standard headers without so much as a warning. And worse still, they reinterpret your inclusions based on your declarations".
The reason the're picky is becaue the libarys with .h are different then the ones without: first you got <iostream.h>, then there was made a newer version wich was called simply <iostream> so people wood be able to see wich libary is used.

I never use the .h version, so I dont know or that gives strange results when using namespace std; But when you use <iostream>, there is no need to avoid using namespace std;
Pages: 12