使用Aspose.Cells for Java可以轻松完成。您可以
- 首先锁定所有列(所有单元)在一个工作表
- 解锁特定的细胞或细胞
的范围请参见下面的示例。
String dataDir = "D:\\data\\";
// Create or load workbook
Workbook book = new Workbook();
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
Style style;
StyleFlag flag = new StyleFlag();
// First lock all columns
for (int iCol=0 ; iCol<255 ; iCol++)
{
// Get style of the column
style = sheet.getCells().getColumns().get(iCol).getStyle();
// Apply locking to the style
style.setLocked(true);
flag.setLocked(true);
sheet.getCells().getColumns().get(iCol).applyStyle(style, flag);
}
// Get the range of cells, which we want to unlock
Range rangeUnlocked = sheet.getCells().createRange("A1:D4");
// Add a new style
int styleIndex = book.getStyles().add();
Style styleUnlocked = book.getStyles().get(styleIndex);
// Unlock cells
styleUnlocked.setLocked(false);
rangeUnlocked.setStyle(styleUnlocked);
// Protect the sheet
sheet.protect(ProtectionType.ALL);
//Save the Excel file
book.save(dataDir + "protectedrange.xlsx");
我在阅读Aspose工作作为一个开发者传播者。
ASPOSE在这方面不能像Excel那样工作(即所有的单元格都被默认锁定 - * un *在应用保护之前锁定了4个单元格)? – pnuts
我没有听说过aspose-cells自己,所以我不知道我是否阅读正确的文档,但在这里:http://www.aspose.com/docs/display/cellsjava/Style有一个'setLocked(booleanvalue)''方法...是的,我认为pnuts是正确的,因为'Excel'默认为所有单元格被锁定 – chancea
@chancea是不是只适用于单元格样式?我的意思是......我不确定,但它似乎锁定/解锁单元格中的编辑样式。无论如何,我要检查它.. – periback2