I have created password protected zip file which has xls file with the help of this http://java.sys-con.com/node/1258827.
My question is ,Is there any java api which will create password protected xls file instead of zip file.
I want to directly apply password on xls file.Encryption/Decryption is the option but want to prompt when when double clicked on file.
edit:
I got this
HSSFSheet.protectSheet("xyz");
but it only makes sheet read only.
Even I have tried this hssfworkbook.writeProtectWorkbook("abc", "abc");
but its not prompting for password.
Edit1:There is the method in org.apache.poi.hssf.record.crypto.Biff8EncryptionKey
package to open password protected excel file org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword(password);
Is there any method to set password on unprotected excel file?
Edit2:
I tried JExcel API
Workbook w=Workbook.getWorkbook(new File("c:\\employees22533.xls"));
Sheet ws=null;
ws=w.getSheet("Employee List");
SheetSettings sh=ws.getSettings();
sh.setPassword("abc");
But its not setting any password
I've personally used JExcelApi
but I do not remember having seen something about password protection into it. As far as JExcelApi
is concerned there are a number of features where the answer is known to be "No":
- Pivot Tables
- Dropdown Lists
- Rich Text in cells
- Set repeating rows
- Password Protection
I personally think that if Password Protection would've been possible(with open source api's)
Their would be a lot of tutorials available on internet and you could search them simply by a quick Google search, Unfortunately none of the freely available Java spreadsheet APIs seems to support writing encrypted spreadsheets.
However If you're willing to use commercial api/library then see this
You can do it using JACOB. Here is the code for no protection temp.xsl file to protected temptest.xsl file. You need to have jacob.jar and jacob-XX-XX.dll in your classpath
package test;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class excel
{
private ActiveXComponent excelApp = null;
public excel()
{
String xlsFile = "D:\\temp.xls";
excelApp = new ActiveXComponent("Excel.Application");
excelApp.setProperty("Visible", new Variant(false));
Object workbooks = excelApp.getProperty("Workbooks").toDispatch();
Object workbook = Dispatch.invoke((Dispatch) workbooks,"Open",Dispatch.Method,new Object[] {xlsFile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
Dispatch.call((Dispatch)workbook, "SaveAs", new Variant("D:\\temptest.xls"),new Variant("1"),new Variant ("pass"));
excelApp.invoke("Quit", new Variant[] {});
}
public static void main(String arg[])
{
System.out.println("hello");
new excel();
}
}