easypoi实现带有合并单元格数据的导出

以前只做过poi的表格导出,复杂繁琐,这次有个功能需要合并单元格的数据导出,从同事那里取经,发现了一个超级炒鸡好用的导出工具easypoi,啊,天哪,开发这个的真是是最强大脑!!!话不多说了,直接上代码:

一:首先在pom.xml引入jar包

这个jar是eosypoi需要的

        
            cn.afterturn
            easypoi-web
            3.0.3
        

这个jar是@Data注解需要的


   org.projectlombok
   lombok
   1.16.16
   

二、这里主要使用了Hibernate+JPA+easypoi
1)controller代码:
/**
     * 机房机架导出功能
     * @param map
     */
    @GetMapping(value = "/exportResourceRoomCloud")
    public void exportResourceRoomCloud(@RequestParam Map map) {
        logger.info("某某某列表导出-参数->{}", map);
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulateAll(map);
        JSONObject result = restCenterService.postJsonSend(JSONObject.class, "http://localhost:9091/LeaderPortal/queryRoomCloudByPageAndOrder", jsonObject.toString());
        JSONArray t = result.getJSONObject("data").getJSONArray("data");
/*        List titles = new ArrayList();
        titles.add("机房总览");
        titles.add("楼层");
        titles.add("总机架");
        titles.add("已用机架");
        titles.add("剩余机架");
        titles.add("主要用途");
        titles.add("主要客户");*/

        JSONObject j;
        try {

            Map map1 = new HashMap();
            List list = new ArrayList<>();
            for (int i = 0; i <  t.size(); i++) {
                j = t.getJSONObject(i);
                leaderRoomEntity entity = new leaderRoomEntity();
                entity.setBuildLocation("null".equals(j.getString("buildLocation")) ? "" : j.getString("buildLocation"));
                entity.setFloor("null".equals(j.getString("floor")) ? "" : j.getString("floor"));
                entity.setFrameTotal("null".equals(j.getString("frameTotal")) ? "" : j.getString("frameTotal"));
                entity.setFrameUsed(("null".equals(j.getString("frameUsed")) ? "" : j.getString("frameUsed")));
                entity.setMainSurplus(Integer.parseInt(("null".equals(j.getString("frameTotal")) ? "" : j.getString("frameTotal")))-
                        Integer.parseInt(("null".equals(j.getString("frameUsed")) ? "" : j.getString("frameUsed"))));
                entity.setMainUses("null".equals(j.getString("mainUses")) ? "" : j.getString("mainUses"));
                entity.setMainCustomer("null".equals(j.getString("mainCustomer")) ? "" : j.getString("mainCustomer"));
                list.add(entity);
            }

            map1.put("entityList", list);

//此处是使用easypoi导出的功能,上面的是设置实体类的值,至于实体类的值怎么设置,就是看你需要导出哪些字段,我这里是封装好的查询表格的信息的,你也可以使用原生的sql,JDBC来查询,只要能set到实体类即可
            ExportParams params1 = new ExportParams("某某信息表","某信息表");
            Workbook workbook = ExcelExportUtil.exportExcel(params1,leaderRoomEntity.class ,list);
            response.setHeader("Content-Type","application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode("某信息", "UTF-8")+".xls");

            response.setCharacterEncoding("UTF-8");
            workbook.write(response.getOutputStream());

        } catch (Exception e) {
            logger.info("导出失败");
        }

    }

2)

实体类,此处的需要合并单元格的字段设置mergeVertical = true,每个字段对应导出表格的首行

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;

/**
 * 用于easyPoi导出表格的实体类,对应的首行
 */
@Data
public class leaderRoomEntity {
    private static final long serialVersionUID = 1L;
    private String idx;
    @Excel(name = "机房总览", mergeVertical = true, width = 30)
    private String buildLocation;

    @Excel(name = "楼层",width = 30)
    private String floor;

    @Excel(name = "总机架", width = 30)
    private String frameTotal;

    @Excel(name = "已用机架", width = 30)
    private String frameUsed;

    @Excel(name = "剩余机架", width = 30)
    private int mainSurplus;

    @Excel(name = "主要用途", width = 30)
    private String mainUses;

    @Excel(name = "主要客户",width = 30)
    private String mainCustomer;

}

附上参考资料:http://easypoi.mydoc.io/#text_202980?tdsourcetag=s_pctim_aiomsg   EasyPoi官方文档

你可能感兴趣的:(easypoi实现带有合并单元格数据的导出)