xlnt/samples/sample1.cpp

37 lines
1.3 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
// Read sample1.xlsx and print out a 2-dimensional
// representation of each sheet. Cells are separated by commas.
// Each new line is a new row.
2015-10-14 12:41:04 +08:00
int main()
{
// Create a new workbook by reading sample1.xlsx in the current directory.
2015-11-02 13:29:39 +08:00
xlnt::workbook wb;
wb.load("sample1.xlsx");
2015-10-14 12:41:04 +08:00
// The workbook class has begin and end methods so it can be iterated upon.
// Each item is a sheet in the workbook.
2015-11-02 13:29:39 +08:00
for(const auto sheet : wb)
{
2015-10-14 12:41:04 +08:00
// Print the title of the sheet on its own line.
2015-11-02 13:29:39 +08:00
std::cout << sheet.get_title() << ": " << std::endl;
2015-10-14 12:41:04 +08:00
// Iterating on a range, such as from worksheet::rows, yields cell_vectors.
// Cell vectors don't actually contain cells to reduce overhead.
// Instead they hold a reference to a worksheet and the current cell_reference.
// Internally, calling worksheet::get_cell with the current cell_reference yields the next cell.
// This allows easy and fast iteration over a row (sometimes a column) in the worksheet.
2015-11-02 13:29:39 +08:00
for(auto row : sheet.rows())
{
for(auto cell : row)
{
2015-10-14 12:41:04 +08:00
// cell::operator<< adds a string represenation of the cell's value to the stream.
2015-11-02 13:29:39 +08:00
std::cout << cell << ", ";
}
2015-10-14 12:41:04 +08:00
2015-11-02 13:29:39 +08:00
std::cout << std::endl;
}
}
2015-10-14 12:41:04 +08:00
}