Hi Vincent,
Well, for your info, all the cells in a worksheet are initialized to be set with Locked on attributes by default.....right click on any cell in a simple worksheet and click Format Cells.... option, now click the last tab named "Protection" and you may see that "Locked" checkbox is checked by default. So, one should uncheck this locked checkbox first if he wants to set only a few cells/row/column in a worksheet. The process is simple one; you will unlock all the cells in the worksheet first, now lock your desired cells or row(s) or column(s), lastly, you will lock the worksheet and that's it.
May the following sample codes (which demonstrates below) help you for your requirements, kindly consult it.
Sample codes:
Lock only A1, A2 and A3 cells in the worksheet, the rest of the cells are not locked at all.
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.getWorksheets().getSheet(0);
// Define the style object.
Style style;
// First Loop through all the columns in the worksheet and unlock them.
for(int i = 0; i <= 255; i ++)
{
style = sheet.getCells().getColumns().getColumn(i).getStyle();
style.setCellLocked(false);
}
// Lock the three cells...i.e. A1, B1, C1.
style = sheet.getCells().getCell("A1").getStyle();
style.setCellLocked(true);
style = sheet.getCells().getCell("B1").getStyle();
style.setCellLocked(true);
style = sheet.getCell("C1").getStyle();
style.setCellLocked(true);
// Finally, Protect the sheet now.
Protection protection = new Protection();
sheet.protect(protection);
// Save the excel file.
wb.save("d:\\test\\lockedcells.xls", FileFormatType.EXCELXP);
Lock the first Column (A Column), the rest of the columns/cells are not locked at all.
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.getWorksheets().getSheet(0);
// Define the style object.
Style style;
// Define the styleflag object.
StyleFlag flag;
// First Loop through all the columns in the worksheet and unlock them.
for(int i = 0; i <= 255; i ++)
{
style = sheet.getCells().getColumns().getColumn(i).getStyle();
style.setCellLocked(false);
flag = new StyleFlag();
flag.setLocked(true);
sheet.getCells().getColumns().getColumn(i).applyStyle(style, flag);
}
// Get the first column style.
style = sheet.getCells().getColumns().getColumn(0).getStyle();
// Lock it.
style.setCellLocked(true);
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.setLocked(true);
// Apply the style to the first column.
sheet.getCells().getColumns().getColumn(0).applyStyle(style, flag);
// Protect the sheet.
Protection protection = new Protection();
sheet.protect(protection);
// Save the excel file.
wb.save("d:\\test\\lockedcolumn.xls", FileFormatType.EXCELXP);
For further reference, please check the following docs:
http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/protecting-worksheets.html
http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/advanced-protection-settings-since-excel-xp.html
http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/unprotect-a-worksheet.html
Hopfully, it will help you,
Thank you.