java 使用 PIOFSFileSystem 读取 xlsx 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26729618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read xlsx file using POIFSFileSystem
提问by JavaTweets
I need to unprotect a protected xlsx file.e.g Book1.xlsx Below code runs fine for the first time, Reads Book1.xlsx, decrypt it and again write it to the same filename.
我需要取消保护受保护的 xlsx 文件。例如 Book1.xlsx 下面的代码第一次运行良好,读取 Book1.xlsx,解密并再次将其写入相同的文件名。
public static void unprotectXLSXSheet(String fileName, String password) {
try{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
d.verifyPassword(password);
InputStream is = d.getDataStream(fs);
System.out.println(is.available());
XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(is));
FileOutputStream fileOut;
fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}catch(FileNotFoundException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
But when the same code tries to access the newly created unprotected Book1.xlsx(or anyother unprotected xlsx file) it fails and showing
但是当相同的代码尝试访问新创建的不受保护的 Book1.xlsx(或任何其他不受保护的 xlsx 文件)时,它会失败并显示
Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:131)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
at com.wolseley.Excel.TestMainDummy.unprotectXLSXSheet(TestMainDummy.java:113)
at com.wolseley.Excel.TestMainDummy.main(TestMainDummy.java:52)
i need help in reading xlsx file and also unlock it using password, as done above.
我需要帮助阅读 xlsx 文件并使用密码解锁它,如上所述。
采纳答案by jmn
Basically the following line of code doesn't work for Office 2007+ XML documents:
基本上以下代码行不适用于 Office 2007+ XML 文档:
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
So you'll first need to check the header in the input stream whether it's supported by calling this:
因此,您首先需要通过调用以下命令来检查输入流中的标头是否受支持:
POIFSFileSystem.hasPOIFSHeader(is)
and only decrypting if the above returns true. The hasPOIFSHeader
method requires an input stream that supports mark/reset, so check that as well and wrap it in a PushbackInputStream
if not.
并且只有在上述返回 true 时才解密。该hasPOIFSHeader
方法需要一个支持标记/重置的输入流,因此也要检查它并将其包装在PushbackInputStream
if not 中。
Putting it all together then becomes something like this:
把它们放在一起,然后变成这样:
public static void unprotectXLSXSheet(String fileName, String password) throws Exception {
InputStream is = null;
FileOutputStream fileOut = null;
try {
is = new FileInputStream(fileName);
if (!is.markSupported()) {
is = new PushbackInputStream(is, 8);
}
if (POIFSFileSystem.hasPOIFSHeader(is)) {
POIFSFileSystem fs = new POIFSFileSystem(is);
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
d.verifyPassword(password);
is = d.getDataStream(fs);
}
System.out.println(is.available());
XSSFWorkbook wb = new XSSFWorkbook(OPCPackage.open(is));
fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
fileOut.flush();
} finally {
if (is != null) {
is.close();
}
if (fileOut != null) {
fileOut.close();
}
}
}
回答by Shishir Kumar
Below old Stack Overflow answer may be help you out from this.
下面旧的 Stack Overflow 答案可能会帮助你解决这个问题。
Reading property sets from Office 2007+ documents with java poi
使用 java poi 从 Office 2007+ 文档中读取属性集
The class you'll want is POIXMLProperties, something like:
您需要的类是 POIXMLProperties,类似于:
OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
POIXMLProperties props = new POIXMLProperties(pkg);
System.out.println("The title is " + props.getCorePart().getTitle());
From POIXMLProperties you can get access to all the built-in properties, and the custom ones too!
从 POIXMLProperties 您可以访问所有内置属性,也可以访问自定义属性!