Excel文件加密的两种方式

我们经常会遇到导出的数据需要安全和需要脱敏的场景,这种场景下如何做呢,这里我们使用EasyExcel来实现,有以下两种方案

使用EasyExcel配合Zip4j将文件加密为zip

Zip4j的压缩选项更多

    //生成密码压缩文件
    private static File getZipFile(File  file,char [] pwd) throws ZipException {
        ZipFile zipFile = new ZipFile(UUID.randomUUID().toString()+".zip",pwd);
        ZipParameters parameters = new ZipParameters();
        // 压缩级别
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
        zipFile.addFile(file,parameters);
        return zipFile.getFile();
    }
    //转换File为MultipartFile
        public static MultipartFile getMultipartFile(File file,String pwd) throws Exception{
        File zipFile =   getZipFile(file,pwd.toCharArray());
        MultipartFile multipartFile = new CommonsMultipartFile(ExcelUtil.createFileItem(zipFile));
        if (file != null && file.exists()) {
            file.delete();
        }
        if (zipFile != null && zipFile.exists()) {
            zipFile.delete();
        }
        return multipartFile;
    }
 
        /**
     *  multipartFile.getInputStream()
     * @description: 将输入流输出到页面
     */
    public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
        OutputStream out = null;
        try {
            out = resp.getOutputStream();
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = inputStream.read(b)) != -1) {
                out.write(b, 0, len);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

使用的pom,请使用最新版,之前的老版本会有安全问题

        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.10.0</version>
        </dependency>

直接使用EasyExcel的password为Excel文件加密

        ExcelWriter writer = null;
        OutputStream outputStream = null;
        try {
            String fileName = UUID.randomUUID() + excelTypeEnum.getValue();
            prepareResponse(response, fileName);
            outputStream = response.getOutputStream();
            writer = EasyExcel.write(outputStream)
                    .excelType(excelTypeEnum)
                    .password(pwd)
                    .build();
            WriteSheet sheet = EasyExcel.writerSheet(sheetName)
                    .head(dataList.get(0).getClass())
                    .build();
            writer.write(dataList, sheet);

        } finally {
            if (writer != null) {
                writer.finish();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
posted @ 2022-05-02 20:28  镇屌  阅读(523)  评论(0编辑  收藏  举报