0

I am trying to edit existing sheet,add new values to it and protect newly created workbook. From searching on net I came to know that sheet.protectSheet("some_password") is helpful. But in my case the compiler gives error that protectSheet method is not there. Then I tried using sheet.setProtect(true), but on execution it gives following error:

java.lang.NullPointerException
        at org.apache.poi.hssf.usermodel.HSSFSheet.setProtect(HSSFSheet.java:890)

Here is my code:

FileInputStream fsIP= new FileInputStream(new File(request.getSession().getServletContext().getRealPath(".")+ "/Cheque Printing.xls"));

HSSFWorkbook wb = new HSSFWorkbook(fsIP);

HSSFSheet worksheet = wb.getSheetAt(0);

Is the problem that I am accessing sheet via getSheetAt and not via createSheet ?

Pham Trung
  • 11,204
  • 2
  • 24
  • 43
JG1991
  • 143
  • 3
  • 14
  • Please take a look another answer on SO http://stackoverflow.com/questions/14701322/apache-poi-how-to-protect-sheet-with-options – hanumant Feb 23 '15 at 06:38
  • i want to use hssf not xssf can it help me this way? – JG1991 Feb 23 '15 at 08:37
  • 1
    I am not sure which version of Apache POI you are using. But the latest version has a simple and straightforward method. https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html#protectSheet(java.lang.String) – hanumant Feb 23 '15 at 08:43

1 Answers1

0

Without updating my poi library I found this solution:

Make your reference excel sheet protected and change above line to:

HSSFWorkbook wb = new HSSFWorkbook(fsIP,true);

Thats it! Rest of the code is same.

JG1991
  • 143
  • 3
  • 14