前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Excel文件加密的两种方式

Excel文件加密的两种方式

作者头像
zhaozhen
发布2022-06-21 16:38:28
1.3K0
发布2022-06-21 16:38:28
举报

Excel文件加密的两种方式

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

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

Zip4j的压缩选项更多

代码语言:javascript
复制
    //生成密码压缩文件
    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,请使用最新版,之前的老版本会有安全问题

代码语言:javascript
复制
        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.10.0</version>
        </dependency>

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

代码语言:javascript
复制
        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();
            }
        }
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-05-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微瞰技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Excel文件加密的两种方式
    • 使用EasyExcel配合Zip4j将文件加密为zip
      • 直接使用EasyExcel的password为Excel文件加密
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档