如何检查是否XLSX文件有密码保护或不使用Apache POI [英] How to check if xlsx file is password protected or not using apache poi
本文介绍了如何检查是否XLSX文件有密码保护或不使用Apache POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查是否XLSX文件有密码保护或没有。我们可以检查xls文件如下:
How to check if xlsx file is password protected or not. we can check for xls file as follows
FileInputStream fin = new FileInputStream(new File("C:/Book1.xls"));
POIFSFileSystem poifs = new POIFSFileSystem(fin);
EncryptionInfo info = new EncryptionInfo(poifs);
Decryptor d = Decryptor.getInstance(info);
try {
if (!d.verifyPassword(Decryptor.DEFAULT_PASSWORD)) {
throw new RuntimeException("Unable to process: document is encrypted");
}
InputStream dataStream = d.getDataStream(poifs);
HSSFWorkbook wb = new HSSFWorkbook(dataStream);
// parse dataStream
} catch (GeneralSecurityException ex) {
throw new RuntimeException("Unable to process encrypted document", ex);
}
但上面的code适用于仅XLS不是XLSX。
But the above code works for only xls not for xlsx.
推荐答案
尝试使用
XSSFWorkbook wb = new XSSFWorkbook(dataStream);
来自Apache POI:是HSSF将Excel '97(-2007)文件格式的POI项目的纯Java实现XSSF是Excel 2007中OOXML格式(.xlsx)文件格式的POI项目的纯Java实现。 http://poi.apache.org/s$p$padsheet/ 你再一个XLSX文件中使用HSSF(这会为XLS工作)。
From Apache POI: "HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format." http://poi.apache.org/spreadsheet/ You're using the HSSF (which will work for xls) on a XLSX file.
这篇关于如何检查是否XLSX文件有密码保护或不使用Apache POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文