我正在使用Apache POI生成Excel文件(2007)。我想要保护工作表,但启用一些选项。这些选项是指当您尝试在Excel应用程序中保护工作表时出现的复选框列表(在标签“允许此工作表的所有用户:”下)。具体来说,我想启用“选择锁定/未锁定的单元格”,“格式化列”,“排序”和“允许自动筛选”。非常感谢!:D
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();
}
removePivot(s,workbookx);
是从哪里来的?此外,这个答案在 poi-ooxml 3.17 上是否有所改变? - Kevin MeredithremovePivot(s,workbookx);
,我能够打开保存的XLSX文件,然后取消“保护工作表”而无需输入密码。然后,我可以随意修改工作表。 - Kevin Meredith 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);
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();
sheet.getSettings()
的 set() 方法之外,你无法做任何事情。 - TheWhiteRabbit