使用 POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in); EncryptionInfo encInfo = new EncryptionInfo(poifsFileSystem); Decryptor decryptor = Decryptor.getInstance(encInfo); decryptor.verifyPassword(pwd);时报错:org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML.,这个问题怎么解决?
时间: 2024-04-14 13:28:58 浏览: 11
出现这个错误是因为您正在尝试使用POI的POIFSFileSystem类来处理Office 2007+ XML格式的文件,而这个类主要用于处理旧版本的二进制格式(.xls)。对于Office 2007+ XML格式(.xlsx),您应该使用XSSFWorkbook类。
以下是一个示例代码,演示如何使用POI库来解密密码保护的Office 2007+ XML格式的Excel文件(.xlsx):
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class ExcelDecryptionExample {
public static void main(String[] args) {
String inputFile = "encrypted.xlsx";
String outputFile = "decrypted.xlsx";
String password = "password";
try {
FileInputStream fis = new FileInputStream(inputFile);
Workbook workbook = new XSSFWorkbook(fis);
// 如果文件受密码保护,请取消下面两行的注释并设置正确的密码
// POIFSFileSystem poifsFileSystem = new POIFSFileSystem(fis);
// EncryptionInfo encInfo = new EncryptionInfo(poifsFileSystem);
// Decryptor decryptor = Decryptor.getInstance(encInfo);
// decryptor.verifyPassword(password);
// 解密文件
workbook.removeSheetAt(0); // 删除加密时自动添加的隐藏工作表
// 在这里进行其他操作,例如读取或修改工作表数据
FileOutputStream fos = new FileOutputStream(outputFile);
workbook.write(fos);
fis.close();
fos.close();
System.out.println("Excel file decrypted successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请确保将`encrypted.xlsx`替换为您要解密的实际文件名,并将`password`替换为正确的密码。解密后的文件将保存在`decrypted.xlsx`中。
注意:对于没有加密的文件,您无需使用`Decryptor`类验证密码。
希望这可以解决您的问题!如有任何疑问,请随时提问。