Examples

Simple - reading from an existing xlsx spread sheet.

The following C plus plus code will read the values from an xlsx file and print the string values to the screen. This is a very simple example to get you started.

#include <iostream>
#include <xlnt/xlnt.hpp>

int main()
{
    xlnt::workbook wb;
    wb.load("/home/timothymccallum/test.xlsx");
    auto ws = wb.active_sheet();
    std::clog << "Processing spread sheet" << std::endl;
    for (auto row : ws.rows(false)) 
    { 
        for (auto cell : row) 
        { 
            std::clog << cell.to_string() << std::endl;
        }
    }
    std::clog << "Processing complete" << std::endl;
    return 0;
}

Save the contents of the above file

Compile by typing the following command

Excecute by typing the following command

The output of the program, in my case, is as follows

As you can see the process.cpp file simply walks through the spread sheet values row by row and column by column (A1, B1, C1, A2, B2, C2 and so on).

Simple - storing a spread sheet in a 2 dimensional C++ Vector for further processing

Loading a spread sheet into a Vector provides oppourtunities for you to perform high performance processing. There will be more examples on performing fast look-ups, merging data, performing deduplication and more. For now, let's just learn how to get the spread sheet loaded into memory.

Save the contents of the above file

Compile by typing the following command

Excecute by typing the following command

The output of the program, in my case, is as follows

You will have noticed that this process is very fast. If you type the "time" as shown below, you can measure just how fast loading and retrieving your spread sheet is, using xlnt; In this case only a fraction of a second. More on this later.

Simple - writing values to a new xlsx spread sheet.

This process is also quite quick; a time command showed that xlnt was able to create and write 10, 000 values to the output spread sheet in 0.582 seconds.

Last updated