[alibaba/easyexcel]导出数据加密

2024-06-20 569 views
3

导出excel如何加密?能用poi 的方式么?

回答

4

我建议通过其他方式对excel文件再次加密,因为对数据加密本身可能耗费的时间更多,用成型的产品会更好。不知道维护者有什么好想法。

5

这个是easydata没有任何关系,直接用poi即可。参照文档:https://poi.apache.org/encryption.html 建议采用的方式是先用easydata导出,然后用poi,重新读取加密:

 @Test
    public void writeV2011() throws Exception {
        String path = "D:\\test\\4566.xlsx";

        OutputStream out = new FileOutputStream(path);
        ExcelWriter writer = EasyExcelFactory.getWriter(out, ExcelTypeEnum.XLSX, false);
        //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
        Sheet sheet1 = new Sheet(1);
        sheet1.setSheetName("第一个sheet");
        writer.write1(createTestListObject(), sheet1);

        writer.finish();
        out.close();

        try (POIFSFileSystem fs = new POIFSFileSystem()) {
            EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
            // EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, null);

            Encryptor enc = info.getEncryptor();
            enc.confirmPassword("123456");

            // Read in an existing OOXML file and write to encrypted output stream
            // don't forget to close the output stream otherwise the padding bytes aren't added
            try (OPCPackage opc = OPCPackage.open(new File(path), PackageAccess.READ_WRITE);
                 OutputStream os = enc.getDataStream(fs)) {
                opc.save(os);
            }

            // Write out the encrypted version
            try (FileOutputStream fos = new FileOutputStream(path)) {
                fs.writeFilesystem(fos);
            }
        }
    }