Only 3.16 supports encryption for HSSF. You have to wait until it is released to get a stable version.
> On 14 Jan 2017, at 04:49, Pavan Kumar <[email protected]> wrote: > > Hi, > > I have seen below statement in > 20 November 2016 - POI 3.16-beta1 available ( > https://poi.apache.org/download.html)This release includes improved VBA > support, better encryption support for password protected files and > encrypting temporary files. > > I have used the beta version and tried setting password using below > code(Its working). > Is there any way we can set password for HSSFWorkbook using stable version > of Apache POI? > Point is I dont have the permission to filesystem, so cannot create the > workbook, then save workbook filesystem and then set password to file > created in filesystem. Its just that I want to set password to In memory > Workbook. > > import java.io.FileNotFoundException; > import java.io.FileOutputStream; > import java.io.IOException; > > import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; > import org.apache.poi.hssf.usermodel.HSSFCell; > import org.apache.poi.hssf.usermodel.HSSFRichTextString; > import org.apache.poi.hssf.usermodel.HSSFRow; > import org.apache.poi.hssf.usermodel.HSSFSheet; > import org.apache.poi.hssf.usermodel.HSSFWorkbook; > > public class Poitest { > > public static void main(final String[] args) throws Exception { > > try { > > HSSFWorkbook workbook = new HSSFWorkbook(); > Biff8EncryptionKey.setCurrentUserPassword("test"); > HSSFSheet sheet = workbook.createSheet("sheet1"); > int rows = sheet.getLastRowNum(); > System.out.println("rows:" + rows); > > for (int i = 0; i <= rows; i++) { > HSSFRow row = sheet.getRow(i); > if (row == null) > continue; > int cols = row.getLastCellNum(); > System.out.println("cols:" + cols); > > for (int j = 0; j <= cols; j++) { > HSSFCell cell = row.getCell((int) j); > if (cell == null) > continue; > if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { > HSSFRichTextString val = > cell.getRichStringCellValue(); > System.out.println(val); > } > if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { > double val = cell.getNumericCellValue(); > System.out.println(val); > } > } > } > > workbook.write(new FileOutputStream("test3_16Beta.xls")); > workbook.close(); > } catch (FileNotFoundException e) { > e.printStackTrace(); > } catch (IOException e) { > e.printStackTrace(); > } finally { > // try { > // binput.close(); > // } catch (IOException e) { > // e.printStackTrace(); > // } > } > System.out.println("done"); > > } > } > > Thanks, > Pavan --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
