Xlnt
Cross-Platform C++ Library for Excel Spreadsheet
Open Source API allows to Read, Write, Modify & Export Microsoft Excel XLSX Spreadsheet Files via C++ API.
Xlnt is a modern open source C++ library that provides features for manipulating XLSX spreadsheets files. It enables software developers to read and write spreadsheet files from/to XLSX files. On May 10th, 2017 the first public release of Xlnt version 1.0 was available for public use. The recent work mostly focuses on compatibility enhancements and better performance.
The Xlnt library offers functionality for many important features, such as creating Excel-style workbooks and numbers-style workbooks, encrypted Workbook creation, Excel Binary Workbook, document Properties, hyperlink support, Page Margins, Comments, Cell Styles, and many more.
Getting Started with Xlnt
The following steps update the compiler and set the appropriate environment variables.
Install PIP Command
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt-get upgrade
sudo apt-get install gcc-6 g++-6
export CC=/usr/bin/gcc-6
export CXX=/usr/bin/g++-6
C++ API to Read from an Existing XLSX Spreadsheet
The open source Xlnt library provides functionality for reading an existing XLSX spreadsheet inside C++ applications. It also allows developers to print the string values to the screen. Moreover, once you read the contents it is also possible to export the contents into a file and store in the location of your choice.
Read XLSX Spreadsheet via C++ API
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;
}
Apply Formatting & Style to Excel Spreadsheet
The Xlnt library enables programmers to apply formatting and styles to their data inside an Excel spreadsheet. A format in Xlnt corresponds to the alignment, border, fill, font, number format, and protection settings applied to a cell. On the other hand, a style is a named style created in the "Cell styles" dropdown in Excel. It needs to have a name and optionally any alignment, border, fill, font, number format, and protection. A cell can have both a format and a style.
Apply Number Formatting via C++ API
#include
#include
int main()
{
xlnt::workbook wb;
auto cell = wb.active_sheet().cell("A1");
cell.number_format(xlnt::number_format::percentage());
cell.value(0.513);
std::cout << cell.to_string() << std::endl;
return 0;
}
Setting Page Margins of a Worksheet
The open source Xlnt enables programmers to apply page margins to an Excel worksheet page inside their own C++ applications. Page margins specify how much blank area should be left around the information in an Excel worksheet. They provide a visual border for the user's printed pages and an area where the page can be held or bound.