documentation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (c) 2017-2018 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <xlnt/xlnt.hpp>
  24. #include "helpers/path_helper.hpp"
  25. // Readme
  26. // from https://tfussell.gitbooks.io/xlnt/content/ and https://github.com/tfussell/xlnt/blob/master/README.md
  27. void sample_readme_example1()
  28. {
  29. xlnt::workbook wb;
  30. xlnt::worksheet ws = wb.active_sheet();
  31. ws.cell("A1").value(5);
  32. ws.cell("B2").value("string data");
  33. ws.cell("C3").formula("=RAND()");
  34. ws.merge_cells("C3:C4");
  35. ws.freeze_panes("B2");
  36. wb.save("sample.xlsx");
  37. }
  38. // Simple - reading from an existing xlsx spread sheet.
  39. // from https://tfussell.gitbooks.io/xlnt/content/docs/introduction/Examples.html
  40. void sample_read_and_print_example()
  41. {
  42. xlnt::workbook wb;
  43. wb.load(path_helper::sample_file("documentation-print.xlsx")); // modified to use the test data directory
  44. auto ws = wb.active_sheet();
  45. std::clog << "Processing spread sheet" << std::endl;
  46. for (auto row : ws.rows(false))
  47. {
  48. for (auto cell : row)
  49. {
  50. std::clog << cell.to_string() << std::endl;
  51. }
  52. }
  53. std::clog << "Processing complete" << std::endl;
  54. }
  55. // Simple - storing a spread sheet in a 2 dimensional C++ Vector for further processing
  56. // from https://tfussell.gitbooks.io/xlnt/content/docs/introduction/Examples.html
  57. void sample_read_into_vector_example()
  58. {
  59. xlnt::workbook wb;
  60. wb.load(path_helper::sample_file("documentation-print.xlsx")); // modified to use the test data directory
  61. auto ws = wb.active_sheet();
  62. std::clog << "Processing spread sheet" << std::endl;
  63. std::clog << "Creating a single vector which stores the whole spread sheet" << std::endl;
  64. std::vector<std::vector<std::string>> theWholeSpreadSheet;
  65. for (auto row : ws.rows(false))
  66. {
  67. std::clog << "Creating a fresh vector for just this row in the spread sheet" << std::endl;
  68. std::vector<std::string> aSingleRow;
  69. for (auto cell : row)
  70. {
  71. std::clog << "Adding this cell to the row" << std::endl;
  72. aSingleRow.push_back(cell.to_string());
  73. }
  74. std::clog << "Adding this entire row to the vector which stores the whole spread sheet" << std::endl;
  75. theWholeSpreadSheet.push_back(aSingleRow);
  76. }
  77. std::clog << "Processing complete" << std::endl;
  78. std::clog << "Reading the vector and printing output to the screen" << std::endl;
  79. for (int rowInt = 0; rowInt < theWholeSpreadSheet.size(); rowInt++)
  80. {
  81. for (int colInt = 0; colInt < theWholeSpreadSheet.at(rowInt).size(); colInt++)
  82. {
  83. std::cout << theWholeSpreadSheet.at(rowInt).at(colInt) << std::endl;
  84. }
  85. }
  86. }
  87. // Simple - writing values to a new xlsx spread sheet.
  88. // from https://tfussell.gitbooks.io/xlnt/content/docs/introduction/Examples.html
  89. void sample_write_sheet_to_file_example()
  90. {
  91. // Creating a 2 dimensional vector which we will write values to
  92. std::vector<std::vector<std::string>> wholeWorksheet;
  93. //Looping through each row (100 rows as per the second argument in the for loop)
  94. for (int outer = 0; outer < 100; outer++)
  95. {
  96. //Creating a fresh vector for a fresh row
  97. std::vector<std::string> singleRow;
  98. //Looping through each of the columns (100 as per the second argument in the for loop) in this particular row
  99. for (int inner = 0; inner < 100; inner++)
  100. {
  101. //Adding a single value in each cell of the row
  102. std::string val = std::to_string(inner + 1);
  103. singleRow.push_back(val);
  104. }
  105. //Adding the single row to the 2 dimensional vector
  106. wholeWorksheet.push_back(singleRow);
  107. std::clog << "Writing to row " << outer << " in the vector " << std::endl;
  108. }
  109. //Writing to the spread sheet
  110. //Creating the output workbook
  111. std::clog << "Creating workbook" << std::endl;
  112. xlnt::workbook wbOut;
  113. //Setting the destination output file name
  114. std::string dest_filename = "output.xlsx";
  115. //Creating the output worksheet
  116. xlnt::worksheet wsOut = wbOut.active_sheet();
  117. //Giving the output worksheet a title/name
  118. wsOut.title("data");
  119. //We will now be looping through the 2 dimensional vector which we created above
  120. //In this case we have two iterators one for the outer loop (row) and one for the inner loop (column)
  121. std::clog << "Looping through vector and writing to spread sheet" << std::endl;
  122. for (int fOut = 0; fOut < wholeWorksheet.size(); fOut++)
  123. {
  124. std::clog << "Row" << fOut << std::endl;
  125. for (int fIn = 0; fIn < wholeWorksheet.at(fOut).size(); fIn++)
  126. {
  127. //Take notice of the difference between accessing the vector and accessing the work sheet
  128. //As you may already know Excel spread sheets start at row 1 and column 1 (not row 0 and column 0 like you would expect from a C++ vector)
  129. //In short the xlnt cell reference starts at column 1 row 1 (hence the + 1s below) and the vector reference starts at row 0 and column 0
  130. wsOut.cell(xlnt::cell_reference(fIn + 1, fOut + 1)).value(wholeWorksheet.at(fOut).at(fIn));
  131. //Further clarification to avoid confusion
  132. //Cell reference arguments are (column number, row number); e.g. cell_reference(fIn + 1, fOut + 1)
  133. //Vector arguments are (row number, column number); e.g. wholeWorksheet.at(fOut).at(fIn)
  134. }
  135. }
  136. std::clog << "Finished writing spread sheet" << std::endl;
  137. wbOut.save(dest_filename);
  138. }
  139. // Number Formatting
  140. // from https://tfussell.gitbooks.io/xlnt/content/docs/advanced/Formatting.html
  141. void sample_number_formatting_example()
  142. {
  143. xlnt::workbook wb;
  144. auto cell = wb.active_sheet().cell("A1");
  145. cell.number_format(xlnt::number_format::percentage());
  146. cell.value(0.513);
  147. std::cout << '\n'
  148. << cell.to_string() << std::endl;
  149. }
  150. // Properties
  151. // from https://tfussell.gitbooks.io/xlnt/content/docs/advanced/Properties.html
  152. void sample_properties_example()
  153. {
  154. xlnt::workbook wb;
  155. wb.core_property(xlnt::core_property::category, "hors categorie");
  156. wb.core_property(xlnt::core_property::content_status, "good");
  157. wb.core_property(xlnt::core_property::created, xlnt::datetime(2017, 1, 15));
  158. wb.core_property(xlnt::core_property::creator, "me");
  159. wb.core_property(xlnt::core_property::description, "description");
  160. wb.core_property(xlnt::core_property::identifier, "id");
  161. wb.core_property(xlnt::core_property::keywords, {"wow", "such"});
  162. wb.core_property(xlnt::core_property::language, "Esperanto");
  163. wb.core_property(xlnt::core_property::last_modified_by, "someone");
  164. wb.core_property(xlnt::core_property::last_printed, xlnt::datetime(2017, 1, 15));
  165. wb.core_property(xlnt::core_property::modified, xlnt::datetime(2017, 1, 15));
  166. wb.core_property(xlnt::core_property::revision, "3");
  167. wb.core_property(xlnt::core_property::subject, "subject");
  168. wb.core_property(xlnt::core_property::title, "title");
  169. wb.core_property(xlnt::core_property::version, "1.0");
  170. wb.extended_property(xlnt::extended_property::application, "xlnt");
  171. wb.extended_property(xlnt::extended_property::app_version, "0.9.3");
  172. wb.extended_property(xlnt::extended_property::characters, 123);
  173. wb.extended_property(xlnt::extended_property::characters_with_spaces, 124);
  174. wb.extended_property(xlnt::extended_property::company, "Incorporated Inc.");
  175. wb.extended_property(xlnt::extended_property::dig_sig, "?");
  176. wb.extended_property(xlnt::extended_property::doc_security, 0);
  177. wb.extended_property(xlnt::extended_property::heading_pairs, true);
  178. wb.extended_property(xlnt::extended_property::hidden_slides, false);
  179. wb.extended_property(xlnt::extended_property::h_links, 0);
  180. wb.extended_property(xlnt::extended_property::hyperlink_base, 0);
  181. wb.extended_property(xlnt::extended_property::hyperlinks_changed, true);
  182. wb.extended_property(xlnt::extended_property::lines, 42);
  183. wb.extended_property(xlnt::extended_property::links_up_to_date, false);
  184. wb.extended_property(xlnt::extended_property::manager, "johnny");
  185. wb.extended_property(xlnt::extended_property::m_m_clips, "?");
  186. wb.extended_property(xlnt::extended_property::notes, "note");
  187. wb.extended_property(xlnt::extended_property::pages, 19);
  188. wb.extended_property(xlnt::extended_property::paragraphs, 18);
  189. wb.extended_property(xlnt::extended_property::presentation_format, "format");
  190. wb.extended_property(xlnt::extended_property::scale_crop, true);
  191. wb.extended_property(xlnt::extended_property::shared_doc, false);
  192. wb.extended_property(xlnt::extended_property::slides, 17);
  193. wb.extended_property(xlnt::extended_property::template_, "template!");
  194. wb.extended_property(xlnt::extended_property::titles_of_parts, {"title"});
  195. wb.extended_property(xlnt::extended_property::total_time, 16);
  196. wb.extended_property(xlnt::extended_property::words, 101);
  197. wb.custom_property("test", {1, 2, 3});
  198. wb.custom_property("Editor", "John Smith");
  199. wb.save("lots_of_properties.xlsx");
  200. }
  201. int main()
  202. {
  203. sample_readme_example1();
  204. std::clog << '\n';
  205. sample_read_and_print_example();
  206. std::clog << '\n';
  207. sample_read_into_vector_example();
  208. std::clog << '\n';
  209. sample_write_sheet_to_file_example();
  210. std::clog << '\n';
  211. sample_number_formatting_example();
  212. std::clog << '\n';
  213. sample_properties_example();
  214. std::clog.flush();
  215. return 0;
  216. }