2013-02-05 11 views
10

Utilizzo il POI Apache per generare un file Excel (2007). Quello che voglio è proteggere il foglio, ma con alcune opzioni abilitate. Per opzioni intendo l'elenco delle caselle di controllo quando si tenta di proteggere il foglio nell'applicazione Excel (sotto l'etichetta "Consenti a tutti gli utenti di questo foglio di lavoro di:"). Nello specifico, voglio abilitare "Seleziona celle bloccate/sbloccate", "Formatta colonne", "Ordina" e "Consenti filtro automatico". Grazie mille! : DPOI Apache - Come proteggere il foglio con le opzioni?

+0

non credo che al di là 'sheet.getSettings()' metodi set(), si può fare nulla. – TheWhiteRabbit

+0

sheet.getSettings() proviene da JExcel, non da Apache POI, penso. – Jairo

risposta

10

In Apache POI 3.9 è possibile utilizzare la protezione dello strato XSSF abilitando le funzioni di blocco. anche tu puoi lasciare indietro alcuni oggetti excel sbloccati come nel caso in cui ho lasciato fuori l'oggetto excel (cioè la casella di testo) sbloccato e il resto è bloccato.

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

Si potrebbe verificare che non è possibile selezionare le funzionalità, è tutto o niente. Questo è attualmente un bug noto in Apache Poi. Fonte: https://issues.apache.org/bugzilla/show_bug.cgi?id=51483

È possibile risolvere questo problema utilizzando la seguente soluzione:

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

Im usando un oggetto XSSFSheet. C'è un modo per abilitare l'opzione Cancella tutti i filtri in excel con il mio foglio protetto.? eventuali suggerimenti ? –

Problemi correlati