EasyPoi 导出Excel(ExcelExportEntity生成表头)
阅读原文时间:2023年07月09日阅读:1
  • [引入依赖]

    cn.afterturn easypoi-base 3.0.1
    cn.afterturn easypoi-web 3.0.1
    cn.afterturn easypoi-annotation 3.0.1

    /**
    * 写入数据导出Excel
    *
    * @param response
    */
    @GetMapping("/writeExcel")
    public void writeExcel(HttpServletResponse response) throws IOException {
    ExportParams params = new ExportParams();
    //生成要导出的数据
    List> resultList = new ArrayList<>();
    List> tempDataList = new ArrayList<>();
    List tempRowList1 = new ArrayList<>(Arrays.asList(1, 8, 1, 4, 2, 0, 9, 2, 2, 2));
    tempDataList.add(tempRowList1);
    List tempRowList2 = new ArrayList<>(Arrays.asList(0, 1, 8, 3, 4, 5, 7, 7, 8, 9));
    tempDataList.add(tempRowList2);
    List tempRowList3 = new ArrayList<>(Arrays.asList(1, 1, 2, 8, 4, 5, 6, 7, 8, 8));
    tempDataList.add(tempRowList3);

        for (List<Object> rowList : tempDataList) {
            System.out.println("这个集合的值是:" + rowList);
            Map<String, Object> rowMap = new HashMap<>();
            for (int i = 1; i <= rowList.size(); i++) {
                rowMap.put("title" + i, rowList.get(i - 1));
            }
            resultList.add(rowMap);
        }
    //列名
    List&lt;ExcelExportEntity&gt; entityList = new ArrayList&lt;&gt;();
    for (int i = 1; i &lt;= tempRowList1.size(); i++) {
        ExcelExportEntity entity = new ExcelExportEntity("标题" + i, "title" + i);
        entityList.add(entity);
    }
    
    Workbook workbook = ExcelExportUtil.exportExcel(params, entityList, resultList);
    
    response.addHeader("Content-Disposition", "filename=test.xlsx");
    //设置类型,扩展名为.xls
    response.setContentType("application/vnd.ms-excel");
    workbook.write(response.getOutputStream());
    }

    表头的值和数据的key是需要对应的

    手机扫一扫

    移动阅读更方便

    阿里云服务器
    腾讯云服务器
    七牛云服务器