Apache POI - How to protect sheet with options?
Asked Answered
R

3

18

I'm using the Apache POI to generate an Excel File (2007). What I want is to protect the sheet, but with some options enabled. By options I mean the check box list when you try to protect the sheet in the Excel application (under the label "Allow all users of this worksheet to:"). Specifically, I want to enable "Select locked/unlocked cells", "Format Column", "Sort", and "Allow Autofilter". Thank you very much! :D

Ronnyronsard answered 5/2, 2013 at 6:17 Comment(2)
i dont think beyond sheet.getSettings() set() methods, you can do anything.Solvolysis
sheet.getSettings() is from JExcel, not Apache POI, I think.Ronnyronsard
H
18

In Apache POI 3.9 you can use XSSF Sheet protection by enabling lock functions. even you can leave behind few excel objects unlocked as in case below I left out excel object (i.e text box) unlocked and rest are locked.

 private static void lockAll(Sheet s, XSSFWorkbook workbookx){
    String password= "abcd";
    byte[] pwdBytes = null;
    try {
        pwdBytes  = Hex.decodeHex(password.toCharArray());
    } catch (DecoderException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    XSSFSheet sheet = ((XSSFSheet)s);
    removePivot(s,workbookx);
    sheet.lockDeleteColumns();
    sheet.lockDeleteRows();
    sheet.lockFormatCells();
    sheet.lockFormatColumns();
    sheet.lockFormatRows();
    sheet.lockInsertColumns();
    sheet.lockInsertRows();
    sheet.getCTWorksheet().getSheetProtection().setPassword(pwdBytes);
    for(byte pwdChar :pwdBytes){
        System.out.println(">>> Sheet protected with '" + pwdChar + "'");
    }
    sheet.enableLocking();

    workbookx.lockStructure();

}
Hammerlock answered 2/4, 2013 at 8:20 Comment(3)
Could you please explain from where removePivot(s,workbookx); comes? Also, does this answer change at all on poi-ooxml 3.17?Deliadelian
Also, using this code on v. 3.17, minus the removePivot(s,workbookx);, I was able to open the saved XLSX, but then un-click "Protect Sheet" without entering a password. Then, I could modify the sheet however I liked.Deliadelian
@KevinMeredith: To make this work, you've to call the protectSheet method on the WorkSheet object with a password. With this, the protection can no longer be removed.Cheshvan
H
7

You might encounter that you can't select which features, it's either all or nothing. This is currently a known bug in Apache Poi. Source: https://issues.apache.org/bugzilla/show_bug.cgi?id=51483

You can fix this by using the following workaround:

  xssfSheet.enableLocking();
  CTSheetProtection sheetProtection = xssfSheet.getCTWorksheet().getSheetProtection();
  sheetProtection.setSelectLockedCells(true); 
  sheetProtection.setSelectUnlockedCells(false); 
  sheetProtection.setFormatCells(true); 
  sheetProtection.setFormatColumns(true); 
  sheetProtection.setFormatRows(true); 
  sheetProtection.setInsertColumns(true); 
  sheetProtection.setInsertRows(true); 
  sheetProtection.setInsertHyperlinks(true); 
  sheetProtection.setDeleteColumns(true); 
  sheetProtection.setDeleteRows(true); 
  sheetProtection.setSort(false); 
  sheetProtection.setAutoFilter(false); 
  sheetProtection.setPivotTables(true); 
  sheetProtection.setObjects(true); 
  sheetProtection.setScenarios(true);
Harangue answered 1/10, 2013 at 11:13 Comment(1)
Im using an XSSFSheet object.Is there a way to enable Clear all filters option in excel with my sheet being protected.? any suggestions ?Unassailable
S
0

Thanks to other answers, specially from @Patrigan, below code snipet that worked for me, using Apache POI version 3.17.

sheet.enableLocking();
CTSheetProtection sheetProtection = sheet.getCTWorksheet().getSheetProtection();
sheetProtection.setSelectLockedCells(true); 
sheetProtection.setSelectUnlockedCells(false); 
sheetProtection.setFormatCells(true); 
sheetProtection.setFormatColumns(true); 
sheetProtection.setFormatRows(true); 
sheetProtection.setInsertColumns(true); 
sheetProtection.setInsertRows(true); 
sheetProtection.setInsertHyperlinks(true); 
sheetProtection.setDeleteColumns(true); 
sheetProtection.setDeleteRows(true); 
sheetProtection.setSort(false); 
sheetProtection.setAutoFilter(false); 
sheetProtection.setPivotTables(true); 
sheetProtection.setObjects(true); 
sheetProtection.setScenarios(true);
sheet.protectSheet(password);
  
workbook_car.lockStructure();
Stpeter answered 27/12, 2022 at 20:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.