2009-01-10 21:47:59 +00:00
|
|
|
{
|
2014-06-26 07:15:51 +00:00
|
|
|
excel5read.lpr
|
2009-01-10 21:47:59 +00:00
|
|
|
|
|
|
|
Demonstrates how to read an Excel 5.x file using the fpspreadsheet library
|
|
|
|
|
|
|
|
AUTHORS: Felipe Monteiro de Carvalho
|
|
|
|
}
|
|
|
|
program excel5read;
|
|
|
|
|
|
|
|
{$mode delphi}{$H+}
|
|
|
|
|
|
|
|
uses
|
2014-08-29 15:32:43 +00:00
|
|
|
Classes, SysUtils, fpspreadsheet, xlsbiff5;
|
2009-01-10 21:47:59 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
InputFilename: string;
|
|
|
|
MyDir: string;
|
|
|
|
i: Integer;
|
|
|
|
CurCell: PCell;
|
|
|
|
begin
|
|
|
|
// Open the input file
|
|
|
|
MyDir := ExtractFilePath(ParamStr(0));
|
2009-02-03 09:41:17 +00:00
|
|
|
InputFileName := MyDir + 'test.xls';
|
2014-06-18 08:07:00 +00:00
|
|
|
if not FileExists(InputFileName) then begin
|
|
|
|
WriteLn('Input file ', InputFileName, ' does not exist. Please run excel5write first.');
|
|
|
|
Halt;
|
|
|
|
end;
|
2009-01-10 21:47:59 +00:00
|
|
|
WriteLn('Opening input file ', InputFilename);
|
|
|
|
|
|
|
|
// Create the spreadsheet
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
2014-08-30 18:03:22 +00:00
|
|
|
MyWorkbook.Options := MyWorkbook.Options + [boReadFormulas];
|
2009-01-10 21:47:59 +00:00
|
|
|
MyWorkbook.ReadFromFile(InputFilename, sfExcel5);
|
|
|
|
|
|
|
|
MyWorksheet := MyWorkbook.GetFirstWorksheet;
|
|
|
|
|
|
|
|
// Write all cells with contents to the console
|
|
|
|
WriteLn('');
|
|
|
|
WriteLn('Contents of the first worksheet of the file:');
|
|
|
|
WriteLn('');
|
|
|
|
|
2009-11-08 19:21:23 +00:00
|
|
|
CurCell := MyWorkSheet.GetFirstCell();
|
2009-01-10 22:58:00 +00:00
|
|
|
for i := 0 to MyWorksheet.GetCellCount - 1 do
|
2009-01-10 21:47:59 +00:00
|
|
|
begin
|
2014-08-30 18:03:22 +00:00
|
|
|
Write('Row: ', CurCell^.Row,
|
2009-02-03 09:41:17 +00:00
|
|
|
' Col: ', CurCell^.Col, ' Value: ',
|
2014-08-30 18:03:22 +00:00
|
|
|
UTF8ToAnsi(MyWorkSheet.ReadAsUTF8Text(CurCell^.Row, CurCell^.Col)));
|
|
|
|
if HasFormula(CurCell) then
|
|
|
|
Write(' - Formula: ', CurCell^.FormulaValue);
|
|
|
|
WriteLn;
|
2009-11-08 19:21:23 +00:00
|
|
|
CurCell := MyWorkSheet.GetNextCell();
|
2009-01-10 21:47:59 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
// Finalization
|
|
|
|
MyWorkbook.Free;
|
|
|
|
end.
|
|
|
|
|