write.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <iostream>
  2. #include <xlnt/xlnt.hpp>
  3. // Make 3 sheets containing 10,000 cells with unique numeric values.
  4. int main()
  5. {
  6. // create the workbook
  7. xlnt::workbook workbook;
  8. // workbooks have a single sheet called "Sheet" initially so let's get rid of it
  9. auto to_remove = workbook.get_sheet_by_name("Sheet");
  10. workbook.remove_sheet(to_remove);
  11. // this loop will create three sheets and populate them with data
  12. for(int i = 0; i < 3; i++)
  13. {
  14. // the title will be "Sample2-1", "Sample2-2", or "Sample2-3"
  15. // if we don't specify a title, these would be "Sheet#" where
  16. // # is the lowest number that doesn't have a corresponding sheet yet.
  17. auto sheet = workbook.create_sheet("Sample2-" + std::to_string(i));
  18. for(int row = 1; row < 101; row++)
  19. {
  20. for(int column = 1; column < 101; column++)
  21. {
  22. // Since we can't overload subscript to accept both number,
  23. // create a cell_reference which "points" to the current cell.
  24. xlnt::cell_reference ref(column, row);
  25. // This is important!
  26. // The cell class is really just a wrapper around a pointer.
  27. // For this reason, we can store them by value and still modify
  28. // the data in the containing worksheet.
  29. auto cell = sheet[ref];
  30. // set_value has overloads for many types such as strings and ints
  31. cell.set_value(row * 100 + column);
  32. }
  33. }
  34. }
  35. // This will be written to the current directory.
  36. workbook.save("sample2.xlsx");
  37. }