Difference between revisions of "User talk:Garydale"
Sekelsenmat (talk | contribs) (New page: ==FPSpreadsheet code to open a file== Hello, there is already a method to read a file in fpspreadsheet regardless of the format: ReadFile If you don't pass any file format it will attemp...) |
(response to Felipe with code to replace fpspreadsheet code.) |
||
Line 4: | Line 4: | ||
If you don't pass any file format it will attempt to auto-detect the format. At the moment it doesn't differentiate between various XLS formats, just supposes biff8, but it would be better to improve it to do this then to add new code to do the same thing is each app using fpspreadsheet. --[[User:Sekelsenmat|Felipe Monteiro de Carvalho]] 11:13, 2 August 2011 (CEST) | If you don't pass any file format it will attempt to auto-detect the format. At the moment it doesn't differentiate between various XLS formats, just supposes biff8, but it would be better to improve it to do this then to add new code to do the same thing is each app using fpspreadsheet. --[[User:Sekelsenmat|Felipe Monteiro de Carvalho]] 11:13, 2 August 2011 (CEST) | ||
+ | |||
+ | |||
+ | Hello Felipe: Please feel free to use my code to improve the ReadFromFile method. I looked at the code and my routine should be almost a drop-in replacement. Here's my take at it: | ||
+ | |||
+ | procedure TsWorkbook.ReadFromFile(AFileName: string); | ||
+ | var SheetType: TsSpreadsheetFormat; | ||
+ | valid: Boolean; | ||
+ | |||
+ | procedure getFileType(const FileToUpdate: TFileName; var SheetType: TsSpreadsheetFormat; var valid: Boolean); | ||
+ | var suffix: String; | ||
+ | begin | ||
+ | valid := True; | ||
+ | suffix := ExtractFileExt(FileToUpdate); | ||
+ | if suffix = STR_EXCEL_EXTENSION then | ||
+ | SheetType := sfExcel8 | ||
+ | else if suffix = STR_OPENDOCUMENT_CALC_EXTENSION then | ||
+ | SheetType := sfOpenDocument | ||
+ | else if suffix = STR_OOXML_EXCEL_EXTENSION then | ||
+ | SheetType := sfOOXML | ||
+ | else if suffix = '.csv' then | ||
+ | SheetType := sfCSV | ||
+ | else | ||
+ | valid := False; | ||
+ | end; | ||
+ | |||
+ | begin | ||
+ | getFileType(AFileName, SheetType, valid); | ||
+ | if valid then begin | ||
+ | try | ||
+ | ReadFromFile(AFileName, SheetType); | ||
+ | except | ||
+ | if SheetType = sfExcel8 then begin | ||
+ | repeat | ||
+ | try | ||
+ | SheetType := Pred(SheetType); | ||
+ | ReadFromFile(AFileName, SheetType); | ||
+ | valid := True; | ||
+ | except | ||
+ | valid := False | ||
+ | end; | ||
+ | until valid or (SheetType = sfExcel2); | ||
+ | end; | ||
+ | end; | ||
+ | end; | ||
+ | end; | ||
+ | |||
+ | |||
+ | NOTE: I prefer Wirth's later syntax enhancements to his original Algol/Pascal work. In particular, I like his switch to statement lists within closed constructs rather than begin/end blocks. As such, I tend to use begin/end even when they aren't needed, simply to allow statements to be properly closed. |
Revision as of 11:39, 3 August 2011
FPSpreadsheet code to open a file
Hello, there is already a method to read a file in fpspreadsheet regardless of the format: ReadFile
If you don't pass any file format it will attempt to auto-detect the format. At the moment it doesn't differentiate between various XLS formats, just supposes biff8, but it would be better to improve it to do this then to add new code to do the same thing is each app using fpspreadsheet. --Felipe Monteiro de Carvalho 11:13, 2 August 2011 (CEST)
Hello Felipe: Please feel free to use my code to improve the ReadFromFile method. I looked at the code and my routine should be almost a drop-in replacement. Here's my take at it:
procedure TsWorkbook.ReadFromFile(AFileName: string); var SheetType: TsSpreadsheetFormat;
valid: Boolean;
procedure getFileType(const FileToUpdate: TFileName; var SheetType: TsSpreadsheetFormat; var valid: Boolean); var suffix: String; begin valid := True; suffix := ExtractFileExt(FileToUpdate); if suffix = STR_EXCEL_EXTENSION then SheetType := sfExcel8 else if suffix = STR_OPENDOCUMENT_CALC_EXTENSION then SheetType := sfOpenDocument else if suffix = STR_OOXML_EXCEL_EXTENSION then SheetType := sfOOXML else if suffix = '.csv' then SheetType := sfCSV else valid := False; end;
begin
getFileType(AFileName, SheetType, valid); if valid then begin try ReadFromFile(AFileName, SheetType); except if SheetType = sfExcel8 then begin repeat try SheetType := Pred(SheetType); ReadFromFile(AFileName, SheetType); valid := True; except
valid := False
end; until valid or (SheetType = sfExcel2); end; end; end;
end;
NOTE: I prefer Wirth's later syntax enhancements to his original Algol/Pascal work. In particular, I like his switch to statement lists within closed constructs rather than begin/end blocks. As such, I tend to use begin/end even when they aren't needed, simply to allow statements to be properly closed.