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:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmath>
usingnamespace 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?
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.
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)