6
使用 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();
目前了解到的细节
- 不设置 excelType 时,easyexcel 默认 ExcelTypeEnum.valueOf 会返回 xls.;
- 使用 Microsoft Office 使用密码能正常打 file.xlsx ,并没有提示文件格式问题;