easyexcel Excel 加密处理文件类型问题

nuypyhwy  于 4个月前  发布在  其他
关注(0)|答案(5)|浏览(41)

使用 EasyExcel 写加密后 xlsx 文档,并设置密码。读取的时候,不能设置 excelType 为 xlsx,否则报错;

代码复现

final List<DataVo> dataList = new ArrayList<>();
        try (final FileOutputStream output = new FileOutputStream(new File("file.xlsx"))) {
            try (final ExcelWriter excelWriter =
                         EasyExcel.write(output, DataVo.class)
                                 .excelType(ExcelTypeEnum.XLSX)
                                 .charset(StandardCharsets.UTF_8)
                                 .password("password")
                                 .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                                 .build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
                excelWriter.write(dataList, writeSheet);
                excelWriter.finish();
            }
        }
        EasyExcel.read(
                        new FileInputStream("file.xlsx"),
                        DataVo.class,
                        new FileLisnter()
                )
                .password("password")
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(0)
                .doRead();

目前了解到的细节

  1. 不设置 excelType 时,easyexcel 默认 ExcelTypeEnum.valueOf 会返回 xls.;
  2. 使用 Microsoft Office 使用密码能正常打 file.xlsx ,并没有提示文件格式问题;
vawmfj5a

vawmfj5a1#

第一个方法,可以试试在设置excelType后设置密码

qyuhtwio

qyuhtwio2#

第二个方法,就是提前对excel文件解密,然后解密的数据使用EasyExcel读,相关用例如下

import org.junit.jupiter.api.Test;

public class MyTest {
    @Test
    public void test() {
        final List<DemoData> dataList = new ArrayList<>();

        DemoData demoData = new DemoData();
        demoData.setId(System.currentTimeMillis());
        demoData.setString("你好");
        demoData.setDate(new Date());
        demoData.setDoubleData(23.0);
        dataList.add(demoData);

        try (final FileOutputStream output = new FileOutputStream("file.xlsx")) {
            try (final ExcelWriter excelWriter =
                         EasyExcel.write(output, DemoData.class)
                                 .excelType(ExcelTypeEnum.XLSX)
                                 .charset(StandardCharsets.UTF_8)
                                 .password("password")
                                 .build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
                excelWriter.write(dataList, writeSheet);
                excelWriter.finish();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        FileInputStream fis = new FileInputStream("file.xlsx");
        POIFSFileSystem fs = new POIFSFileSystem(fis);
        EncryptionInfo info = new EncryptionInfo(fs);
        Decryptor decryptor = Decryptor.getInstance(info);
        decryptor.verifyPassword("password");
        InputStream dataStream = decryptor.getDataStream(fs);

        EasyExcel.read(dataStream)
//                .password("password")
                .excelType(ExcelTypeEnum.XLSX)
                .head(DemoData.class)
                .registerReadListener(
                        new ReadListener<DemoData>() {
                            @Override
                            public void invoke(DemoData data, AnalysisContext context) {
                                System.out.println(data);
                            }

                            @Override
                            public void doAfterAllAnalysed(AnalysisContext context) {

                            }
                        }
                )
                .sheet()
                .doRead();
    }
}```
im9ewurl

im9ewurl3#

第一个方法,可以试试在设置excelType后设置密码

这个方法没用

wbgh16ku

wbgh16ku4#

第二个方法,就是提前对excel文件解密,然后解密的数据使用EasyExcel读,相关用例如下

import org.junit.jupiter.api.Test;

public class MyTest {
    @Test
    public void test() {
        final List<DemoData> dataList = new ArrayList<>();

        DemoData demoData = new DemoData();
        demoData.setId(System.currentTimeMillis());
        demoData.setString("你好");
        demoData.setDate(new Date());
        demoData.setDoubleData(23.0);
        dataList.add(demoData);

        try (final FileOutputStream output = new FileOutputStream("file.xlsx")) {
            try (final ExcelWriter excelWriter =
                         EasyExcel.write(output, DemoData.class)
                                 .excelType(ExcelTypeEnum.XLSX)
                                 .charset(StandardCharsets.UTF_8)
                                 .password("password")
                                 .build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
                excelWriter.write(dataList, writeSheet);
                excelWriter.finish();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        FileInputStream fis = new FileInputStream("file.xlsx");
        POIFSFileSystem fs = new POIFSFileSystem(fis);
        EncryptionInfo info = new EncryptionInfo(fs);
        Decryptor decryptor = Decryptor.getInstance(info);
        decryptor.verifyPassword("password");
        InputStream dataStream = decryptor.getDataStream(fs);

        EasyExcel.read(dataStream)
//                .password("password")
                .excelType(ExcelTypeEnum.XLSX)
                .head(DemoData.class)
                .registerReadListener(
                        new ReadListener<DemoData>() {
                            @Override
                            public void invoke(DemoData data, AnalysisContext context) {
                                System.out.println(data);
                            }

                            @Override
                            public void doAfterAllAnalysed(AnalysisContext context) {

                            }
                        }
                )
                .sheet()
                .doRead();
    }
}```

这个建议封装到 easyexcel 内部,自动识别是否是加密文档。如果是,直接提示需要密码即可。

2j4z5cfb

2j4z5cfb5#

这个bug现在我在尝试修复,目前能给出的解决办法就是这样,这个内部是实现了这个解密逻辑的,但是不知道什么原因没有生效,导致没有解密成功,进而影响了解析

相关问题