Java多线程导入Excel示例

文章介绍了如何在SpringMVC中使用ApachePOI库和多线程技术,提高大文件Excel导入的速度,通过分批处理和线程池并发执行,优化导入性能并演示了相关代码实现。

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

在导入Excel的时候,如果文件比较大,行数很多,一行行读往往速度比较慢,为了加快导入速度,我们可以采用多线程的方式
话不多说直接上代码
首先是Controller

import com.sakura.base.service.ExcelService;
import com.sakura.common.api.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    @PostMapping("/import")
    public ApiResult<Boolean> importExcel(@ModelAttribute MultipartFile file) throws Exception {
        boolean flag = excelService.importExcel(file);
        return ApiResult.result(flag);
    }
}

然后Service

import org.springframework.web.multipart.MultipartFile;

public interface ExcelService {

    boolean importExcel(MultipartFile file) throws Exception;

}

ServiceImpl

import com.sakura.base.entity.User;
import com.sakura.base.mapper.UserMapper;
import com.sakura.base.service.ExcelService;
import lombok.extern.java.Log;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
@Log
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean importExcel(MultipartFile file) throws Exception {
        // 文件为空这些校验大家自己加,这里只是做一个示例

        // 每次处理的数据量,大家可以自己调整,比如每次处理1000条
        int batchSize = 5;
        // 最大线程数,大家可以自己调整,根据自己服务器性能来调整
        int maxThreads = 3;

        // 创建一个线程池并设置最大线程数
        ExecutorService executorService = Executors.newFixedThreadPool(maxThreads);
        Workbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());

            // 获取第一页
            Sheet sheet = workbook.getSheetAt(0);
            // 获取总行数
            int rowCount = sheet.getLastRowNum();

            // 第0行一般为表头,从第一行开始
            int startRow = 1;
            // 结束行,Math.min用来比较两个数的大小,取最小值
            int endRow = Math.min(batchSize, rowCount);

            while (startRow <= rowCount) {
                // 提交任务到线程池
                executorService.execute(new ExcelRowProcessorTask(sheet, startRow, endRow));

                // 下一批数据的起始行
                startRow = endRow + 1;
                // 下一批数据的结束行
                endRow = Math.min(startRow + batchSize - 1, rowCount);
            }

            // 关闭线程池
            executorService.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return true;
    }

    private class ExcelRowProcessorTask implements Runnable {
        private final Sheet sheet;
        private final int startRow;
        private final int endRow;

        public ExcelRowProcessorTask(Sheet sheet, int startRow, int endRow) {
            this.sheet = sheet;
            this.startRow = startRow;
            this.endRow = endRow;
        }

        @Override
        public void run() {

            // _________________________________________
            // 测试用,模拟处理数据的耗时,实际应用删除
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // _________________________________________

            for (int i = startRow; i <= endRow; i++) {
                Row row = sheet.getRow(i);
                if (row != null) {
                    Cell nameCell = row.getCell(0); // 第一列
                    Cell phoneCell = row.getCell(1); // 第二列

                    nameCell.setCellType(CellType.STRING);
                    String name = nameCell.getStringCellValue();

                    phoneCell.setCellType(CellType.STRING);
                    String phoneNumber = phoneCell.getStringCellValue();

                    User user = new User();
                    user.setName(name);
                    user.setPhoneNumber(phoneNumber);

                    userMapper.insert(user);
                }
            }
        }
    }
}

实体类User就不贴了没啥好说的

还有就是poi的jar包

		<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>

用postman验证上面的代码

在这里插入图片描述

可以看下数据库的数据,因为我限制了每次处理的数据为5条,同时最多有3个线程,所以可以看到同一时间段导进去的数据为15条

在这里插入图片描述

上面这个还有一个问题就是主线程不会等数据导入完就会返回,如果你需要主线程等待数据导入完可以加上下面这行代码

executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); // 等待所有任务执行完毕 Long.MAX_VALUE为超时时间,可以自由设置

就放在关闭线程池后面就可以了

在这里插入图片描述

有想看下怎么用多线程导出Excel的移步 Java多线程导出Excel示例

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

子非衣

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

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

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

打赏作者

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

抵扣说明:

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

余额充值