<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.1.3</version> </dependency>
1.简单导出
package com.it.exercise.easyexcel; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.lang.UUID; import lombok.Data; import lombok.ToString; import lombok.experimental.Accessors; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Random; @Data @ToString @Accessors(chain = true) public class EmpSalary { private Integer id; private String realName; private String empNo; private String salaryDate; private Float baseAmount; private Float fullAttendAmount; private Float insurance; //特别资金 private Float specialAmount; /** * * * 传入0 时默认 10 * * @param listSize * @return */ public static List<EmpSalary> buildEmpSalaryList(int listSize) { int defaultSize = listSize; if (defaultSize == 0) { defaultSize = 10; } List<EmpSalary> empSalaryList = new ArrayList<>(defaultSize); for (int i = 0; i < defaultSize; i++) { EmpSalary empSalary = new EmpSalary(); LocalDateTime localDateTime = LocalDateTime.now(); empSalary.setSalaryDate(LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN)) .setEmpNo(UUID.randomUUID().toString()) .setRealName("user_" + UUID.randomUUID().toString(true)) .setBaseAmount(new Random().nextFloat() * 1000) .setInsurance(new Random().nextFloat() * 1000) .setFullAttendAmount(new Random().nextFloat() * 1000) .setSpecialAmount(new Random().nextFloat() * 1000); empSalaryList.add(empSalary); } return empSalaryList; } public static List<EmpSalaryDTO> toEmpSalaryDTO(int listSize){ List<EmpSalary> empSalaryList = buildEmpSalaryList(listSize); List<EmpSalaryDTO> empSalaryDTOS = BeanUtil.copyToList(empSalaryList, EmpSalaryDTO.class); return empSalaryDTOS; } }
package com.it.exercise.easyexcel; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import lombok.Data; import lombok.ToString; @Data @ToString public class EmpSalaryDTO { @ExcelProperty("ID") private Integer id; @ExcelProperty("姓名") private String realName; @ExcelProperty("员工编号") private String empNo; @ExcelProperty(value = "工资日期") private String salaryDate; @ExcelProperty("基本工资") private Float baseAmount; @ExcelProperty("全勤奖") private Float fullAttendAmount; @ExcelProperty("五险一金") private Float insurance; //特别资金 @ExcelIgnore private Float specialAmount; }
@SneakyThrows @Override public boolean exportExcel(HttpServletResponse response) { String fileName = new String("导出excel.xlsx".getBytes(), StandardCharsets.ISO_8859_1); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); List<EmpSalary> empSalaryList = EmpSalary.buildEmpSalaryList(0); AtomicReference<Integer> coumOrder = new AtomicReference<>(1); Optional.ofNullable(empSalaryList) .ifPresent(empSalaries -> empSalaries.forEach(empSalary -> empSalary.setId(coumOrder.getAndSet(coumOrder.get() + 1)))); ServletOutputStream outputStream = response.getOutputStream(); EasyExcel.write(outputStream, EmpSalaryDTO.class) .sheet("sheet1") .doWrite(empSalaryList); return true; }
2.导出多个sheet
/** * 模拟导出多个sheet (大数据) * * @param response * @return */ @Override public boolean exportExcelManySheets(HttpServletResponse response) { List<EmpSalaryDTO> empSalaryDTOS = EmpSalary.toEmpSalaryDTO(10); List<List<EmpSalaryDTO>> partition = Lists.partition(empSalaryDTOS, 5); // Optional<List<List<EmpSalaryDTO>>> partition1 = Optional.ofNullable(partition); // partition1.orElseThrow(() -> new RuntimeException("list集合为空")); String fileName = "aaaaaa.xlsx"; OutputStream outputStram = getOutputStram(response, fileName); ExcelWriter excelWriter = null; try { excelWriter = EasyExcel.write(outputStram, EmpSalaryDTO.class).build(); int count=0; for (List<EmpSalaryDTO> salaryDTOS : partition) { count++; WriteSheet writeSheet = EasyExcel.writerSheet("sheet_" + count).build(); excelWriter.write(salaryDTOS, writeSheet); } } catch (Exception e) { e.printStackTrace(); } finally { if (excelWriter != null) { excelWriter.finish(); } if (outputStram != null) { try { outputStram.close(); } catch (IOException e) { e.printStackTrace(); } } return false; } }