Sometimes, we may need some cells to be read-only. For example, when creating a template, the data in the template cannot be changed randomly. In Excel, you can click "review> protect worksheet", for example:
Is there any way in npoi to achieve this effect through encoding? The answer is yes.
Hssfsheet sheet1 = Hssfworkbook. createsheet ( " Sheet1 " );
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 ("Not locked");
Cel1.cellstyle=Unlocked;
Cel2.setcellvalue ("Locked");
Cel2.cellstyle=Locked;
Sheet1.protectsheet ("Password");
AsCodeAs shown in, we set the islocked of cellstype to true, indicating that the cell will be locked. The following operations are performed in Excel:
Then, use protectsheet to set the password.
The execution result is as follows:
You can modify a column that is not locked.
Locked Columns cannot be modified.
Enter a password to unlock the instance.
Returned directory