Sometimes, we may need to read only certain cells, such as when making a template, the template data is not at liberty to let others changed. In Excel, can "Review -> Protection Sheet" be accomplished, as shown below:
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
sheet1.ProtectSheet ( "password"); // set a password
HSSFRow row1 = sheet1.CreateRow(0);
HSSFCell cel1 = row1.CreateCell(0);
HSSFCell cel2 = row1.CreateCell(1);
HSSFCellStyle unlocked = hssfworkbook.CreateCellStyle();
unlocked.IsLocked = false;
HSSFCellStyle locked = hssfworkbook.CreateCellStyle();
locked.IsLocked = true;
cel1.SetCellValue("没被锁定");
cel1.CellStyle = unlocked;
cel2.SetCellValue("被锁定");
cel2.CellStyle = locked;
As seen in the code, we pass the set ISLocked CellStype is True, it indicates that the cell will be locked. Equivalent of the following operations performed in Excel:
Execution results are as follows:
Not locked column can be modified.
Locked click Edit will prompt:
We need to enter a password before they can (where password is the password we set above)
Original Post Address: https://www.cnblogs.com/dfzone/p/3805697.html