Sunday, June 15, 2008

Reading Excel Spreadsheets from Delphi

The basic code looks like:

var
XlsObj : OleVariant;
BookObj : OleVariant;
SheetObj : OleVariant;

begin
XlsObj := CreateOleObject('Excel.Application');
XlsObj.DisplayAlerts := false; // prevent warnings from popping up
BookObj := XlsObj.Workbooks.Open(XlsFileName,,true); // readonly
SheetObj := BookObj.Sheets[SheetName];
do_some_work;
BookObj.Close(false);
end;