java使用EasyExcel导出百万数据

该博客展示了如何使用阿里巴巴的EasyExcel库导出1000960行数据到Excel文件中,并记录了执行耗时。在Controller中,通过Autowired的LedgerRealFeeService获取数据,然后利用EasyExcel的write方法将数据写入到指定路径的Excel文件。每个数据对象LedgerreRealFeeVo包含了业务号、任务号、类型等11个字段,所有操作在1000960行数据上执行完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Maven依赖如下:

       <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>easyexcel</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.17</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.17</version>
		</dependency>
Controller:
package com.excel.demo.controller;


import com.alibaba.excel.EasyExcel;
import com.excel.demo.vo.LedgerreRealFeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :Hj
 * @date 2021/2/24 10:50
 */
@RestController
@RequestMapping("/person")
public class PersonControl {
    @Autowired
    private LedgerRealFeeService feeService;

    @RequestMapping(value = "/getData")
    public String test(){
        //获取数据
        List<LedgerreRealFeeVo> realFeeVos= feeService.getDataVo();
        String filename = "D:\\Data\\Data.xlsx";
        long start = System.currentTimeMillis();
        //关键代码
        EasyExcel.write(filename,LedgerreRealFeeVo.class).sheet("数据").doWrite(realFeeVos);
        long finish = System.currentTimeMillis();
        long timeElapsed=finish-start;
        System.out.print(timeElapsed);

        return "sucess";
    }

}

VO:

package com.excel.demo.vo;

import com.alibaba.excel.annotation.ExcelProperty;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author :Hj
 * @date 2021/2/24 10:47
 */
public class LedgerreRealFeeVo {
    @ExcelProperty(value = "业务号")
    private Long sno;
    @ExcelProperty(value = "任务号")
    private String taskId;
    @ExcelProperty(value = "类型")
    private String certiType;
    @ExcelProperty(value = "人员代码")
    private String agentCode;
    @ExcelProperty(value = "机构")
    private String comCode;
    @ExcelProperty(value = "业务性质")
    private String businessNature;
    @ExcelProperty(value = "团队代码")
     private String teamCode;
    @ExcelProperty(value = "团队名称")
     private String teamName;
    @ExcelProperty(value = "日期")
    private Date realPayDate;
    @ExcelProperty(value = "afee")
    private BigDecimal afee;
    @ExcelProperty(value = "bfee")
    private BigDecimal bfee;
    @ExcelProperty(value = "cfee")
    private BigDecimal cfee;

    public Long getSno() {
        return sno;
    }

    public void setSno(Long sno) {
        this.sno = sno;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getCertiType() {
        return certiType;
    }

    public void setCertiType(String certiType) {
        this.certiType = certiType;
    }

    public String getAgentCode() {
        return agentCode;
    }

    public void setAgentCode(String agentCode) {
        this.agentCode = agentCode;
    }

    public String getComCode() {
        return comCode;
    }

    public void setComCode(String comCode) {
        this.comCode = comCode;
    }

    public String getBusinessNature() {
        return businessNature;
    }

    public void setBusinessNature(String businessNature) {
        this.businessNature = businessNature;
    }

    public String getTeamCode() {
        return teamCode;
    }

    public void setTeamCode(String teamCode) {
        this.teamCode = teamCode;
    }

    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }

    public Date getRealPayDate() {
        return realPayDate;
    }

    public void setRealPayDate(Date realPayDate) {
        this.realPayDate = realPayDate;
    }

    public BigDecimal getAfee() {
        return afee;
    }

    public void setAfee(BigDecimal afee) {
        this.afee = afee;
    }

    public BigDecimal getBfee() {
        return bfee;
    }

    public void setBfee(BigDecimal bfee) {
        this.bfee = bfee;
    }

    public BigDecimal getCfee() {
        return cfee;
    }

    public void setCfee(BigDecimal cfee) {
        this.cfee = cfee;
    }
}

1000960行数据耗时:

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心寒丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值