From 00dfa0a45ea49dc54c56bb19a33d5a136710130d Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Fri, 18 Feb 2022 18:17:14 +0000 Subject: [PATCH] fpspreadsheet: minor refactoring of crypto example. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8199 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../ooxml_decrypt_and_read.pas | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/components/fpspreadsheet/examples/read_write/ooxmldemo_crypto/ooxml_decrypt_and_read.pas b/components/fpspreadsheet/examples/read_write/ooxmldemo_crypto/ooxml_decrypt_and_read.pas index 4e12df5df..852c903bc 100644 --- a/components/fpspreadsheet/examples/read_write/ooxmldemo_crypto/ooxml_decrypt_and_read.pas +++ b/components/fpspreadsheet/examples/read_write/ooxmldemo_crypto/ooxml_decrypt_and_read.pas @@ -44,34 +44,41 @@ begin end; end; - if not FileExists(InputFileName) then begin - WriteLn('Input file ', InputFileName, ' does not exist. Please run opendocwrite first.'); - Halt; - end; - WriteLn('Opening input file ', InputFilename); + if FileExists(InputFileName) then + begin + WriteLn('Opening input file ', InputFilename); - // Create the spreadsheet - MyWorkbook := TsWorkbook.Create; + // Create the spreadsheet + MyWorkbook := TsWorkbook.Create; + MyWorkbook.Options := MyWorkbook.Options + [boReadFormulas]; + try + try + MyWorkbook.ReadFromFile(InputFilename, sfidOOXML_crypto, password); - MyWorkbook.Options := MyWorkbook.Options + [boReadFormulas]; - MyWorkbook.ReadFromFile(InputFilename, sfidOOXML_crypto, password); + MyWorksheet := MyWorkbook.GetFirstWorksheet; - MyWorksheet := MyWorkbook.GetFirstWorksheet; + // Write all cells with contents to the console + WriteLn(''); + WriteLn('Contents of the first worksheet of the file:'); + WriteLn(''); - // Write all cells with contents to the console - WriteLn(''); - WriteLn('Contents of the first worksheet of the file:'); - WriteLn(''); + for cell in MyWorksheet.Cells do + WriteLn( + 'Row: ', cell^.Row, + ' Col: ', cell^.Col, + ' Value: ', UTF8ToConsole(MyWorkSheet.ReadAsText(cell^.Row, cell^.Col)) + ); + except + WriteLn('Error opening file ', InputFileName); + end; - for cell in MyWorksheet.Cells do - WriteLn( - 'Row: ', cell^.Row, - ' Col: ', cell^.Col, - ' Value: ', UTF8ToConsole(MyWorkSheet.ReadAsText(cell^.Row, cell^.Col)) - ); - - // Finalization - MyWorkbook.Free; + finally + // Finalization + MyWorkbook.Free; + end; + end + else + WriteLn('Input file ', InputFileName, ' does not exist.'); if ParamCount = 0 then begin