xlnt/samples/sample2.cpp

45 lines
1.7 KiB
C++
Raw Normal View History

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