2009-08-07 14:16:17 +00:00
|
|
|
{
|
2014-06-26 07:15:51 +00:00
|
|
|
excel8read.lpr
|
2009-08-07 14:16:17 +00:00
|
|
|
|
|
|
|
Demonstrates how to read an Excel 8.x file using the fpspreadsheet library
|
|
|
|
|
|
|
|
AUTHORS: Felipe Monteiro de Carvalho
|
|
|
|
}
|
|
|
|
program excel8read;
|
|
|
|
|
|
|
|
{$mode delphi}{$H+}
|
|
|
|
|
|
|
|
uses
|
2015-03-05 10:35:32 +00:00
|
|
|
Classes, SysUtils, LazUTF8, fpsTypes, fpspreadsheet, xlsbiff8,
|
2014-08-29 15:02:00 +00:00
|
|
|
fpsutils;
|
2009-08-07 14:16:17 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
InputFilename: string;
|
|
|
|
MyDir: string;
|
|
|
|
i: Integer;
|
|
|
|
CurCell: PCell;
|
2009-09-01 16:47:45 +00:00
|
|
|
|
2014-05-14 15:24:02 +00:00
|
|
|
{$R *.res}
|
|
|
|
|
2009-08-07 14:16:17 +00:00
|
|
|
begin
|
|
|
|
// Open the input file
|
|
|
|
MyDir := ExtractFilePath(ParamStr(0));
|
|
|
|
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 excel8write first.');
|
|
|
|
Halt;
|
|
|
|
end;
|
2009-08-07 14:16:17 +00:00
|
|
|
WriteLn('Opening input file ', InputFilename);
|
|
|
|
|
|
|
|
// Create the spreadsheet
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
2015-05-31 20:28:25 +00:00
|
|
|
try
|
|
|
|
MyWorkbook.Options := MyWorkbook.Options + [boReadFormulas];
|
|
|
|
MyWorkbook.ReadFromFile(InputFilename, sfExcel8);
|
|
|
|
MyWorksheet := MyWorkbook.GetFirstWorksheet;
|
2014-05-23 13:16:01 +00:00
|
|
|
|
2015-05-31 20:28:25 +00:00
|
|
|
// Write all cells with contents to the console
|
|
|
|
WriteLn('');
|
|
|
|
WriteLn('Contents of the first worksheet of the file:');
|
|
|
|
WriteLn('');
|
2009-08-07 14:16:17 +00:00
|
|
|
|
2015-05-31 20:28:25 +00:00
|
|
|
for CurCell in MyWorksheet.Cells do
|
|
|
|
begin
|
|
|
|
Write('Row: ', CurCell^.Row,
|
|
|
|
' Col: ', CurCell^.Col, ' Value: ',
|
2015-08-12 20:07:24 +00:00
|
|
|
UTF8ToConsole(MyWorkSheet.ReadAsText(CurCell^.Row,
|
2015-05-31 20:28:25 +00:00
|
|
|
CurCell^.Col))
|
|
|
|
);
|
|
|
|
if HasFormula(CurCell) then
|
|
|
|
WriteLn(' Formula: ', MyWorkSheet.ReadFormulaAsString(CurCell))
|
|
|
|
else
|
|
|
|
WriteLn;
|
|
|
|
end;
|
2009-08-07 14:16:17 +00:00
|
|
|
|
2015-05-31 20:28:25 +00:00
|
|
|
finally
|
|
|
|
// Finalization
|
|
|
|
MyWorkbook.Free;
|
2009-08-07 14:16:17 +00:00
|
|
|
end;
|
2016-03-18 19:50:40 +00:00
|
|
|
|
2021-04-15 12:19:42 +00:00
|
|
|
if ParamCount = 0 then
|
|
|
|
begin
|
|
|
|
{$IFDEF WINDOWS}
|
|
|
|
WriteLn;
|
|
|
|
WriteLn('Press ENTER to quit...');
|
|
|
|
ReadLn;
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
2009-08-07 14:16:17 +00:00
|
|
|
end.
|
|
|
|
|