poi判断.xls和.xlsx格式

直接判断后缀名不严谨
1 通过POIFSFileSystem.hasPOIFSHeader(InputStream is);判断Excel 2003及以下

2通过POIXMLDocument.hasOOXMLHeader(InputStream is);判断Excel 2007及以上

这种判断,即使将excel文件后缀变换,也会正确识别,比如将.xlsx人为换成xls导入,还是能识别出为2007以上版本。

注意:传入的InputStream用BufferedInputStream装饰一层,如果直接传入InputStream,如果流不支持mark/reset机制,会抛出java.io.IOException: mark/reset not supported

public static void main(String[] args)throws Exception {
        File file = new File("D:\\docs\\work\\需求排期安排.xlsx");
        InputStream is = new FileInputStream(file);
        //这里用BufferedInputStream再包装一层,可解决:mark/reset not supported问题
        BufferedInputStream bis = new BufferedInputStream(is);
        if(POIFSFileSystem.hasPOIFSHeader(bis)) {
            System.out.println("2003及以下");
        }
        if(POIXMLDocument.hasOOXMLHeader(bis)) {
            System.out.println("2007及以上");
        }
    }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,spire.xls.jar 不支持对 xlsx 文件设置打开密码和取消打开密码。你可以尝试使用 Apache POI 或者其他类似的库来实现该功能。以下是使用 Apache POI 设置 xlsx 文件打开密码的示例代码: ``` import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileOutputStream; public class XlsxEncryptionExample { public static void main(String[] args) throws Exception { // 加载 xlsx 文件 File inputFile = new File("example.xlsx"); Workbook workbook = new XSSFWorkbook(inputFile); // 设置加密信息 EncryptionInfo encryptionInfo = new EncryptionInfo(EncryptionInfo.VERSION_AES_128); Encryptor encryptor = encryptionInfo.getEncryptor(); encryptor.confirmPassword("password123"); // 加密文件 POIFSFileSystem fileSystem = new POIFSFileSystem(); encryptor.encryptDocument(fileSystem, workbook); // 保存加密后的文件 File outputFile = new File("example_encrypted.xlsx"); try (FileOutputStream fos = new FileOutputStream(outputFile)) { fileSystem.writeFilesystem(fos); } } } ``` 要取消 xlsx 文件的打开密码,只需要删除 `workbook.getCTWorkbook().getWorkbookProtection()` 中的 `workbookProtection` 元素即可。以下是示例代码: ``` import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class XlsxDecryptionExample { public static void main(String[] args) throws Exception { // 加载加密后的 xlsx 文件 File inputFile = new File("example_encrypted.xlsx"); POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(inputFile)); EncryptionInfo encryptionInfo = new EncryptionInfo(fileSystem); Decryptor decryptor = Decryptor.getInstance(encryptionInfo); // 解密文件 if (!decryptor.verifyPassword("password123")) { throw new RuntimeException("Invalid password"); } try (FileOutputStream fos = new FileOutputStream("example_decrypted.xlsx")) { decryptor.getDataStream(fileSystem).transferTo(fos); } // 加载解密后的 xlsx 文件 Workbook workbook = new XSSFWorkbook(new FileInputStream("example_decrypted.xlsx")); // 取消打开密码 workbook.getCTWorkbook().unsetWorkbookProtection(); // 保存取消打开密码后的文件 try (FileOutputStream fos = new FileOutputStream("example_no_password.xlsx")) { workbook.write(fos); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值