fpspreadsheet: Add stream version of TsWorkbook.GetFormatFromFileHeader (patch by Craig Peterson, issue #28465)

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4241 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2015-08-03 19:34:46 +00:00
parent 6a6f290fda
commit bfd19235c6

View File

@@ -637,7 +637,9 @@ type
destructor Destroy; override;
class function GetFormatFromFileHeader(const AFileName: TFileName;
out SheetType: TsSpreadsheetFormat): Boolean;
out SheetType: TsSpreadsheetFormat): Boolean; overload;
class function GetFormatFromFileHeader(AStream: TStream;
out SheetType: TsSpreadsheetFormat): Boolean; overload;
function CreateSpreadReader(AFormat: TsSpreadsheetFormat): TsBasicSpreadReader;
function CreateSpreadWriter(AFormat: TsSpreadsheetFormat): TsBasicSpreadWriter;
procedure ReadFromFile(AFileName: string; AFormat: TsSpreadsheetFormat); overload;
@@ -6504,6 +6506,25 @@ end;
-------------------------------------------------------------------------------}
class function TsWorkbook.GetFormatFromFileHeader(const AFileName: TFileName;
out SheetType: TsSpreadsheetFormat): Boolean;
var
stream: TStream;
begin
stream := TFileStream.Create(AFileName, fmOpenRead + fmShareDenyNone);
try
Result := GetFormatFromFileHeader(stream, SheetType)
finally
stream.Free;
end;
end;
{@@ ----------------------------------------------------------------------------
Helper method for determining the spreadsheet type. Read the first few bytes
of a stream and determines the spreadsheet type from the characteristic
signature. Only implemented for xls where several file types have the same
extension.
-------------------------------------------------------------------------------}
class function TsWorkbook.GetFormatFromFileHeader(AStream: TStream;
out SheetType: TsSpreadsheetFormat): Boolean; overload;
const
BIFF2_HEADER: array[0..3] of byte = (
$09,$00, $04,$00); // they are common to all BIFF2 files that I've seen
@@ -6526,15 +6547,15 @@ const
var
buf: packed array[0..7] of byte = (0,0,0,0,0,0,0,0);
stream: TStream;
i: Integer;
ok: Boolean;
begin
Result := false;
stream := TFileStream.Create(AFileName, fmOpenRead + fmShareDenyNone);
try
if AStream = nil then
exit;
// Read first 8 bytes
stream.ReadBuffer(buf, 8);
AStream.ReadBuffer(buf, 8);
// Check for Excel 2
ok := true;
@@ -6558,20 +6579,16 @@ begin
// Now we know that the file is a Microsoft compound document.
// We check for Excel 5 in which the stream is named "Book"
if ValidOLEStream(stream, 'Book') then begin
if ValidOLEStream(AStream, 'Book') then begin
SheetType := sfExcel5;
exit(True);
end;
// Now we check for Excel 8 which names the stream "Workbook"
if ValidOLEStream(stream, 'Workbook') then begin
if ValidOLEStream(AStream, 'Workbook') then begin
SheetType := sfExcel8;
exit(True);
end;
finally
stream.Free;
end;
end;
{@@ ----------------------------------------------------------------------------