poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MSB <markbrd...@tiscali.co.uk>
Subject Re: writeProtectWorkbook() method does not work to set password to excel file to open
Date Wed, 16 Sep 2009 06:57:02 GMT

There is nothing that I can see to explicitly set the open password for an
Excel (.xls) file. However, I am going to try a workaround just to see if
the following is possible;

Using Excel, create a template file and set the open password.
Open that file with POI and populate the worksheet(s) then save the workbook
away again using a different name. Hopefully, the password will not prevent
POI from parsing the file and allowing me to work with the workbook.
Try to open this workbook using Excel: hopefully, it will ask me for the
password.

Will post again later.

Yours

Mark B


Debendra wrote:
> 
> Hi,
> Could anyone please help me to create a password protected excel sheet.
> The requirement is, password needs to be given to open an excel file. It
> will not open the excel file without password. I am using the following
> program, but it is not asking any password while opening.
> 
> import java.io.;
> import org.apache.poi.hssf.usermodel.;
> class PasswordExcelSheet {
> public static void main(String arg[]) {
> try{
> FileOutputStream out = new
> FileOutputStream("D:\\java_prog\\excelsheet\\MyExcel.xls");
> HSSFWorkbook hssfworkbook = new HSSFWorkbook();
> hssfworkbook.writeProtectWorkbook("abc","xyz");
> HSSFSheet sheet = hssfworkbook.createSheet("new sheet");
> String s="HCL Tech";
> HSSFCellStyle cs = hssfworkbook.createCellStyle();
> HSSFDataFormat df = hssfworkbook.createDataFormat();
> HSSFFont f = hssfworkbook.createFont();
> HSSFFont f2 = hssfworkbook.createFont();
> cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
> HSSFRow row = sheet.createRow((short)0);
> HSSFCell cell = row.createCell((short)0);
> cell.setCellValue(s );
> cell.setCellStyle(cs);
> //sheet.protectSheet("xyz");
> hssfworkbook.write(out);
> out.close();
> }
> catch(Exception e){
> System.out.println("Error : "+e);
> }
> }
> } 
> 

-- 
View this message in context: http://www.nabble.com/writeProtectWorkbook%28%29-method-does-not-work-to-set-password-to-excel-file-to-open-tp25447832p25466985.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message