使用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