npoi icon indicating copy to clipboard operation
npoi copied to clipboard

WriteProtectWorkbook record in HSSFWorkbook doesn't work

Open CloudyWing opened this issue 2 years ago • 1 comments

My English is very bad, so I use google translation, if the description is not clear, please forgive me>

If directly new HSSFWorkbook(),WriteProtectWorkbook is not workig,The reason is as follows.

The following code is from InternalWorkbook.cs

public static InternalWorkbook CreateWorkbook() {
    ......
    list.Add(InterfaceEndRecord.Instance);
    list.Add(CreateWriteAccess());
    ......
}
......
public void WriteProtectWorkbook(string password, string username) {
    FileSharingRecord fileSharing = FileSharing;
    WriteAccessRecord writeAccessRecord = WriteAccess;
    fileSharing.ReadOnly = 1;
     fileSharing.Password = (short)CryptoFunctions.CreateXorVerifier1(password);
    fileSharing.Username = username;
    writeAccessRecord.Username = username;
}

When 「CreateWriteAccess()」 is executed, it is not pointed to field「writeAccess」, and in 「WriteProtectWorkbook()」, the call of 「FileSharing」 is placed in front of 「WriteAccess」, cause when calls Property 「WriteAccess」, because field 「writeAccess」 is null, WriteAccessRecord is created repeatedly and added to InterfaceEndRecord.

The normal Record order should be as follows. [INTERFACEEND/] [WriteAccess] [FILESHARING]

But the order of this situation is as follow. [INTERFACEEND/] [WriteAccess] [WriteAccess] [FILESHARING]

Although I don’t know the reason, apparently two [WriteAccess] in [INTERFACEEND/] and [FILESHARING] will cause "WriteProtectWorkbook" to not work properly. For the current version to work normally, you must call 「WriteAccess」 before calling 「WriteProtectWorkbook()」

_ = workbook.InternalWorkbook.WriteAccess;
workbook.WriteProtectWorkbook("Your Password", "");

The orderof Records generated at this time is as follows. [INTERFACEEND/] [WriteAccess] [FILESHARING] [WriteAccess]

Although there is still an extra [WriteAccess], it can work at least in version 2.5.5, and the previous version has not yet been tested.

CloudyWing avatar Dec 18 '21 08:12 CloudyWing

Looks you fully understand what's going on. Can you provide a PR?

tonyqus avatar Feb 04 '22 00:02 tonyqus