Java easypoi 导出excel 并合并相关列

在项目开发中经常会使用到合并列,格式如下:

 1.引入easypoi

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
        </dependency>

2.定义一个实体类PersonDto

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

import java.io.Serializable;

@Data
public class PersonDto implements Serializable {
    private static final long serialVersionUID = 1L;

    @Excel(name = "省份", mergeVertical = true, width = 50)
    private String province;

    @Excel(name = "城市", mergeVertical = true, width = 50)
    private String city;

    @Excel(name = "民族", mergeVertical = true, width = 50)
    private String national;


    @Excel(name = "姓名", width = 20)
    private String userName;


    @Excel(name = "年龄", needMerge = true)
    private int age;


    @Excel(name = "地址", width = 50)
    private String address;
}

3.定义控制器ExportController

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.util.PoiMergeCellUtil;
import com.boot.basic.annotation.security.PreAuth;
import com.boot.web.vo.PersonDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("/excel")
public class ExcelController {
    @Autowired
    protected HttpServletResponse response;

    /**
     * 数据导出excel
     */
    @ApiOperation(value = "Get方式导出Excel")
    @RequestMapping(value = "/export", method = RequestMethod.GET, produces = "application/octet-stream")
    protected void exportExcel() {
        try {
            List<PersonDto> list = new ArrayList<>();

            for (int i = 0; i < 10; i++) {
                PersonDto entity = new PersonDto();
                entity.setProvince("广东");
                entity.setCity("广州");
                entity.setNational("汉族");
                entity.setUserName("张三_" + i);
                if (i > 4) {
                    entity.setUserName("李四");
                }
                entity.setAge(16 + i);
                entity.setAddress("广东省广州市_" + i);
                list.add(entity);
            }

            for (int i = 0; i < 10; i++) {
                PersonDto entity = new PersonDto();
                entity.setProvince("广西");
                entity.setCity("南宁");
                entity.setNational("壮族");
                entity.setUserName("王五_" + i);
                entity.setAddress("广西南宁_" + i);
                if (i > 2) {
                    entity.setNational("汉族");
                }
                if (i > 4) {
                    entity.setCity("桂林");
                    entity.setUserName("王五");
                    entity.setAddress("广西桂林_" + i);
                }
                entity.setAge(21 + i);
                list.add(entity);
            }

            // 导出excel
            ExportParams params = new ExportParams("人员信息表", "人员信息表", ExcelType.XSSF);
            Workbook workbook = ExcelExportUtil.exportExcel(params, PersonDto.class, list);
            // 重置响应对象
            response.reset();
            // 指定下载的文件名--设置响应头
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("人员信息表" + System.currentTimeMillis(), "UTF-8") + ".xlsx");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("数据导出失败:" + e.getMessage());
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值