目标:导出excel,包含多级表头、自动合并单元格

4.1.2 cn.afterturneasypoi-base${easypoi.version}cn.afterturneasypoi-web${easypoi.version}cn.afterturneasypoi-annotation${easypoi.version}
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;/*** Excel导入导出工具类* com.tdh.emos.utils -> ExcelUtils** @Author: guoxy* @Description:* @Date: 2021/7/21*/public class ExcelUtils {/*** excel 导出** @param list 数据列表* @param fileName 导出时的excel名称* @param response*/public static void exportExcel(List
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.tdh.emos.vo.performanceStatistics.AreaFymcVo;
import lombok.Data;import java.math.BigDecimal;/*** com.tdh.emos.vo.statics -> CreditorStaticsVo** @Author: guoxy* @Description:* @Date: 2022/6/28*/
@Data
public class CreditorStaticsVo{private static final String title = "债权申报";/*** 法院*/@Excel(name = "法院", orderNum = "0",mergeVertical = true,height = 16)private String province;/****/@Excel(name = "市", orderNum = "1",mergeVertical = true)private String city;/*** 法院*/@Excel(name = "法院", orderNum = "2",mergeVertical = true,width = 25)private String fymc;/*** 案件总数*/@Excel(name = "案件总数", orderNum = "3")private Integer caseTotal;/*** 发布债权申报公告的案件数*/@Excel(name = "发布债权申报公告的案件数", orderNum = "4",width = 15,groupName = title)private Integer noticeTotal;/*** 发布债权申报公告率*/private BigDecimal noticeRate;@Excel(name = "发布债权申报公告率", orderNum = "5",width = 15,groupName = title)private String noticeRateStr;/*** 债权人总数*/@Excel(name = "债权人总数", orderNum = "6",width = 15,groupName = title)private Integer creditorTotal;/*** 线上债权申报人数*/@Excel(name = "线上债权申报人数", orderNum = "7",width = 15,groupName = title)private Integer applyPassTotal;/*** 线上申报率*/private BigDecimal applyRate;@Excel(name = "线上申报率", orderNum = "8",width = 15,groupName = title)private String applyRateStr;/*** 线上债权申报总额(元)*/@Excel(name = "线上债权申报总额(元)", orderNum = "9",width = 15,groupName = title)private BigDecimal applyAmount;/*** 线上债权审核确定总额(元)*/@Excel(name = "线上债权审核确定总额(元)", orderNum = "10",width = 15,groupName = title)private BigDecimal confirmAmount;/*** 确定债权总额(元)*/@Excel(name = "确定债权总额(元)", orderNum = "11",width = 15,groupName = title)private BigDecimal creditorAmount;}
/*** 设置导出参数*/public static void dataExportParam(List list, HeadStatisticsDTO dto, HttpServletResponse response, Class clazz) throws IOException {// 是否有省级高院dto.setTitle(StrUtil.format("破产平台应用效能统计表({})",dto.getTitleType()));List fydmProvince = list.stream().filter(e -> e.getFydm().endsWith(Constant.FYDM_PROVINCE) && Constant.ONE_STR.equals(e.getDm())).map(AreaFymcVo::getFydm).collect(Collectors.toList());String fileName = StrUtil.format("{}破产平台应用效能统计{}({})", dto.getAreaName(),dto.getTitleType(), StrUtil.format("{}-{}",dto.getAssignStartTime(),dto.getAssignEndTime()));// 标题String sheetName = StrUtil.format("数据统计日期:{}-{}",DateUtil.format(DateUtil.parse(dto.getAssignStartTime()), "yyyy年MM月"),DateUtil.format(DateUtil.parse(dto.getAssignEndTime()), "yyyy年MM月"));// 自定义合并行列,CellRangeAddress(第几行开始,第几行结束,第几列开始,第几列结束)List addressList = new ArrayList<>();int titleNum = 2;// 合并第三到第四行,第一到第三列(即法院)addressList.add(new CellRangeAddress(titleNum, titleNum + 1, 0, 2));if (fydmProvince.size() > 0) {addressList.add(new CellRangeAddress(list.size() + titleNum--, list.size() + titleNum--, 1, 2));}if (dto.getAreaId().endsWith(Constant.FYDM_PROVINCE) && list.size() > 1) {long count = list.stream().filter(e -> e.getFydm().endsWith(Constant.FYDM_PROVINCE) && !Constant.ZERO_STR.equals(e.getDm())).count();// 高院合并单元格if (count > 0) {addressList.add(new CellRangeAddress(list.size() + titleNum, list.size() + titleNum, 1, 2));}titleNum++;addressList.add(new CellRangeAddress(list.size() + titleNum, list.size() + titleNum, 1, 2));}// 一级二级标题ExportParams exportParams = new ExportParams(dto.getAreaName() + dto.getTitle(), dto.getAreaName() + dto.getTitle(), ExcelType.XSSF);
// exportParams.setStyle(ExcelExportStyler.class); // 设置样式exportParams.setSecondTitle(sheetName);response.setHeader("Content-file", "filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));response.setHeader("Access-Control-Expose-Headers", "Content-file" );ExcelUtils.exportExcel(list, clazz, fileName, exportParams, addressList, response);}

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!