1. Products
  2.   Spreadsheet
  3.   C++
  4.   Libxlsxwriter
 
  

Open Source C Library for Excel Spreadsheets

Create, Edit, Manipulate, and Convert Microsoft Excel 2007 and XLSX Spreadsheet files, merging spreadsheets, adding charts to worksheet via Open Source C API.

Libxlsxwriter is an open source C library that can be used to work with Microsoft Excel 2007 as well as Excel XLSX files inside your own applications. The Libxlsxwriter library is very compatible with Excel XLSX files and allows developers to create, modify, and process XLSX spreadsheets with ease. The library works smoothly on leading platforms like Linux, FreeBSD, OpenBSD, OS X, iOS, and Windows. It can compile for 32 as well as for 64-bit.

The open source Libxlsxwriter library is well optimized and can properly work with large spreadsheet files. It has included support for several important features, such as Merging spreadsheet cells, defining names, Autofilters support, adding Charts to worksheet pages, data validation, and drop-down lists, adding PNG/JPEG images, adding cell comments, working with VBA macros, adding or remove the column from a sheet, and many more.

Previous Next

Getting Started with Libxlsxwriter

Libxlsxwriter supports CMake for building and installing the library. Please use the following command.

Install libxlsxwriter via CRAN

 cmake $SOURCE_DIR $FLAGS 

Clone the libxlsxwriter source code repository from GitHub. Please use the following code.

Install libxlsxwriter via GitHub

it clone https://github.com/jmcnamara/libxlsxwriter.git
# Or use your preferred protocol instead of https 

C API to Create Excel XLSX Spreadsheet

The open source Libxlsxwriter API enables software developers to create and modify Excel XLSX Spreadsheets inside their C applications. The API provides support for writing text, numbers, formulas, and hyperlinks to multiple worksheets in an Excel spreadsheet file. You can easily import or export data from Excel files and can merge or split sheets, search data, and much more.

Create Excel Spreadsheet via C API<

#include "xlsxwriter.h"

int main() {

    /* Create a new workbook and add a worksheet. */
    lxw_workbook  *workbook  = workbook_new("demo.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    /* Add a format. */
    lxw_format *format = workbook_add_format(workbook);

    /* Set the bold property for the format */
    format_set_bold(format);

    /* Change the column width for clarity. */
    worksheet_set_column(worksheet, 0, 0, 20, NULL);

    /* Write some simple text. */
    worksheet_write_string(worksheet, 0, 0, "Hello", NULL);

    /* Text with formatting. */
    worksheet_write_string(worksheet, 1, 0, "World", format);

    /* Write some numbers. */
    worksheet_write_number(worksheet, 2, 0, 123,     NULL);
    worksheet_write_number(worksheet, 3, 0, 123.456, NULL);

    /* Insert an image. */
    worksheet_insert_image(worksheet, 1, 2, "logo.png");

    workbook_close(workbook);

    return 0;
}

Working with Excel Charts

The Libxlsxwriter API has provided functionality for adding and modifying Charts to Excel spreadsheets using C commands. You can use chart marker functions to generate and manage charts. You can also add a trendline to indicate trends in the data and error bars to indicate error bounds in the data. You can also data labels to a chart series to indicate the values of the plotted data points. You can easily apply chart formatting properties to any chart object.

Create Charts in Excel Worksheet via Libxlsxwriter

#include "xlsxwriter.h"
 
int main() {
 
    lxw_workbook  *workbook  = new_workbook("chart.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
 
    // User function to add data to worksheet, not shown here.
    write_worksheet_data(worksheet);
 
    // Create a chart object.
    lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
 
    // In the simplest case we just add some value data series.
    // The NULL categories will default to 1 to 5 like in Excel.
    chart_add_series(chart, NULL, "=Sheet1!$A$1:$A$5");
    chart_add_series(chart, NULL, "=Sheet1!$B$1:$B$5");
    chart_add_series(chart, NULL, "=Sheet1!$C$1:$C$5");
 
    // Insert the chart into the worksheet
    worksheet_insert_chart(worksheet, CELL("B7"), chart);
 
    return workbook_close(workbook);
}

Work with Excel Data Validation

Data validation is an Excel feature that lets developers control what users enter into an Excel cell. For example, you can restrict users to enter a number between 1 and 6 or make sure the text entry in a cell is less than 25 characters. The Libxlsxwriter API provides numerous options to validate user input such as Any Value, Whole Number, Decimal, List, Date, Time, Text length, or custom formula.

Display Customize Mssage When Integer is not b/t 1 & 100

// Display a custom info message when integer isn't between 1 and 100.
data_validation->validate         = LXW_VALIDATION_TYPE_INTEGER;
data_validation->criteria         = LXW_VALIDATION_CRITERIA_BETWEEN;
data_validation->minimum_number   = 1;
data_validation->maximum_number   = 100;
data_validation->input_title      = "Enter an integer:";
data_validation->input_message    = "between 1 and 100";
data_validation->error_title      = "Input value is not valid!";
data_validation->error_message    = "It should be an integer between 1 and 100";

Working with VBA Macros

The Libxlsxwriter API gives software developers the ease to add a VBA file containing functions or macros to an XLSX workbook. Excel files that contain functions and macros should use an XLSM extension or else Excel will complain and possibly not open the file. The VBA macros normally refer to workbooks and worksheet objects. If the VBA codenames aren't specified explicitly then libxlsxwriter will use the Excel defaults of ThisWorkbook and Sheet1, Sheet2, etc.

Add VBA Macros to Excel Workbook inside C Apps

 
#include "xlsxwriter.h"
 
int main() {
 
    /* Note the xlsm extension of the filename */
    lxw_workbook  *workbook  = workbook_new("macro.xlsm");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
 
    worksheet_set_column(worksheet, COLS("A:A"), 30, NULL);
 
    /* Add a macro file extracted from an Excel workbook. */
    workbook_add_vba_project(workbook, "vbaProject.bin");
    worksheet_write_string(worksheet, 2, 0, "Press the button to say hello.", NULL);
    lxw_button_options options = {.caption = "Press Me", .macro = "say_hello",
                                  .width = 80, .height = 30};
     worksheet_insert_button(worksheet, 2, 1, &options);
 
    return workbook_close(workbook);
}
 English