@@ -10,36 +10,53 @@ using namespace OpenXLSX;
1010int main ()
1111{
1212 cout << " ********************************************************************************\n " ;
13- cout << " DEMO PROGRAM #07: Row Handling - Implicit Conversion\n " ;
13+ cout << " DEMO PROGRAM #07: Row Data - Implicit Conversion\n " ;
1414 cout << " ********************************************************************************\n " ;
1515
16+
17+ // The previous example showed, among other things, how a std::vector of XLCellValue objects
18+ // could be assigned to to an XLRow object, to populate the data to the cells in that row.
19+ // In fact, this can be done with any container supporting bi-directional iterators, holding
20+ // any data type that is convertible to a valid cell value. This is illustrated in this example.
21+
22+ // First, create a new document and access the sheet named 'Sheet1'.
1623 cout << " \n Generating spreadsheet ..." << endl;
1724 XLDocument doc;
1825 doc.create (" ./Demo07.xlsx" );
1926 auto wks = doc.workbook ().worksheet (" Sheet1" );
2027
28+ // A std::vector holding values that are convertible to a cell value can be assigned to an XLRow
29+ // object, using the 'values()' method. For example ints, doubles, bools and std::strings.
2130 wks.row (1 ).values () = std::vector<int > { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
2231 wks.row (2 ).values () = std::vector<double > { 1.1 , 2.2 , 3.3 , 4.4 , 5.5 , 6.6 , 7.7 , 8.8 };
2332 wks.row (3 ).values () = std::vector<bool > { true , false , true , false , true , false , true , false };
2433 wks.row (4 ).values () = std::vector<std::string> { " A" , " B" , " C" , " D" , " E" , " F" , " G" , " H" };
2534
35+ // Save the sheet...
2636 cout << " Saving spreadsheet ..." << endl;
2737 doc.save ();
2838 doc.close ();
2939
40+ // ...and reopen it (just to make sure that it is a valid .xlsx file)
3041 cout << " Re-opening spreadsheet ..." << endl << endl;
3142 doc.open (" ./Demo07.xlsx" );
3243 wks = doc.workbook ().worksheet (" Sheet1" );
3344
45+ // The '.values()' method returns a proxy object that can be converted to any container supporting
46+ // bi-directional iterators. The following three blocks shows how the row data can be converted to
47+ // std::vector, std::deque, and std::list. In principle, this will also work with non-stl containers,
48+ // e.g. containers in the Qt framework. This has not been tested, though.
49+
50+ // Conversion to std::vector<XLCellValue>
3451 cout << " Conversion to std::vector<XLCellValue> ..." << endl;
3552 for (auto & row : wks.rows ()) {
36- // for (auto& value : std::vector<XLCellValue>(row.values())) {
37- for (auto & value : row.values <std::vector<XLCellValue>>()) {
53+ for (auto & value : std::vector<XLCellValue>(row.values ())) {
3854 cout << value << " " ;
3955 }
4056 cout << endl;
4157 }
4258
59+ // Conversion to std::deque<XLCellValue>
4360 cout << endl << " Conversion to std::deque<XLCellValue> ..." << endl;
4461 for (auto & row : wks.rows ()) {
4562 for (auto & value : std::deque<XLCellValue>(row.values ())) {
@@ -48,6 +65,7 @@ int main()
4865 cout << endl;
4966 }
5067
68+ // Conversion to std::list<XLCellValue>
5169 cout << endl << " Conversion to std::list<XLCellValue> ..." << endl;
5270 for (auto & row : wks.rows ()) {
5371 for (auto & value : std::list<XLCellValue>(row.values ())) {
@@ -56,6 +74,11 @@ int main()
5674 cout << endl;
5775 }
5876
77+ // In addition to supporting any bi-directional container types, the cell values can also be converted to
78+ // compatible plain data types instead of XLCellValue objects, e.g. ints, doubles, bools and std::string.
79+ // This is illustrated in the following three blocks:
80+
81+ // Conversion to std::vector<[int, double, bool, std::string]>
5982 cout << endl << " Conversion to std::vector<[int, double, bool, std::string]> ..." << endl;
6083 for (auto & value : std::vector<int >(wks.row (1 ).values ())) cout << value << " " ;
6184 cout << endl;
@@ -66,9 +89,9 @@ int main()
6689 for (auto & value : std::vector<std::string>(wks.row (4 ).values ())) cout << value << " " ;
6790 cout << endl;
6891
92+ // Conversion to std::deque<[int, double, bool, std::string]>
6993 cout << endl << " Conversion to std::deque<[int, double, bool, std::string]> ..." << endl;
70- // for (auto& value : std::deque<int>(wks.row(1).values())) cout << value << " ";
71- for (auto & value : wks.row (1 ).values <std::deque<int >>()) cout << value << " " ;
94+ for (auto & value : std::deque<int >(wks.row (1 ).values ())) cout << value << " " ;
7295 cout << endl;
7396 for (auto & value : std::deque<double >(wks.row (2 ).values ())) cout << value << " " ;
7497 cout << endl;
@@ -77,6 +100,7 @@ int main()
77100 for (auto & value : std::deque<std::string>(wks.row (4 ).values ())) cout << value << " " ;
78101 cout << endl;
79102
103+ // Conversion to std::list<[int, double, bool, std::string]>
80104 cout << endl << " Conversion to std::list<[int, double, bool, std::string]> ..." << endl;
81105 for (auto & value : std::list<int >(wks.row (1 ).values ())) cout << value << " " ;
82106 cout << endl;
0 commit comments