ExcelFile.SaveForHashing Method
Overloads
ExcelFile.SaveForHashing(Stream)
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(Stream, TExcludedRecords) with the excludedRecords parameter set to TExcludedRecords.All. Use SaveForHashing(Stream, TExcludedRecords) 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
Namespace: FlexCel.Core
public void SaveForHashing(Stream aStream)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
aStream | Stream | 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:
static byte[] GetHash(string FileName)
{
XlsFile xls = new XlsFile(FileName);
using (System.Security.Cryptography.SHA256CryptoServiceProvider hasher = new System.Security.Cryptography.SHA256CryptoServiceProvider())
{
using (MemoryStream ms = new MemoryStream())
{
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;
return hasher.ComputeHash(ms); //Compute the hash of the file.
}
}
}
See also
ExcelFile.SaveForHashing(Stream, TExcludedRecords)
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
Namespace: FlexCel.Core
public abstract void SaveForHashing(Stream aStream, TExcludedRecords excludedRecords)
Parameters
<-> | Parameter | Type | Description |
---|---|---|---|
aStream | Stream | Stream where the file will be saved. You will probably want to hash this stream to store the corresponding hash. | |
excludedRecords | 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:
static byte[] GetHash(string FileName)
{
XlsFile xls = new XlsFile(FileName);
using (System.Security.Cryptography.SHA256CryptoServiceProvider hasher = new System.Security.Cryptography.SHA256CryptoServiceProvider())
{
using (MemoryStream ms = new MemoryStream())
{
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;
return hasher.ComputeHash(ms); //Compute the hash of the file.
}
}
}