alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.69k stars 7.58k forks source link

导出xls正常导出xlsx提示 #3909

Closed ztwebster closed 1 month ago

ztwebster commented 3 months ago

触发场景描述

easyexcel导出xls正常导出xlsx提示

触发Bug的代码

easyexcel导出xls的代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/vnd.ms-excel");
        resp.setHeader("Content-disposition", "attachment;filename*=utf-8''cc2024.xls");

        ExcelWriter writer = EasyExcelFactory.write(resp.getOutputStream()).excelType(ExcelTypeEnum.XLS).build();
        WriteSheet sheet1 = new WriteSheet();
        sheet1.setSheetName("测试");
        sheet1.setSheetNo(0);
        WriteTable table = new WriteTable();
        table.setTableNo(1);
        List<List<String>> headTitles = ListUtils.newArrayList();
        headTitles.add(ListUtils.newArrayList("日期"));

        table.setHead(headTitles);
        writer.write(contentData(), sheet1, table);
        writer.finish();
    }

    private static List<List<Object>> contentData() {
        List<List<Object>> contentList = new ArrayList<List<Object>>();
        contentList.add(ListUtils.newArrayList("0801"));
        contentList.add(ListUtils.newArrayList("0802"));
        return contentList;
    }
  easyexcel导出xlsx的代码:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        resp.setHeader("Content-disposition", "attachment;filename*=utf-8''cc2024.xlsx");

        ExcelWriter writer = EasyExcelFactory.write(resp.getOutputStream()).excelType(ExcelTypeEnum.XLSX).build();
        WriteSheet sheet1 = new WriteSheet();
        sheet1.setSheetName("测试");
        sheet1.setSheetNo(0);

        WriteTable table = new WriteTable();
        table.setTableNo(1);
        List<List<String>> headTitles = ListUtils.newArrayList();
        headTitles.add(ListUtils.newArrayList("日期"));

        table.setHead(headTitles);
        writer.write(contentData(), sheet1, table);
        writer.finish();
    }

    private static List<List<Object>> contentData() {
        List<List<Object>> contentList = new ArrayList<List<Object>>();
        contentList.add(ListUtils.newArrayList("0801"));
        contentList.add(ListUtils.newArrayList("0802"));
        return contentList;
    }

提示的异常或者没有达到的效果

easyexcel导出xls正常打开: image

easyexcel导出xlsx报错提示: Excel 无法打开文件"cc2024(10).xsx”,因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配, image

psxjoy commented 3 months ago

版本4.0.1无法复现。 如果可以的话,麻烦提供最小的可复现的实例

psxjoy commented 1 month ago

不知道您是否已经解决该问题?我们先暂时关闭这个问题,如果还存在问题请重新Reopen这个issue。