Direct reading and writing Excel files
LibXL is a library that can read and write Excel files. It doesn't require Microsoft Excel and .NET framework, combines an easy to use and powerful features. Library can be used to
- Generate a new spreadsheet from scratch
- Extract data from an existing spreadsheet
- Edit an existing spreadsheet
LibXL can help your applications in exporting and extracting data to/from Excel files with minimum effort.
Also it can be used as report engine. Library can be used in C, C++, C#, Delphi, PHP, Python, PureBasic, Xojo, Lazarus, Fortran and other languages. Supports Excel 97-2003 binary formats (xls) and Excel 2007-2021 XML formats (xlsx/xlsm). Supports Unicode and 64-bit platforms. There are a wrapper for .NET developers and separate Linux, Mac and iOS editions. See features of the library in
demo.xls or
demo.xlsx files.
Simple interoperate, no more Excel dependency
LibXL has C/C++ headers, Delphi unit and .NET assembly for including in your project. No OLE automation.
Customizing the look and feel
LibXL supports numerous formatting options: alignments, borders, colors, fill patterns, fonts, merging cells and so on.
High performance
Writing speed is about 2 100 000 cells per second for numbers and 240 000 cells per second for 8-character random strings in binary xls format (CPU 3.2 GHz).
Royalty-free distribution with your application
Our customers can use this library in their commercial applications without any additional fees.
#include "libxl.h"
using namespace libxl;
int main()
{
Book* book = xlCreateXMLBook();
if(book)
{
Sheet* sheet = book->addSheet(L"Sheet1");
if(sheet)
{
sheet->writeStr(2, 1, L"Hello, World !");
sheet->writeNum(3, 1, 1000);
}
book->save(L"example.xlsx");
book->release();
}
return 0;
}
Previous example
Next example
Invoice example
Download now
Purchase now
Code example: generate a new spreadsheet from scratch
#include "libxl.h"
int main()
{
BookHandle book = xlCreateXMLBook();
if(book)
{
SheetHandle sheet = xlBookAddSheet(book, L"Sheet1");
if(sheet)
{
xlSheetWriteStr(sheet, 2, 1, L"Hello, World !", NULL);
xlSheetWriteNum(sheet, 3, 1, 1000, NULL);
}
xlBookSave(book, L"example.xlsx");
xlBookRelease(book);
}
return 0;
}
#include "libxl.h"
using namespace libxl;
int main()
{
Book* book = xlCreateXMLBook();
if(book)
{
Sheet* sheet = book->addSheet(L"Sheet1");
if(sheet)
{
sheet->writeStr(2, 1, L"Hello, World !");
sheet->writeNum(3, 1, 1000);
}
book->save(L"example.xlsx");
book->release();
}
return 0;
}
class Program
{
static void Main(string[] args)
{
try
{
Book book = new XmlBook();
Sheet sheet = book.addSheet("Sheet1");
sheet.writeStr(2, 1, "Hello, World !");
sheet.writeNum(3, 1, 1000);
book.save("example.xlsx");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
}
var
Book: TBook;
Sheet: TSheet;
begin
Book := TXmlBook.Create;
Sheet := Book.addSheet('Sheet1');
Sheet.writeStr(2, 1, 'Hello, World !');
Sheet.writeNum(3, 1, 1000);
Book.save('example.xlsx');
Book.Free;
end;
<?php
$book = new ExcelBook('', '', true);
$sheet = $book->addSheet('Sheet1');
$sheet->write(2, 1, 'Hello, World !');
$sheet->write(3, 1, 1000);
$book->save('example.xlsx');
?>
XIncludeFile "libxl.pb"
book = xlCreateXMLBook()
sheet = xlBookAddSheet(book, "Sheet1")
xlSheetWriteStr(sheet, 2, 1, "Hello, World !")
xlSheetWriteNum(sheet, 3, 1, 1000)
xlBookSave(book, "example.xlsx")
xlBookRelease(book)
End
Code example: extract data from an existing spreadsheet
BookHandle book = xlCreateXMLBook();
if(book)
{
if(xlBookLoad(book, L"example.xlsx"))
{
SheetHandle sheet = xlBookGetSheet(book, 0);
if(sheet)
{
double d;
const wchar_t* s = xlSheetReadStr(sheet, 2, 1, NULL);
if(s) wprintf(L"%s\n", s);
d = xlSheetReadNum(sheet, 3, 1, NULL);
printf("%g\n", d);
}
}
xlBookRelease(book);
}
Book* book = xlCreateXMLBook();
if(book)
{
if(book->load(L"example.xlsx"))
{
Sheet* sheet = book->getSheet(0);
if(sheet)
{
const wchar_t* s = sheet->readStr(2, 1);
if(s) wcout << s << endl;
double d = sheet->readNum(3, 1);
cout << d << endl;
}
}
book->release();
}
try
{
Book book = new XmlBook();
book.load("example.xlsx");
Sheet sheet = book.getSheet(0);
string s = sheet.readStr(2, 1);
Console.WriteLine(s);
double d = sheet.readNum(3, 1);
Console.WriteLine(d);
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
var
Book: TBook;
Sheet: TSheet;
d: double;
s: string;
begin
Book := TXmlBook.Create;
Book.load('example.xlsx');
Sheet := Book.getSheet(0);
s := Sheet.readStr(2, 1);
d := Sheet.readNum(3, 1);
Book.Free;
end;
<?php
$book = new ExcelBook('', '', true);
if($book->loadFile('example.xlsx'))
{
$sheet = $book->getSheet(0);
$s = $sheet->read(2, 1);
$d = $sheet->read(3, 1);
echo $s."<br/>".$d;
}
?>
XIncludeFile "libxl.pb"
book = xlCreateXMLBook()
If(xlBookLoad(book, "example.xlsx"))
sheet = xlBookGetSheet(book, 0)
s$ = xlSheetReadStr(sheet, 2, 1, 0)
d = xlSheetReadNum(sheet, 3, 1, 0)
OpenConsole()
PrintN(s$)
PrintN(StrU(d))
CloseConsole()
EndIf
xlBookRelease(book)
End
Code example: edit an existing spreadsheet
BookHandle book = xlCreateXMLBook();
if(book)
{
if(xlBookLoad(book, L"example.xls"))
{
SheetHandle sheet = xlBookGetSheet(book, 0);
if(sheet)
{
double d = xlSheetReadNum(sheet, 3, 1, NULL);
xlSheetWriteNum(sheet, 3, 1, d * 2, NULL);
xlSheetWriteStr(sheet, 4, 1, L"new string", NULL);
}
xlBookSave(book, L"example.xlsx");
}
xlBookRelease(book);
}
Book* book = xlCreateXMLBook();
if(book)
{
if(book->load(L"example.xlsx"))
{
Sheet* sheet = book->getSheet(0);
if(sheet)
{
double d = sheet->readNum(3, 1);
sheet->writeNum(3, 1, d * 2);
sheet->writeStr(4, 1, L"new string");
}
book->save(L"example.xlsx");
}
book->release();
}
try
{
Book book = new XmlBook();
book.load("example.xlsx");
Sheet sheet = book.getSheet(0);
double d = sheet.readNum(3, 1);
sheet.writeNum(3, 1, d * 2);
sheet.writeStr(4, 1, "new string");
book.save("example.xlsx");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
var
Book: TBook;
Sheet: TSheet;
d: double;
begin
Book := TXmlBook.Create;
Book.load('example.xlsx');
Sheet := Book.getSheet(0);
d := Sheet.readNum(3, 1);
Sheet.writeNum(3, 1, d * 2);
Book.save('example.xlsx');
Book.Free;
end;
<?php
$book = new ExcelBook('', '', true);
if($book->loadFile('example.xlsx'))
{
$sheet = $book->getSheet(0);
$d = $sheet->read(3, 1);
$sheet->write(3, 1, $d * 2);
$sheet->write(4, 1, 'new string');
$book->save('example.xlsx');
}
?>
XIncludeFile "libxl.pb"
book = xlCreateXMLBook()
If(xlBookLoad(book, "example.xlsx"))
sheet = xlBookGetSheet(book, 0)
d = xlSheetReadNum(sheet, 3, 1, 0)
xlSheetWriteNum(sheet, 3, 1, d * 2)
xlSheetWriteStr(sheet, 4, 1, "new string")
xlBookSave(book, "example.xlsx")
EndIf
xlBookRelease(book)
End
Code example: apply formatting options
font = xlBookAddFont(book, NULL);
xlFontSetName(font, L"Impact");
xlFontSetSize(font, 36);
format = xlBookAddFormat(book, NULL);
xlFormatSetAlignH(format, ALIGNH_CENTER);
xlFormatSetBorder(format, BORDERSTYLE_MEDIUMDASHDOTDOT);
xlFormatSetBorderColor(format, COLOR_RED);
xlFormatSetFont(format, font);
sheet = xlBookAddSheet(book, L"Custom");
if(sheet)
{
xlSheetWriteStr(sheet, 2, 1, L"Format", format);
xlSheetSetCol(sheet, 1, 1, 25, NULL, 0);
}
xlBookSave(book, L"format.xlsx");
Font* font = book->addFont();
font->setName(L"Impact");
font->setSize(36);
Format* format = book->addFormat();
format->setAlignH(ALIGNH_CENTER);
format->setBorder(BORDERSTYLE_MEDIUMDASHDOTDOT);
format->setBorderColor(COLOR_RED);
format->setFont(font);
Sheet* sheet = book->addSheet(L"Custom");
if(sheet)
{
sheet->writeStr(2, 1, L"Format", format);
sheet->setCol(1, 1, 25);
}
book->save(L"format.xlsx");
Book book = new XmlBook();
Font font = book.addFont();
font.size = 36;
Format format = book.addFormat();
format.alignH = AlignH.ALIGNH_CENTER;
format.setBorder(BorderStyle.BORDERSTYLE_MEDIUMDASHDOTDOT);
format.setBorderColor(Color.COLOR_RED);
format.font = font;
Sheet sheet = book.addSheet("Sheet1");
sheet.writeStr(2, 1, "Format", format);
sheet.setCol(1, 1, 25);
book.save("format.xlsx");
var
Book: TBook;
Sheet: TSheet;
Font: TFont;
Format: TFormat;
begin
Book := TXmlBook.Create;
Font := Book.addFont();
Font.setSize(36);
Format := Book.addFormat();
Format.alignH := ALIGNH_CENTER;
Format.setBorder(BORDERSTYLE_MEDIUMDASHDOTDOT);
Format.Font := Font;
Sheet := Book.addSheet('Custom');
Sheet.writeStr(2, 1, 'Format', Format);
Sheet.setCol(1, 1, 25);
Book.save('format.xlsx');
Book.Free;
end;
<?php
$book = new ExcelBook('', '', true);
$font = $book->addFont();
$font->name('Impact');
$font->size(36);
$format = $book->addFormat();
$format->horizontalAlign(ExcelFormat::ALIGNH_CENTER);
$format->borderStyle(ExcelFormat::BORDERSTYLE_MEDIUMDASHDOTDOT);
$format->borderColor(ExcelFormat::COLOR_RED);
$format->setFont($font);
$sheet = $book->addSheet('Custom');
$sheet->write(2, 1, 'Format', $format);
$sheet->setColWidth(1, 1, 25);
$book->save('format.xlsx');
?>
XIncludeFile "libxl.pb"
book = xlCreateXMLBook()
font = xlBookAddFont(book)
xlFontSetName(font, "Impact")
xlFontSetSize(font, 36)
format = xlBookAddFormat(book)
xlFormatSetAlignH(format, #ALIGNH_CENTER)
xlFormatSetBorder(format, #BORDERSTYLE_MEDIUMDASHDOTDOT)
xlFormatSetBorderColor(format, #COLOR_RED)
xlFormatSetFont(format, font)
sheet = xlBookAddSheet(book, "Custom")
xlSheetWriteStr(sheet, 2, 1, "Format", format)
xlSheetSetCol(sheet, 1, 1, 25)
xlBookSave(book, "format.xlsx")
xlBookRelease(book)
End