Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Brief Overview

The library uses either Libre Office API or Microsoft Office API as the underlaying API to provide an easy interface to manipulate Spreadsheets. For example we can open a spreadsheet document and export it to PDF format like this.

boost::document d("file_path");
d.open_document();
d.export_document(boost::document_file_format::PDF);
d.close_document();

We can perform other simple operations to the document like this, See the Reference of document header file.

A document needs to be opened first to manipulate it. It may or may not be closed explicitly as the destruction automatically triggers a close if it is open. However, closing and reopening causes buggy behaviour right now and thus should be avoided. The interface is designed to accomodate all the differences between the API's and thus uniform behaviour can be expected irregardless of the base.

Simple Sheet Operations such as inserting sheets, deleting sheets, counting the number of sheets, getting sheets by indices are currently supported. A simple example can be seen as follows.

boost::document d(file_path);
d.open_document();

boost::sheet s = d.get_sheet("Anurag");
s.rename_sheet("Vatika");

d.delete_sheet(0);

for(int i = 0;i < d.sheet_count(); i++) {
    sheet sx = d.getSheet(i);
    sx.rename_sheet("AnuragSheet" + std::to_string(i));
}

[Warning] Warning

Sheets are zero indexed irrespective of the underlaying API!

There are various ways of getting a cell from a sheet or a document. The [] operator has been overloaded, also there are get_cell() functions which also provide exception handling.

boost::document d(file_path);
boost::sheet s = d["Anurag"];
boost::cell c = s.get_cell(4,5); // row, column
boost::cell c1 = s[4][5]; // 5,4 actually
boost::cell c2 = s["C4"]; // 4,2

[Warning] Warning

Cells are zero indexed irrespective of the underlaying API!

Certain cell operations are also supported currently, such as setting and getting strings, doubles and formulas. For Example,

if(c1.get_content_type() == boost::cell_content_type::EMPTY) {
    return;
}
c1 = 14.66;
c1 = "Anurag";
c1.set_cell_formula("=C3+C4");
c1.set_cell_value("Anurag");
std::string s = c1.get_string();

Row and column classes are iteratable and can be used to manipulate the cells. The iterators themselves are random - access iterators and can be taken advantage of.

boost::sheet s1 = c["Anurag"];
boost::row r = s1.get_row(3);
std::fill(r.begin(), r.begin() + 50, 1);
double sum = 0;
const boost::row_iterator end(r.begin() + 50);
for (auto it = r.begin(); it != end; ++it) {
    sum += (*it).get_value();
}


PrevUpHomeNext