Opening a .csv file using microsoft excel

Hi guys, I have written a program that writes a .csv file readable in Microsoft excel. Is there a way I can launch the file with Microsoft excel once I compile and run the program? The codes I have so far is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmath>

using namespace std;


int main()
{
double T, a, b;

a=5.0;
b=2;

ofstream outputfile("Table.csv");
outputfile<<"a"<<","<<"T"<<endl;
           for(int n=0; n<10; n++){  
           double T, a, b;
           T=a+b;
           outputfile<<a<<","<<T<<endl;
           }

// If I do this:
std::string excel_file = "Table.csv";
outputfile.open(excel_file.c_str(), ios::in);
 
         if (outputfile.is_open()) {
cout<<"File opened"<<endl;
}
         else {
         cout << "Failed To Open File" << endl;
     }
outputfile.close();
system ("PAUSE");
return 0;

// My file is written but does not launch. Any help?
Last edited on
You need to launch the Excel executable passing to it your file you have just written. There are several ways to do that, system() is the easiest one.

Hi kooth! i tried using system("Table.csv"); but does not do anything. is there some thing i am doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmath>

using namespace std;


int main()
{

double T, a, b;

a=5.0;
b=2;
ofstream outputfile("Table.csv");
outputfile<<"a"<<","<<"T"<<endl;
            for(int n=0; n<10; n++){  
            
            T=a+b;
            outputfile<<a<<","<<T<<endl;
            }

  
    system("Table.csv");
    return 0;
}
Yes, you need to launch "excel.exe" and pass "Table.csv" to it:

1
2
3
4
5
6
7
8
9
10
11

/*    Add this above your "using namespace std;" statement:    */

#include <string>

/*    Add this in your main() routine before calling system():    */

string Command = "excel.exe Table.cvs";

int rc = system( Command );


NOTE: You may need to prepend the full path to excel.exe if it isn't in your PATH.
You should read this forum : http://www.cplusplus.com/forum/beginner/1988/
to know why system() is bad.

For excel, you have so many methods to open files :

1. execute "%PROGRAMFILES%\Microsoft Office\OfficeXX\excel.exe file" where XX is the Office version (12 is Office 2007) you just have to test for the existence of excel before.

2. use the shell (not system, but Windows' shell methods) to open your file, but if .csv files are not associated with Excel, you take the risk to launch a program you don't know.

3. Use the Excel OLE interface (see MSDN if you dunno how it can be achieved)
It is appropriate to use system() to start excel.

However, you'll do better to use the ShellExecute() function:

1
2
#include <windows.h>
#include <shellapi.h> 
1
2
if ((int)ShellExecute( NULL, "open", "Table.csv", NULL, NULL, SW_SHOWNORMAL ) <= 32)
  fooey();

This will start your spreadsheet application (Excel or whatever else the user has set for this file type).

Google "msdn shellexecute" for documentation.

@kooth
The system() function doesn't take strings. int rc = system( Command.c_str() );

Hope this helps.
You're quite right Duoas: I was in a hurry!
Thanks guys, i have used system("Table.csv"); I also noticed that I have to use outputfile.close() before system("Table.csv");
Wow, I totally looked right past that! Good catch, Lane!
Topic archived. No new replies allowed.