Table of Contents

TExcelFile.SaveForHashing Method

Overloads

TExcelFile.SaveForHashing(TStream)

This method will save the file in a format that will remain the same if the file is not modified. Normal xls files contain TimeStamp fields that might be modified when the file is downloaded or just copied.

While you will not be able to load the file saved, you might use this method to create a hash of a file and compare it to others to know if something changed.

This overload will not save cell selections or the active sheet, and it is equivalent to calling SaveForHashing(TStream, TExcludedRecordSet) with the excludedRecords parameter set to TExcludedRecords.All. Use SaveForHashing(TStream, TExcludedRecordSet) for more control on which records to exclude.

Remarks

This method will not save the file in any readable format, and the file format might change between FlexCel versions. The only thing it guarantees is that the hashes for 2 identical xls files will be the same, for the same FlexCel version. Once you upgrade version, hashes might have to be rebuilt.

Also note that this method is useful to detect changes when the file is not edited in Excel. If you open the file in Excel and save it again, Excel will change a lot of reserved bits, and the files will be too different for this method to have the same hashes. This is only to detect changes when copying or downloading an xls file. If you want to compare just cell contents, you might compare the files saved as CSV.

Syntax

Unit: FlexCel.Core

procedure TExcelFile.SaveForHashing(const aStream: TStream); overload;

Parameters

<-> Parameter Type Description
const aStream TStream Stream where the file will be saved. You will probably want to hash this stream to store the corresponding hash.

Examples

The following method will calculate the hash for an existing file:

function GetHash(const FileName: String): ByteArray;
var
  xls: TXlsFile;
  ms: TMemoryStream;
begin
  xls := TXlsFile.Create(FileName);
  try
    ms := TMemoryStream.Create;
    try
      xls.SaveForHashing(ms);  //Save the file in a format without timestamps
       //or other data that might change from save to save.
       //Note that the saved file is invalid, but we only care about its hash.
      ms.Position := 0;
      exit(ComputeHash(ms));  //ComputeHash is some function that computes some hash like an MD5 or SH256 on the data.
    finally
      ms.Free;
    end;
  finally
    xls.Free;
  end;
end;

See also

TExcelFile.SaveForHashing(TStream, TExcludedRecordSet)

This method will save the file in a format that will remain the same if the file is not modified. Normal xls files contain TimeStamp fields that might be modified when the file is downloaded or just copied.

While you will not be able to load the file saved, you might use this method to create a hash of a file and compare it to others to know if something changed.

Remarks

This method will not save the file in any readable format, and the file format might change between FlexCel versions. The only thing it guarantees is that the hashes for 2 identical xls files will be the same, for the same FlexCel version. Once you upgrade version, hashes might have to be rebuilt.

Also note that this method is useful to detect changes when the file is not edited in Excel. If you open the file in Excel and save it again, Excel will change a lot of reserved bits, and the files will be too different for this method to have the same hashes. This is only to detect changes when copying or downloading an xls file. If you want to compare just cell contents, you might compare the files saved as CSV.

Syntax

Unit: FlexCel.Core

procedure TExcelFile.SaveForHashing(const aStream: TStream; const excludedRecords: Set of TExcludedRecords); overload; virtual; abstract;

Parameters

<-> Parameter Type Description
const aStream TStream Stream where the file will be saved. You will probably want to hash this stream to store the corresponding hash.
const excludedRecords Set of TExcludedRecords A list with all the records you don't wish to include in the saved file (like for example cell selection). You will normally will want to specify TExcludedRecords.All here, but you can OR different members of the TExcludedRecords enumerations for more control on what is saved.

Examples

The following method will calculate the hash for an existing file:

function GetHash(const FileName: String): ByteArray;
var
  xls: TXlsFile;
  ms: TMemoryStream;
begin
  xls := TXlsFile.Create(FileName);
  try
    ms := TMemoryStream.Create;
    try
      xls.SaveForHashing(ms);  //Save the file in a format without timestamps
       //or other data that might change from save to save.
       //Note that the saved file is invalid, but we only care about its hash.
      ms.Position := 0;
      exit(ComputeHash(ms));  //ComputeHash is some function that computes some hash like an MD5 or SH256 on the data.
    finally
      ms.Free;
    end;
  finally
    xls.Free;
  end;
end;

See also