create.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <string>
  2. #include <unordered_map>
  3. #include <utility>
  4. #include <xlnt/xlnt.hpp>
  5. int main()
  6. {
  7. const std::vector<std::pair<std::string, double>> amounts =
  8. {
  9. { "Anne", 17.31 },
  10. { "Brent", 21.99 },
  11. { "Catelyn", 94.47 },
  12. { "Diedrich", 101.05 }
  13. };
  14. xlnt::workbook wb;
  15. auto ws = wb.get_active_sheet();
  16. ws.get_cell("A1").set_value("Name");
  17. ws.get_cell("B1").set_value("Amount");
  18. std::size_t row = 2;
  19. auto money_format = xlnt::number_format::from_builtin_id(44);
  20. auto &style = wb.create_style("Currency");
  21. style.set_builtin_id(4);
  22. style.set_number_format(money_format);
  23. for (const auto &amount : amounts)
  24. {
  25. ws.get_cell(xlnt::cell_reference(1, row)).set_value(amount.first);
  26. ws.get_cell(xlnt::cell_reference(2, row)).set_value(amount.second);
  27. ws.get_cell(xlnt::cell_reference(2, row)).set_style("Currency");
  28. row++;
  29. }
  30. std::string sum_formula = "=SUM(B2:B" + std::to_string(row - 1) + ")";
  31. ws.get_cell(xlnt::cell_reference(2, row)).set_style("Currency");
  32. ws.get_cell(xlnt::cell_reference(2, row)).set_formula(sum_formula);
  33. wb.save("create.xlsx");
  34. return 0;
  35. }