当前位置:网站首页>使用工具类按一定规则读取Excel文件
使用工具类按一定规则读取Excel文件
2022-06-11 04:01:00 【小问号我们是朋友】
工作中需要按照功能分类去批量新增数据,数据的载体为Excel文件。现编写一个工具类按照业务规则来读取并处理Excel文件数据。
目录
1.工具类
在读取文件这里,使用hutool的依赖来处理数据;通过ExcelReader的别名处理方法来给Excel中的表头添加映射关系,使得数据可以对应上具体的实体类。
代码如下(示例):
package com.aisino.common.utils;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.aisino.common.core.domain.entity.Inter;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* Created by lijindong on 2022-05-30 17:36
*/
public class ExcelUtils {
/**
* description: 使用工具类获取Excel文件中的接口信息
* create by: lee
*/
public static List<Inter> getInterrInfoByHutool(File file) {
ExcelReader excelReader = ExcelUtil.getReader(file);
excelReader.addHeaderAlias("接口名称","interName");
excelReader.addHeaderAlias("调用方式","callMethod");
excelReader.addHeaderAlias("URL","operUrl");
excelReader.addHeaderAlias("接口状态 ","interStatus");
List<Inter> interList = excelReader.readAll(Inter.class);
excelReader.close();
return interList;
}
/**
* MultipartFile 转换为 File 文件
*
* @param multipartFile
* @return
*/
public final static File transferToFile(MultipartFile multipartFile) {
//选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
File file = null;
try {
String originalFilename = multipartFile.getOriginalFilename();
//获取文件后缀
String prefix = originalFilename.substring(originalFilename.lastIndexOf("."));
file = File.createTempFile(originalFilename, prefix); //创建临时文件
multipartFile.transferTo(file);
//删除
file.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
}
2.具体实现
代码如下(示例):
List<Inter> interList;
//从Excel中获取接口信息组装到集合中
File fileF = ExcelUtils.transferToFile(file);
interList = ExcelUtils.getInterrInfoByHutool(fileF);总结
以上就是今天要讲的内容,本文介绍了Excel工具类的使用,通过它可以实现文件批量导入业务数据。
边栏推荐
猜你喜欢

Simulation of radar emitter modulated signal
![[cnn]|differences between CNN and transformer](/img/ed/2b47387ba390a02c24bf9ff85b36e4.png)
[cnn]|differences between CNN and transformer

Red team shooting range with three-layer protection

Docker swarm installing MySQL Cluster

Google 有哪些牛逼的开源项目?

Esp32 development -lvgl uses internal and external fonts

Code replicates CSRF attack and resolves it

Guanghetong 5g module shines brightly and has won the "2021 science and technology award of China Electronics Society"

【CustomView】Glide+BitmapTransformation 图片上下边框波浪处理(WaveTransformation)

This artifact is highly recommended. One line command will convert the web page to PDF!
随机推荐
[signalr complete series] Net6 Zhongshi signalr communication
NTP time server (GPS Beidou satellite synchronous clock) application boiler monitoring system
ETH 中继接口
Possible problems with password retrieval function (supplementary)
店铺门面转让出租小程序开发制作功能介绍
Market prospect analysis and Research Report of Ethernet scanner in 2022
Google 有哪些牛逼的开源项目?
app直播源码,平台登录页面实现和修改密码页面实现
Quartz: an old and robust open source task scheduling framework, which is smooth enough to use
Implementation of one-dimensional convolutional neural network CNN based on FPGA (V) data quantization (with code)
直播助力杭州电商独角兽冲击上市,分账系统重构电商交易新格局
B - wall painting (C language)
Vulkan-官方示例解读-RayTracingShadows&在这里边使用模型(1)
What great open source projects does Google have?
Eth Transfer
A Security Analysis Of Browser Extensions
AI助力,释放法务势能!iTerms合同智审系统重磅发布
A. Prefix range (C language)
Student teacher examination management system based on SSM framework
Large factory outsourcing or self research company? How to choose a job for a tester?