easypoi合並單元格


@RestController

public class ExportRest {

   

    @ApiOperation(value = "導出Excel", notes = "導出Excel詳細")

    @GetMapping("/export/exportExcel")

    public void exportExcel(HttpServletResponse response) {

        String excelName = "Excel名稱" + System.currentTimeMillis() + ".xlsx";

        try {

            //封裝表頭

            List<ExcelExportEntity> headList = new ArrayList<>();

            ExcelExportEntity excelExportEntity;

            /**

             * "姓名"    ---> 表頭顯示    "name"是屬性映射(后面會用到,name任意配置,但不要在同一個方法中重復,name只做映射標記,與其他無關)

             * excelExportEntity.setWidth(30);    --->  設置單元格寬度

             * excelExportEntity.setNeedMerge(true);   --->  開啟單元格合並,這個屬性的開啟並不會產生影響,因為沒有配置合並條件

             * */

            excelExportEntity = new ExcelExportEntity("姓名", "name");

            excelExportEntity.setWidth(30);

            excelExportEntity.setNeedMerge(true);

            headList.add(excelExportEntity);

   

            excelExportEntity = new ExcelExportEntity("年齡", "age");

            excelExportEntity.setWidth(30);

            excelExportEntity.setNeedMerge(true);

            //合並相同表頭(增加合並條件)

            excelExportEntity.setMergeVertical(true);

   

            headList.add(excelExportEntity);

   

            excelExportEntity = new ExcelExportEntity("性別", "sex");

            excelExportEntity.setWidth(30);

            excelExportEntity.setNeedMerge(true);

            headList.add(excelExportEntity);

   

   

            //"封裝"數據

            List<Map<String, Object>> dataList = new ArrayList<>();

            Map<String, Object> dataMap;

   

            //獲取測試數據

            List<Student> list = getList();

   

            //循環封裝數據

            for (Student student : list) {

                dataMap = new HashMap<>();

                //此時dataMap中的"key" 與headList中的"key"需要保持相 "對應",數據才會映射到對應列下。

                dataMap.put("name", student.getName());

                dataMap.put("age", student.getAge());

                dataMap.put("sex", student.getAge());

                //封裝每一條數據

                dataList.add(dataMap);

            }

   

   

            if (CollectionUtils.isNotEmpty(list)) {

                response.setCharacterEncoding("UTF-8");

                response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

                response.setHeader("Content-Disposition",

                        "attachment;filename=" + URLEncoder.encode(excelName, "UTF-8"));

                ExportParams params = new ExportParams("Excel的title", "sheet的Name");

                Workbook workbook = ExcelExportUtil.exportExcel(params, headList, list);

                workbook.write(response.getOutputStream());

            } else {

                //log.error("沒有可以導出的數據");

                return;

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

   

    //生成假數據 (這里Student只有(name,age,sex))

    public List<Student> getList() {

        List<Student> list = new ArrayList<>();

        int age = 10;

        for (int i = 1; i <= 100; i++) {

            if (i % 10 == 0) {

                age = i + 10;

            }

            list.add(new Student("張三" + i, age, 1));

        }

        return list;

    }

}

   

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM