How to read and write Excel files in Delphi

LibXL can be used in Delphi projects. It contains Delphi classes for seamless integration. It's enough to include LibXL.pas unit in your project and put libxl.dll near. Microsoft Excel isn't required. See examples below.

Writing a simple Excel file

var
  Book: TBook;
  Sheet: TSheet;
  dateFormat: TFormat;
begin
  Book := TBook.Create;
  Sheet := Book.addSheet('Sheet1');

  Sheet.writeStr(2, 1, 'Hello, World !');
  Sheet.writeNum(3, 1, 1000);

  dateFormat := Book.addFormat();
  dateFormat.setNumFormat(NUMFORMAT_DATE);
  Sheet.writeNum(4, 1, Book.datePack(2008, 4, 29), dateFormat);
  Sheet.setCol(1, 1, 12);

  Book.save('example.xls');
  Book.Free;
end;

Reading an Excel file

var
  Book: TBook;
  Sheet: TSheet;
  d: double;
  s: string;
begin
  Book := TBook.Create;
  Book.load('example.xls');
  Sheet := Book.getSheet(0);

  s := Sheet.readStr(2, 1);
  d := Sheet.readNum(3, 1);

  ShowMessage(s + #13#10 + FloatToStr(d));
  Book.Free;
end;
Home  Download now  Purchase now