xlnt/samples/sample1.cpp

38 lines
1.3 KiB
C++
Raw Normal View History

2015-10-14 12:41:04 +08:00
#include <iostream>
#include <xlnt/xlnt.hpp>
/// <summary>
/// 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.
/// </summary>
int main()
{
// Create a new workbook by reading sample1.xlsx in the current directory.
2015-10-26 16:37:04 +08:00
auto wb = xlnt::excel_reader::load_workbook("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.
for(auto sheet : wb)
{
// Print the title of the sheet on its own line.
std::cout << sheet.get_title() << ": " << std::endl;
// 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.
for(auto row : sheet.rows())
{
for(auto cell : row)
{
// cell::operator<< adds a string represenation of the cell's value to the stream.
std::cout << cell << ", ";
}
std::cout << std::endl;
}
}
}