Apache POI - 如何使用选项保护工作表?

18
我正在使用Apache POI生成Excel文件(2007)。我想要保护工作表,但启用一些选项。这些选项是指当您尝试在Excel应用程序中保护工作表时出现的复选框列表(在标签“允许此工作表的所有用户:”下)。具体来说,我想启用“选择锁定/未锁定的单元格”,“格式化列”,“排序”和“允许自动筛选”。非常感谢!:D

我认为除了 sheet.getSettings() 的 set() 方法之外,你无法做任何事情。 - TheWhiteRabbit
sheet.getSettings() 是来自 JExcel,不是 Apache POI。 - John Bautista
3个回答

18
在Apache POI 3.9中,您可以启用锁定功能来使用XSSF表格保护。即使您可以将一些Excel对象保持未锁定,就像下面的情况一样,我将文本框对象保留在未锁定状态,其他对象均已锁定。
 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();

}

1
请问 removePivot(s,workbookx); 是从哪里来的?此外,这个答案在 poi-ooxml 3.17 上是否有所改变? - Kevin Meredith
1
此外,在v.3.17上使用此代码,去掉removePivot(s,workbookx);,我能够打开保存的XLSX文件,然后取消“保护工作表”而无需输入密码。然后,我可以随意修改工作表。 - Kevin Meredith
@KevinMeredith:要使其生效,您必须在工作表对象上调用protectSheet方法并设置密码。这样,保护就无法被取消了。 - Vikash Madhow

6
您可能会发现无法选择特定的功能,只能全部启用或禁用。这是Apache Poi中当前已知的一个 bug。 参考来源: https://issues.apache.org/bugzilla/show_bug.cgi?id=51483 您可以通过以下方法解决此问题:
  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);

我正在使用一个XSSFSheet对象。在保护我的工作表的情况下,有没有一种方法可以启用Excel中的“清除所有筛选器”选项?有什么建议吗? - Ryan Wright

0
感谢其他答案,特别是来自@Patrigan的回答,下面的代码片段对我很有用,使用的是Apache POI版本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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接