行业知识
Java、SpringBoot实现对Excel内容的读取并将Excel内容导入到数据库中(后端篇)
2026-11-26 10:54:17
来源:互联网转载
一、需要读取的Excel表格格式相对完整工整,且只需要写入一张表中
�� 在读取Excel表格的需求中,有像下图的这么一种表格,它的格式工整,且表格中的列名和数据库表中的列名一一对应,如下图:
Excel表:
数据库表:
此时就需要用到下面的方法。
1、引入依赖
org.apache.poi poi RELEASE org.apache.poi poi-ooxml RELEASE commons-io commons-io 2.13.0
2、创建实体类
@Data@TableName("test")public class TestDto { @ExcelImport("id") private String id; @ExcelImport("username") private String username; @ExcelImport("password") private String password; } 3、导入Excel相关的工具类
创建exceIUtil包,并导入ExcelClassField、ExcelExport、ExcelImport和ExcelUtils工具类。
4、创建Mapper接口
public interface TestMapper extends BaseMapper {} 5、创建Service接口及它的实现类
public interface TestService extends IService { void add(TestDto testDto);} public class TestServiceImpl extends ServiceImpl implements TestService { public void add(TestDto testDto) { super.save(testDto);//加入数据库中 }} 6、编写Controller类
@PostMapping("/GetExcel")public Result GetExcel(@RequestParam("file") MultipartFile file) throws Exception { List testDtos = ExcelUtils.readMultipartFile(file, TestDto.class); for (TestDto testDto : testDtos) { testService.add(testDto); } return Result.success(testDtos, "导入成功"); } 7、具体实现
二、需要读取的Excel表格格式固定有规律,且需要写入多张表中
在读取Excel表格的需求中,有一种表格它的格式固定且有规律,一张表格中有写入多张数据库表格的需求,如下图:
在上图的表格中,我们需要在数据库中写入站点配置、车道配置、枪机配置等三张表的数据,且车道配置、枪机配置表的行数随着站点配置中的车道数量的变化而变化,此时就需要用到下面的方法。
1、引入依赖
详见第一种方法。
2、创建实体类
因为需要写入多张表中,所以需要创建多个实体类。且这些属于重复性操作,所以以下我就以写入两张表作为示例。
实体类1:
@Data@TableName("tri_zd_stationinfo")public class stationinfo { @ExcelImport("站点id") private Integer id; @ExcelImport("站点名称") private String station_name; @ExcelImport("业务类型") private String business_type; @ExcelImport("站点位置") private String station_position; @ExcelImport("安装日期") private Date install_date; @ExcelImport("摄像枪品牌类型") private Integer capture_brand; @ExcelImport("车道数量") private Integer lane_count;} 实体类2:
@Data@TableName("tri_zd_laneinfo")public class laneinfo { private Integer station_id; @ExcelImport("车道号") private Integer laneid; @ExcelImport("仪表ip") private String ip;} 3、导入Excel相关的工具类
详见第一种方法。
4、创建Mapper接口
Mapper接口1:
public interface ExcelStationMapper extends BaseMapper {} Mapper接口2:
public interface ExcelLaneMapper extends BaseMapper { //根据你的业务需求写代码,这里有删除旧数据/重复数据的需求 @Delete("delete from tri_zd_laneinfo where station_id = #{station_id};") int delByStation_id (int station_id);} 5、创建Service接口及它的实现类
Service接口及其实现类1:
public interface ExcelStationService extends IService { void delByStation_id(stationinfo stationinfo); void add(stationinfo stationinfo);} @Servicepublic class ExcelStationImpl extends ServiceImpl implements ExcelStationService { @Autowired ExcelStationMapper excelStationMapper; public void delByStation_id(stationinfo stationinfo){ excelStationMapper.deleteById(stationinfo.getId()); } @Override public void add(stationinfo stationinfo) { super.save(stationinfo); }} Service接口及其实现类2:
public interface ExcelLaneService extends IService { void delByStation_id(stationinfo stationinfo); void add (laneinfo laneinfo);} @Servicepublic class ExcelLaneImpl extends ServiceImpl implements ExcelLaneService { @Autowired ExcelLaneMapper excelLaneMapper; public void delByStation_id(stationinfo stationinfo){ excelLaneMapper.delByStation_id(stationinfo.getId()); } @Override public void add(laneinfo laneinfo) { super.save(laneinfo); }} 6、编写Controller类
//读取excel表格内容 @Autowired private ExcelStationService excelStationService; @Autowired private ExcelLaneService excelLaneService; @PostMapping("/GetExcel") public Result GetExcel(@RequestParam("file") MultipartFile file) throws Exception{ List stationinfos = new ArrayList(); List laneinfos = new ArrayList(); try { InputStream inputStream = file.getInputStream(); Workbook workbook = new HSSFWorkbook(inputStream); // 假设 Excel 表格的第一个工作表是要读取的工作表 Sheet sheet = workbook.getSheetAt(0); stationinfo stationinfo = new stationinfo(); for (int i = 2; i