Java一对多情况下,使用EasyPoi实现Excel合并单元格完成导出功能
发布日期:2021-05-15 00:00:43 浏览次数:20 分类:精选文章

本文共 5435 字,大约阅读时间需要 18 分钟。

Excel������������������������������������������

������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Excel���������������������������Excel������������������������������������������������������������������������������������������������������������������������������������������

������������

���������������

������������������������������Excel������������������������������������������

@Data
@Accessors(chain = true)
@ExcelTarget("ReportInfoExcel")
public class StaffExcel {
@Excel(name = "id", isColumnHidden = true)
private String id;
@Excel(name = "������", width = 20)
private String username;
@Excel(name = "������", width = 20)
private String cardNum;
@Excel(name = "������", width = 20)
private String dept;
@Excel(name = "������", width = 20)
private String position;
}
@Data
@Accessors(chain = true)
@ExcelTarget("ReportInfoExcel")
public class PartnerExcel {
@Excel(name = "id", isColumnHidden = true)
private String id;
@Excel(name = "������", width = 20)
private String username;
@Excel(name = "������", width = 20)
private String position;
@Excel(name = "������", width = 20)
private String company;
}
@Data
@Accessors(chain = true)
public class ReportInfoExcel {
@Excel(name = "id", isColumnHidden = true, needMerge = true)
private String id;
@Excel(name = "���������������", width = 20, needMerge = true)
private String reportUsername;
@Excel(name = "���������", width = 20, needMerge = true)
private String tel;
@Excel(name = "������", width = 20, needMerge = true)
private String email;
@ExcelCollection(name = "������������������")
private List
staffExcels;
@ExcelCollection(name = "������������������������")
private List
partnerExcels;
}

������������

���export������������������Response���������Excel������������������������������������������������������

import org.springframework.stereotype.Controller;
import excelutil.ExcelExportUtil;
@Controller
public class ExportController {
@GetMapping("export")
public void export(HttpServletResponse response) {
try {
response.setHeader("Content-Type", "application/vnd.ms-excel");
String fileName = "user" + DateUtil.formatDateTime(new Date()) + ".xls";
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
List
exportList = new LinkedList<>();
// ������������������
ReportInfoExcel reportInfo = new ReportInfoExcel();
reportInfo.setId("2")
.setTel("132********")
.setEmail("77@qq.com")
.setReportUsername("������");
StaffExcel staff = new StaffExcel();
staff.setId("1")
.setUsername("������")
.setCardNum("123456789")
.setDept("������������")
.setPosition("java������");
PartnerExcel partner = new PartnerExcel();
partner.setId("2")
.setUsername("������")
.setCompany("������������")
.setPosition("java������");
// ���������������������
reportInfo.setStaffExcels(Arrays.asList(staff, staff));
reportInfo.setPartnerExcels(Arrays.asList(partner, partner, partner));
exportList.add(reportInfo);
ExportParams exportParams = new ExportParams();
exportParams.setSheetName("������������1");
Map
map = new HashMap<>();
map.put("entity", ReportInfoExcel.class);
map.put("data", exportList);
List
> sheetsList = Arrays.asList(map);
Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

������������

������������������������������������Excel���������������������������������������������������������������������������������������������������������������������������������������������������������

������

���������������������������������������������Excel������������������������������������������������������������ReportInfoExcel���������id������������������needMerge = true���������������������������Excel������������������������������������������������������������������������������������������������������������������needMerge = true������������������������������������

������EasyPoi������Excel���������������������������

  • ������������������������������������Excel������
  • ������������������������������������������������������
  • ���������������������������������������
  • ���������������������������������������������������������������������Excel������������������������������needMerge������������������������������������������������

    上一篇:Java位运算,负数的二进制表示形式,int类型最大值为什么是2的31次方-1
    下一篇:Java常见线程池简介、线程池创建、线程池原理、死锁产生原因及排查

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年04月28日 19时21分31秒