首页 > 解决方案 > JAVA APACHE POI:出现“我们发现 *.xlsx 中的某些内容存在问题。您是否希望我们尽可能多地尝试恢复它”错误

问题描述

我们正在尝试生成 excel 文件,并且在尝试执行此代码时遇到了上述错误 -

我试过使用CreationHelper,也试过FileOutputStream。但是,似乎没有任何效果。我们正在使用 apache poi - 4.0.1

XSSFWorkbook xssfWorkbook = excelDownloadService.createSheetForBudget(type, campaignId, duration);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            xssfWorkbook.write(bos);

            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=Budget.Report_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMddyy")) + ".xlsx");
            xssfWorkbook.write(response.getOutputStream());

            xssfWorkbook.close();
            response.getOutputStream().flush();
            response.getOutputStream().close();

标签: javaapache-poi

解决方案


我在Name其他地方使用该属性来命名工作表。删除它后,它起作用了。

final Name name = xssfWorkbook.createName();
        name.setNameName("Summary.Report_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMddyy")) + ".xlsx");


推荐阅读