密码保护的Excel文件

我有一个Excel密码保护的电子表格。 我需要打开这个电子表格并从中读取数据。 我一直在尝试使用POI API无济于事。 Java解决scheme将是首选,但任何想法都将有所帮助。

编辑:是的,我有密码。 该文件是在Excel中的密码保护; 必须input密码才能查看电子表格。

编辑2:我无法打开POI与密码,我正在寻找一个备用的解决scheme。

你可以使用JExcelApi 。

我已经做了一段时间了,所以我可能不会告诉你如何正确地做,但肯定有一种方法可以使用JExcelApi来做到这一点。 尝试下面的来源:

Workbook workbook = Workbook.getWorkbook(new File("/path/to/protected.xls")); workbook.setProtected(false); WritableWorkbook copy = Workbook.createWorkbook(new File("/path/to/unprotected.xls"), workbook); WritableSheet[] sheets = copy.getSheets(); for (WritableSheet sheet : sheets){ sheet.getSettings().setProtected(false); } copy.write(); copy.close(); 

当然,你将需要导入必要的类并捕获必要的例外。

POI应该能够打开保护的xls文件(使用org.apache.poi.hssf.record.crypt )和受保护的xlsx文件(使用org.apache.poi.poifs.crypt )。 你尝试过这些吗?

如果您使用的是HSSF(用于xls文件),则需要在打开文件之前设置密码。 您可以通过以下方式致电:

  org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword(password); 

之后,HSSF应该能够打开你的文件。

对于XSSF,你需要这样的东西:

  POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("protect.xlsx")); EncryptionInfo info = new EncryptionInfo(fs); Decryptor d = Decryptor.getInstance(info); d.verifyPassword(Decryptor.DEFAULT_PASSWORD); XSSFWorkbook wb = new XSSFWorkbook(d.getDataStream(fs)); 

详细信息在POIencryption文档页面上给出

在ODBC源中添加excel文件(从控制面板 – >pipe理工具),然后执行代码:

 // program to extract data from excel file import java.sql.Connection ; import java.sql.Statement ; import java.sql.ResultSet ; import java.sql.ResultSetMetaData ; import java.sql.DriverManager ; import java.sql.SQLException ; public class ExtractExcelData { public static void main (String[] args) { try { Class.forName(DRIVER); connection = DriverManager.getConnection(URL,userName,password); } catch (ClassNotFoundException cnfe) { System.err.println("unable to load excel driver"); return ; } catch (SQLException se) { System.err.println("cannot connect to excel file"); return ; } try { statement = connection.createStatement(); String select = "SELECT * FROM [Sheet1$]"; resultSet = statement.executeQuery(select); metaData = resultSet.getMetaData(); int count = metaData.getColumnCount(); while ( resultSet.next() ) { String col1 = resultSet.getString(1) ; String col2 = resultSet.getString(2) ; String col3 = resultSet.getString(3) ; System.out.println( col1 ) ; System.out.println( col2 ) ; System.out.println( col3 ) ; System.out.println(); } } catch (SQLException se) { System.err.println("cannot execute query"); return ; } try { statement.close(); resultSet.close(); } catch (SQLException se ) { System.err.println("unable to close excel file"); return ; } } private static final String userName = "" ; private static final String password = "" ; private static final String URL = "jdbc:odbc:testexcel" ; private static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ; private static Connection connection ; private static Statement statement ; private static ResultSet resultSet ; private static ResultSetMetaData metaData ; } 

我试图从java脚本中设置excel文件的密码,这个脚本只能在IE上运行,而excel应该安装在客户端系统上。

 <script> function setPasswordToExcel(password,excelFileName,newFileName) { var Excel; Excel = new ActiveXObject("Excel.Application"); Excel.Visible = false; var obj = Excel.Workbooks.Open(excelFileName); obj.Password =password; obj.SaveAs(newFileName); obj.Close(); Excel.Close(); return 1; } setPasswordToExcel("stephen","C:/test1.xls","C:\\test2.xls"); </script>