Open Source C Library to Read Excel XLS Files
A Lightweight and Fast Open Source Free C Library for Loading, Reading and Extracting Data From Excel XLS Files without Requiring Microsoft Excel.
What is libxls?
Working with Microsoft Excel files is a common requirement in many software applications. While modern spreadsheets use the XLSX format, countless organizations still rely on legacy XLS files. Reading and extracting data from these files can be challenging because the binary XLS format is proprietary and complex. libxls is an open source C library specifically designed to simplify this task. It provides a fast and lightweight solution for reading Excel 97–2003 XLS files without requiring Microsoft Excel or any external Office components.
libxls is a portable C library that enables developers to read and extract information from Microsoft Excel XLS spreadsheets. It supports Excel 97–2003 (.xls) files, multiple worksheets, cell values and formatting information, unicode text, date and number extraction, cross-platform development and more. Unlike office automation approaches, libxls works independently of Microsoft Office, making it suitable for Linux, macOS, Windows, and embedded environments. Because of its simplicity, portability, and performance, libxls is widely used in data migration tools, spreadsheet converters, reporting applications, and cross-platform software projects.
Getting Started with libxls
libxls supports CMake for building and installing the library. Please use the following command.
Install libxls via CRAN
mkdir build
cd build
cmake ..
make
Clone the libxls source code repository from GitHub. Please use the following code.
Install libxls via GitHub
git clone https://github.com/libxls/libxls.git
# Or use your preferred protocol instead of https Reading Legacy Excel XLS Files via C Library
One of the biggest strengths of libxls is its ability to parse Excel 97–2003 binary XLS files. The library understands the internal structure of these spreadsheets and allows applications to access rows, columns, and cell values without requiring Microsoft Excel. This capability is especially useful for organizations that still archive historical data in XLS format. Developers can easily build import tools and automate data extraction processes while preserving compatibility with older documents. The following example opens an Excel XLS workbook using UTF-8 encoding. Once the workbook is loaded, developers can access worksheets and extract spreadsheet data.
How to Read Excel XLS Files via C Library
#include
int main()
{
xlsWorkBook *workbook;
workbook = xls_open("sample.xls", "UTF-8");
if(workbook)
{
printf("Workbook opened successfully\n");
xls_close_WB(workbook);
}
return 0;
}
Access Multiple Worksheets via C Library
Many Excel files contain several worksheets that organize information into different sections. libxls provides APIs for navigating and reading each worksheet independently. This feature is useful when processing financial reports, invoices, inventory sheets, or scientific datasets. Developers can loop through all sheets and extract information automatically without user intervention. The following code opens a workbook and retrieves the total number of worksheets available in the file.
How to Retrieves Total Number of Worksheets in XLS File via C?
#include
int main()
{
xlsWorkBook *wb = xls_open("report.xls", "UTF-8");
if(wb)
{
printf("Number of sheets: %d\n", wb->sheets.count);
xls_close_WB(wb);
}
return 0;
}
Reading Cell Values via C Library
The open source libxls allows applications to access individual cells and retrieve their contents. Whether the spreadsheet contains text, numbers, or dates, the library provides straightforward access to cell information. This capability makes libxls suitable for importing spreadsheet data into databases, generating reports, or converting Excel documents into CSV and JSON formats. The following code example reads the first cell in the worksheet and prints its value to the console.
How to Access Worksheet Cells and Prints its Value Via C Library?
#include
int main()
{
xlsWorkBook *wb = xls_open("sample.xls", "UTF-8");
if(wb)
{
xlsWorkSheet *sheet = xls_getWorkSheet(wb, 0);
xls_parseWorkSheet(sheet);
xlsCell *cell = xls_cell(sheet, 0, 0);
if(cell)
printf("Cell value: %s\n", cell->str);
xls_close_WS(sheet);
xls_close_WB(wb);
}
return 0;
}
Unicode and Character Encoding Support
Modern applications often work with multilingual spreadsheets. libxls supports Unicode text through configurable character encodings, making it possible to process files containing international languages and special characters. This feature is particularly useful for business applications handling customer names, addresses, and reports in different languages. By specifying UTF-8 encoding, the library correctly interprets Unicode text stored inside Excel files as shown in the following code example.
How to Interprets Unicode Text Stored inside Excel files via C?
xlsWorkBook *wb = xls_open("employees.xls", "UTF-8");
if(wb)
{
printf("Workbook loaded using UTF-8 encoding\n");
}