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, Fortran and other languages. Supports Excel 97-2003 binary formats (xls) and Excel 2007/2010 xml formats (xlsx). Supports Unicode and 64-bit platforms. There are a wrapper for .NET developers and separate Mac and Linux edition. See features of the library in
demo.xls or
demo.xlsx files. They have been produced by LibXL.
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 format (CPU 3.2 GHz).
Royalty-free distribution with your application
Our customers can use this library in theirs commercial applications without any fees.
#include "libxl.h"
using namespace libxl;
int main()
{
Book* book = xlCreateBook();
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.xls");
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 = xlCreateBook();
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.xls");
xlBookRelease(book);
}
return 0;
}
#include "libxl.h"
using namespace libxl;
int main()
{
Book* book = xlCreateBook();
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.xls");
book->release();
}
return 0;
}
class Program
{
static void Main(string[] args)
{
try
{
Book book = new BinBook();
Sheet sheet = book.addSheet("Sheet1");
sheet.writeStr(2, 1, "Hello, World !");
sheet.writeNum(3, 1, 1000);
book.save("example.xls");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
}
var
Book: TBook;
Sheet: TSheet;
begin
Book := TBinBook.Create;
Sheet := Book.addSheet('Sheet1');
Sheet.writeStr(2, 1, 'Hello, World !');
Sheet.writeNum(3, 1, 1000);
Book.save('example.xls');
Book.Free;
end;
Code example: extract data from an existing spreadsheet
BookHandle book = xlCreateBook();
if(book)
{
if(xlBookLoad(book, L"example.xls"))
{
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 = xlCreateBook();
if(book)
{
if(book->load(L"example.xls"))
{
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 BinBook();
book.load("example.xls");
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 := TBinBook.Create;
Book.load('example.xls');
Sheet := Book.getSheet(0);
s := Sheet.readStr(2, 1);
d := Sheet.readNum(3, 1);
Book.Free;
end;
Code example: edit an existing spreadsheet
BookHandle book = xlCreateBook();
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.xls");
}
xlBookRelease(book);
}
Book* book = xlCreateBook();
if(book)
{
if(book->load(L"example.xls"))
{
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.xls");
}
book->release();
}
try
{
Book book = new BinBook();
book.load("example.xls");
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.xls");
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
var
Book: TBook;
Sheet: TSheet;
d: double;
begin
Book := TBinBook.Create;
Book.load('example.xls');
Sheet := Book.getSheet(0);
d := Sheet.readNum(3, 1);
Sheet.writeNum(3, 1, d * 2);
Book.save('example.xls');
Book.Free;
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.xls");
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.xls");
Book book = new BinBook();
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.xls");
var
Book: TBook;
Sheet: TSheet;
Font: TFont;
Format: TFormat;
begin
Book := TBinBook.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.xls');
Book.Free;
end;