POI Excel : Making a read only final file prevent edit/select

Excel allows to make the sheet of a workbook to be read only, with the possibility to fine tune the locking options like for example :

  • Locking cell edition
  • Locking cell selection
  • Locking cell format
  • Locking cell insertion

You can get the list of locking options from the Excel security configuration dialog as you can see in the screenshot provided below :

You will then have to provide a password for the locking feature

You can also achieve this easily with POI in a few easy steps as shown in the code below :



private static void lockAll(Sheet s, String password){
   
    // cast the sheet to the appropriate type
    XSSFSheet sheet = ((XSSFSheet)s);

     //protect the sheet with a password
     sheet.protectSheet(password);
     //enable the locking features
     sheet.enableLocking();

     // fine tune the locking options (in this example we are blocking all operations on the cells: select, edit, etc.)
     sheet.lockSelectLockedCells(true);
     sheet.lockSelectUnlockedCells(true);
    

}

No comments:

Post a Comment