Hi, I need to create a c++ program but I do not know how to begin. It needs to o
ID: 3878423 • Letter: H
Question
Hi, I need to create a c++ program but I do not know how to begin. It needs to output the information as a .xls file (excel file) not .csv with a header row with heading for each column and a data row with one set of data. Also, I need to include at least 3 columns of information with real or made up data. Most important it needs to be a .xls file not .csv since the requeriments says and I can only output a .xls file. The program needs to output the .xls file. Thanks.
#include <iostream>
#include <fstream>
using namespace std;
struct student
{
string name;
int id;
double average;
};
int main()
{
string filename = "data.xls";
//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;
}
Explanation / Answer
Use C++ xlsxwriter library for this project as below:
for (row = 0; row < 3; row++) {
worksheet_write_string(worksheet, row, col, s[row].name, NULL);
worksheet_write_number(worksheet, row, col + 1, s[row].id, NULL);
worksheet_write_number(worksheet, row, col + 1, s[row].average, NULL);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.