Excel book protection is very common, mainly do not want to let others modify Excel when used. This avoids malicious modification of data and increases the credibility of the data.
Here is a Java POI for setting book Protection:
You can use the Protectsheet (password) method of the Hssfsheet class.
The specific code is as follows:
PackageCom.util;ImportJava.io.FileOutputStream;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook; Public classPoicreateexcel { Public Static voidMain (string[] args)throwsException {//Create an Excel Workbook that corresponds to an Excel documentHssfworkbook WB =NewHssfworkbook (); Hssfsheet sheet= Wb.createsheet ("Sheet1"); //Sheet adding protection CodeSheet.protectsheet ("123"); //Create a line of sheet for ExcelHssfrow row = Sheet.createrow (0); //Create CellsHssfcell cell = Row.createcell (0); //assigning values to cells in ExcelCell.setcellvalue ("Hello World"); FileOutputStream OS=NewFileOutputStream ("G:\\workbook.xls"); Wb.write (OS); Os.close (); }}
Java POI action Excel add locked cell protection