Write a simple compilable program that creates and outputs to an Excel-compatibl
ID: 3877755 • Letter: W
Question
Write a simple compilable program that creates and outputs to an Excel-compatible XLS file these two rows: (1) a header row with headings for each column and (2) a data row with one set of data. You may have addition data rows if you wish. Include at least 3 columns of information of any kind you like, with real or made-up data. For example, a student record, a stock quote, a weather forecast, etc. Be sure to include code blocks with your identifying comments and cout statements. Include all the libraries and correct syntax so that the program can be saved, compiled, and run
Explanation / Answer
The following code generates a comma-separate values file (CSV) file that can open in Excel. The 1st line is column headings followed by 3 rows of data. The program generates 3 student records in a file named data.csv which can be opened in excel
#include <iostream>
#include <fstream>
using namespace std;
struct student
{
string name;
int id;
double average;
};
int main()
{
string filename = "data.csv";
//create 3 students using the structure array
student s[3] = { {"John", 111, 75.5},
{"Alice", 222, 90.0},
{"Bob", 333, 87.6}};
ofstream outFile(filename.c_str());
outFile << "Name,ID,Average" << endl; //column headings
//print the comma separated data rows
for(int i = 0; i < 3; i++)
{
outFile << s[i].name << "," << s[i].id << "," << s[i].average << endl;
}
outFile.close();
cout << "Please open " << filename << " in Excel" << endl;
}
generated data.csv (can be opened in excel)
====================
Name,ID,Average
John,111,75.5
Alice,222,90
Bob,333,87.6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.