2014-07-24 22:22:26 +00:00
|
|
|
{
|
|
|
|
ooxmlread.lpr
|
|
|
|
|
|
|
|
Demonstrates how to read an Excel xlsx file using the fpspreadsheet library
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
program ooxmlread;
|
|
|
|
|
|
|
|
{$mode delphi}{$H+}
|
|
|
|
|
|
|
|
uses
|
2015-03-05 10:35:32 +00:00
|
|
|
Classes, SysUtils, LazUTF8, fpstypes, fpspreadsheet, xlsxooxml; //fpsallformats;
|
2014-07-24 22:22:26 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
InputFilename: String;
|
|
|
|
MyDir: string;
|
|
|
|
cell: PCell;
|
|
|
|
i: Integer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
MyDir := ExtractFilePath(ParamStr(0));
|
|
|
|
|
|
|
|
// Open the input file
|
|
|
|
MyDir := ExtractFilePath(ParamStr(0));
|
2016-03-18 19:50:40 +00:00
|
|
|
InputFileName := MyDir + 'test.xlsx';
|
2014-07-24 22:22:26 +00:00
|
|
|
if not FileExists(InputFileName) then begin
|
|
|
|
WriteLn('Input file ', InputFileName, ' does not exist. Please run opendocwrite first.');
|
|
|
|
Halt;
|
|
|
|
end;
|
|
|
|
WriteLn('Opening input file ', InputFilename);
|
|
|
|
|
|
|
|
// Create the spreadsheet
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
|
|
|
|
2014-08-30 18:03:22 +00:00
|
|
|
MyWorkbook.Options := MyWorkbook.Options + [boReadFormulas];
|
2014-07-24 22:22:26 +00:00
|
|
|
MyWorkbook.ReadFromFile(InputFilename, sfOOXML);
|
|
|
|
|
|
|
|
MyWorksheet := MyWorkbook.GetFirstWorksheet;
|
|
|
|
|
|
|
|
// Write all cells with contents to the console
|
|
|
|
WriteLn('');
|
|
|
|
WriteLn('Contents of the first worksheet of the file:');
|
|
|
|
WriteLn('');
|
|
|
|
|
2015-03-05 10:35:32 +00:00
|
|
|
for cell in MyWorksheet.Cells do
|
|
|
|
WriteLn(
|
|
|
|
'Row: ', cell^.Row,
|
|
|
|
' Col: ', cell^.Col,
|
2016-03-18 19:50:40 +00:00
|
|
|
' Value: ', UTF8ToConsole(MyWorkSheet.ReadAsText(cell^.Row, cell^.Col))
|
2014-07-24 22:22:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Finalization
|
|
|
|
MyWorkbook.Free;
|
2016-03-18 19:50:40 +00:00
|
|
|
|
|
|
|
{$ifdef WINDOWS}
|
|
|
|
WriteLn;
|
|
|
|
WriteLn('Press ENTER to quit...');
|
|
|
|
ReadLn;
|
|
|
|
{$ENDIF}
|
2014-07-24 22:22:26 +00:00
|
|
|
end.
|
|
|
|
|