2014-10-10 09:10:43 +00:00
|
|
|
{
|
|
|
|
csvread.dpr
|
|
|
|
|
|
|
|
Demonstrates how to read a CSV file using the fpspreadsheet library
|
|
|
|
}
|
|
|
|
|
2015-05-31 20:28:25 +00:00
|
|
|
program csvread;
|
2014-10-10 09:10:43 +00:00
|
|
|
|
|
|
|
{$mode delphi}{$H+}
|
|
|
|
|
|
|
|
uses
|
2015-05-31 20:28:25 +00:00
|
|
|
Classes, SysUtils, LazUTF8, fpstypes, fpsutils, fpspreadsheet, fpscsv;
|
2014-10-10 09:10:43 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
InputFilename: string;
|
|
|
|
MyDir: string;
|
|
|
|
i: Integer;
|
|
|
|
CurCell: PCell;
|
|
|
|
begin
|
|
|
|
// Open the input file
|
|
|
|
MyDir := ExtractFilePath(ParamStr(0));
|
|
|
|
InputFileName := MyDir + 'test' + STR_COMMA_SEPARATED_EXTENSION;
|
|
|
|
if not FileExists(InputFileName) then begin
|
2015-08-01 22:11:44 +00:00
|
|
|
WriteLn('Input file ', InputFileName, ' does not exist. Please run csvwrite first.');
|
2014-10-10 09:10:43 +00:00
|
|
|
Halt;
|
|
|
|
end;
|
|
|
|
|
|
|
|
WriteLn('Opening input file ', InputFilename);
|
|
|
|
|
2014-10-24 10:46:04 +00:00
|
|
|
// Tab-delimited
|
2014-10-13 22:28:38 +00:00
|
|
|
CSVParams.Delimiter := #9;
|
|
|
|
CSVParams.QuoteChar := '''';
|
2014-10-10 09:10:43 +00:00
|
|
|
|
|
|
|
// Create the spreadsheet
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
2015-05-31 20:28:25 +00:00
|
|
|
try
|
|
|
|
MyWorkbook.Options := MyWorkbook.Options + [boReadFormulas];
|
|
|
|
MyWorkbook.ReadFromFile(InputFilename, sfCSV);
|
|
|
|
|
|
|
|
MyWorksheet := MyWorkbook.GetFirstWorksheet;
|
|
|
|
|
|
|
|
// Write all cells with contents to the console
|
|
|
|
WriteLn('');
|
|
|
|
WriteLn('Contents of the first worksheet of the file:');
|
|
|
|
WriteLn('');
|
2014-10-10 09:10:43 +00:00
|
|
|
|
2015-05-31 20:28:25 +00:00
|
|
|
for CurCell in MyWorksheet.Cells do
|
|
|
|
begin
|
|
|
|
if HasFormula(CurCell) then
|
|
|
|
WriteLn('Row: ', CurCell^.Row, ' Col: ', CurCell^.Col, ' Formula: ', MyWorksheet.ReadFormulaAsString(CurCell))
|
|
|
|
else
|
|
|
|
WriteLn(
|
|
|
|
'Row: ', CurCell^.Row,
|
|
|
|
' Col: ', CurCell^.Col,
|
2016-03-18 19:50:40 +00:00
|
|
|
' Value: ', UTF8ToConsole(MyWorkSheet.ReadAsText(CurCell^.Row, CurCell^.Col))
|
2015-05-31 20:28:25 +00:00
|
|
|
);
|
|
|
|
end;
|
|
|
|
|
|
|
|
finally
|
|
|
|
// Finalization
|
|
|
|
MyWorkbook.Free;
|
|
|
|
end;
|
2016-03-18 19:50:40 +00:00
|
|
|
|
|
|
|
{$ifdef WINDOWS}
|
2021-04-15 12:19:42 +00:00
|
|
|
if ParamCount = 0 then
|
|
|
|
begin
|
|
|
|
WriteLn;
|
|
|
|
WriteLn('Press ENTER to quit...');
|
|
|
|
ReadLn;
|
|
|
|
end;
|
2016-03-18 19:50:40 +00:00
|
|
|
{$ENDIF}
|
2014-10-10 09:10:43 +00:00
|
|
|
end.
|
|
|
|
|