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);
    

}

Popular posts from this blog

Multi-hop ssh tunnel - howto : Creating a SSH tunnel with port forwarding between multiple hosts

Spring boot and spring data jpa tutorial - A sample application using spring 4.0 spring boot and JPA (part 2/2)

Maven build number, versioning your projects builds automatically