We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Protect XLS

Hi there,


I would like to do a very simple things in aspose such as sheet protection and here are my snipped code:

License license = new License();
license.setLicense("/home/tpadmin/workspace-trunk/mb/MatchBox-Platform/src/main/resources/licenses/aspose.cells.lic");
Workbook workbook = new Workbook("/tmp/MyTest.xls", new LoadOptions(LoadFormat.EXCEL_97_TO_2003));

workbook.getWorksheets().get(“Sheet1”).getProtection().setPassword(“123”);
workbook.getWorksheets().get(“Sheet2”).getProtection().setPassword(“123”);
workbook.save("/tmp/result.xls", new XlsSaveOptions(SaveFormat.EXCEL_97_TO_2003));

and then, I opened result.xls in Excel 2010 however, the sheet is not protected at all. Please let me know if I do something wrong in here?

I am using Java AsposeCells 7.0.4

Cheers.


Hi,

See the following code on how to protect a sheet:

[Java]

Workbook wb;

wb = new Workbook(strfile_Path);

WorksheetCollection wss = wb.getWorksheets();

Worksheet sheet;

sheet = wss.get(0);

sheet.protect(ProtectionType.ALL, "123", null);

wb.save("result.xls");

OR ...Moreover, you need to add three lines to your existing code if you go for your way e.g

Protection protection = worksheet.getProtection();
protection.setAllowEditingContent(false);
protection.setAllowEditingObject(false);
protection.setAllowEditingScenario(false);
protection.setPassword( "123" );