read.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <iostream>
  2. #include <xlnt/xlnt.hpp>
  3. // Read sample1.xlsx and print out a 2-dimensional
  4. // representation of each sheet. Cells are separated by commas.
  5. // Each new line is a new row.
  6. int main()
  7. {
  8. // Create a new workbook by reading sample1.xlsx in the current directory.
  9. xlnt::workbook wb;
  10. wb.load("sample1.xlsx");
  11. // The workbook class has begin and end methods so it can be iterated upon.
  12. // Each item is a sheet in the workbook.
  13. for(const auto sheet : wb)
  14. {
  15. // Print the title of the sheet on its own line.
  16. std::cout << sheet.get_title() << ": " << std::endl;
  17. // Iterating on a range, such as from worksheet::rows, yields cell_vectors.
  18. // Cell vectors don't actually contain cells to reduce overhead.
  19. // Instead they hold a reference to a worksheet and the current cell_reference.
  20. // Internally, calling worksheet::get_cell with the current cell_reference yields the next cell.
  21. // This allows easy and fast iteration over a row (sometimes a column) in the worksheet.
  22. for(auto row : sheet.rows())
  23. {
  24. for(auto cell : row)
  25. {
  26. // cell::operator<< adds a string represenation of the cell's value to the stream.
  27. std::cout << cell << ", ";
  28. }
  29. std::cout << std::endl;
  30. }
  31. }
  32. }