2013-02-05 33 views
10

저는 Apache POI를 사용하여 Excel 파일 (2007)을 생성하고 있습니다. 제가 원했던 것은 시트를 보호하는 것입니다.하지만 몇 가지 옵션을 사용할 수 있습니다. 옵션으로, Excel 응용 프로그램에서 시트를 보호하려고 할 때 확인란을 의미합니다 ("이 워크 시트의 모든 사용자에게 레이블 허용"레이블 아래에 있음). 특히, "잠긴/열어 본 셀 선택", "서식 열", "정렬"및 "자동 필터 허용"을 활성화하려고합니다. 고마워요! : DApache POI - 옵션으로 시트를 보호하는 방법?

+0

, 당신은 아무것도 할 수 있습니다. – TheWhiteRabbit

+0

sheet.getSettings()는 Apache POI가 아니라 JExcel에서 가져온 것입니다. – Jairo

답변

10

Apache POI 3.9에서는 잠금 기능을 활성화하여 XSSF 시트 보호를 사용할 수 있습니다. 심지어 당신은 몇 가지 엑셀 개체를 엑셀 개체 (즉, 텍스트 상자) 잠금을 해제하고 나머지는 잠겨 있지 않은 경우 아래에 잠금 해제로 남길 수 있습니다.

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

} 
5

어떤 기능을 선택할 수없는 경우가 있습니다. 모두 또는 아무것도 아닙니다. 이것은 현재 Apache Poi의 알려진 버그입니다. 출처 : https://issues.apache.org/bugzilla/show_bug.cgi?id=51483

는 다음과 같은 해결 방법을 사용하여이 문제를 해결할 수 있습니다 내가()`설정() 메소드`sheet.getSettings 이상으로 생각하지 말아

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

나는 XSSFSheet 개체를 사용하고 있습니다. 내 시트를 보호하면서 Excel의 모든 필터 지우기 옵션을 활성화하는 방법이 있습니다. 어떤 제안? –