POI读取加密的EXCEL(兼容XLS,XLSX格式)

最近项目里需要读取加密的excel,有xls和xlsx两种类型,对于加解密apache官方网站有简要说明


需要的jar包poi-3.9,jar    poi-ooxml-3.9.jar  poi-ooxml-schemas-3.9.jar   xmlbeans-2.3.0.jar  dom4j-1.6.1.jar  


注意poi的3个jar版本号要对应,不然会有一些问题。

上代码咯~

    /**
     * Excel导入
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = {"importExcel"})
    @ResponseBody
    public Result importExcel(@RequestParam(value = "ym")String ym,@RequestParam(value = "filedata")MultipartFile file){
    	int count=0;
        Result result = null;
        try {
            if (file != null) {

        		String password = "5151";
        		String fileName=file.getOriginalFilename();
        		String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
        		String yyyyMM=ym.replace("-", "");
        		
        		Workbook workbook;
        		//解密excel
        		if (prefix.toUpperCase().equals("XLS")) {
        			org.apache.poi.hssf.record.crypto.Biff8EncryptionKey
        					.setCurrentUserPassword(password);
        			workbook = WorkbookFactory.create(file.getInputStream());
        			file.getInputStream().close();
        		} else {
//        		    POIFSFileSystem pfs = newPOIFSFileSystem(POIDataSamples.getPOIFSInstance().openResourceAsStream("**.xlsx"));         		
        			POIFSFileSystem pfs = new POIFSFileSystem(file.getInputStream());
        			file.getInputStream().close();
        			EncryptionInfo encInfo = new EncryptionInfo(pfs);
        			Decryptor decryptor = Decryptor.getInstance(encInfo);
        			decryptor.verifyPassword(password);
            	    workbook = new XSSFWorkbook(decryptor.getDataStream(pfs));//不支持2007+的EXCEL
            	 // XSSFWorkbook workbook = new XSSFWorkbook(decryptor.getDataStream(pfs));//支持2007+的EXCEL
        		}
                //读取excel数据
        		Sheet sheet = workbook.getSheetAt(0);//不支持2007+的EXCEL
//	                XSSFSheet sheet = workbook.getSheetAt(0);	支持2007+的excel



  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!对于Java POI库,您可以使用以下代码来加密和解密Excel文件兼容xlsxlsx格式): 加密Excel文件: ```java import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.Encryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import java.io.FileInputStream; import java.io.FileOutputStream; public class ExcelEncryptionExample { public static void main(String[] args) { String inputFile = "input.xlsx"; String outputFile = "output.xlsx"; String password = "password"; try { FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); POIFSFileSystem fs = new POIFSFileSystem(); EncryptionInfo info = new EncryptionInfo(EncryptionInfo.OFFICE_OPENXML); Encryptor enc = info.getEncryptor(); enc.confirmPassword(password); enc.encryptDocument(fis, fos); fis.close(); fos.close(); System.out.println("Excel file encrypted successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` 解密Excel文件: ```java import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.filesystem.POIFSFileSystem; 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); FileOutputStream fos = new FileOutputStream(outputFile); POIFSFileSystem fs = new POIFSFileSystem(fis); EncryptionInfo info = new EncryptionInfo(fs); Decryptor dec = info.getDecryptor(); dec.verifyPassword(password); dec.decryptDocument(fis, fos); fis.close(); fos.close(); System.out.println("Excel file decrypted successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` 请确保将`input.xlsx`和`encrypted.xlsx`替换为您要加密或解密的实际文件名,并将`password`替换为您选择的密码。加密后的文件将保存在`output.xlsx`中,解密后的文件将保存在`decrypted.xlsx`中。 希望能对您有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值