Structure lock in HSSFWorkbook possible?

390 Views Asked by At

In the POI XSSFWorkbook, the methods lockStructure and lockWindows exist with which an Excel Workbook can be protected. (Function in excel menue under "Review->Protect Workbook...")
The function of these methods is analogous to the Workbook.protect method in Excel: https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.protect

Is there any way to enable this protection in a HSSFWorkbook?

I have already tried the writeProtectWorkbook method from HSSFWorkbook, but it does not do the same as the methods in the XSSFWorkbook. The method writeProtectWorkbook sets an open password.

Probably I have to modify the "worksheet protection block" which is described in section 5.82 here on page 200: http://www.openoffice.org/sc/excelfileformat.pdf

Does anyone know how I can get this block with POI?
I use poi:5.0.0.

1

There are 1 best solutions below

0
Lars On BEST ANSWER

I found out myself how to enable Workbook Protection. In the InternalWorkbook there is a list of records. These contain the record types ProtectRecord, WindowProtectRecord and PasswordRecord. With these you can set the Workbook Protection:

List<Record> records = ((HSSFWorkbook) wb).getInternalWorkbook().getRecords();
for (Record record : records) {
    if (record instanceof ProtectRecord) {
        ((ProtectRecord) record).setProtect(true);
    } else if (record instanceof WindowProtectRecord) {
        ((WindowProtectRecord) record).setProtect(true);
    } else if (record instanceof PasswordRecord) {
        ((PasswordRecord) record).setPassword((short) CryptoFunctions.createXorVerifier1("password"));
    }
}