easyexcel 导出excel

<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;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值