You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Pavan Kumar <c....@gmail.com> on 2017/01/14 03:49:25 UTC
Regarding password protection for HSSFWorkbook(In Memory).
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
Re: Regarding password protection for HSSFWorkbook(In Memory).
Posted by Jörn Franke <jo...@gmail.com>.
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 <c....@gmail.com> 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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Regarding password protection for HSSFWorkbook(In Memory).
Posted by Pavan Kumar <c....@gmail.com>.
Thanks Andi. Will open enhancement request.
I would like to know when will 3.16 released. I can see only beta version
available as of now.
Pavan
On 15 Jan 2017 2:26 p.m., "Andreas Beeker" <ki...@apache.org> wrote:
Hi Pavan,
as I've already answered your SO issue [1], the encryption currently uses a
temp. file.
So open an enhancement request in bugzilla and I check what I can do ...
Best wishes,
Andi
[1] http://stackoverflow.com/questions/41354454
Re: Regarding password protection for HSSFWorkbook(In Memory).
Posted by Andreas Beeker <ki...@apache.org>.
Hi Pavan,
as I've already answered your SO issue [1], the encryption currently uses a temp. file.
So open an enhancement request in bugzilla and I check what I can do ...
Best wishes,
Andi
[1] http://stackoverflow.com/questions/41354454
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org