Password protected xls/xlsx file in java

僤鯓⒐⒋嵵緔 提交于 2019-12-06 07:18:08

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();
 }

 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!