spring boot-整合poi Excel表单导入实现 朱雀 2021-10-09 10:18 261阅读 0赞 首先了解一下:POI 中的CellType类型以及值的对应关系 <table> <thead> <tr> <th>CellType</th> <th>类型</th> <th>值</th> </tr> </thead> <tbody> <tr> <td>CELL_TYPE_NUMERIC</td> <td>数值型</td> <td>0</td> </tr> <tr> <td>CELL_TYPE_STRING</td> <td>字符串型</td> <td>1</td> </tr> <tr> <td>CELL_TYPE_FORMULA</td> <td>公式型</td> <td>2</td> </tr> <tr> <td>CELL_TYPE_BLANK</td> <td>空值</td> <td>3</td> </tr> <tr> <td>CELL_TYPE_BOOLEAN</td> <td>布尔型</td> <td>4</td> </tr> <tr> <td>CELL_TYPE_ERROR</td> <td>错误</td> <td>5</td> </tr> </tbody> </table> 记住几种转换的格式:string 、Integer、date、double 掌握这几种转换类型后可以不变应万变。 //字符串类型 if(row.getCell(5)!=null){//第7列 row.getCell(5).setCellType(Cell.CELL_TYPE_STRING); Type.setLng(row.getCell(5).getStringCellValue()); } //Integer类型 if(row.getCell(6)!=null){//第8列 row.getCell(6).setCellType(Cell.CELL_TYPE_STRING); Type.setCoordinate(Integer.parseInt(row.getCell(6).getStringCellValue())); } // 转换为日期类型date类型 if(row.getCell(7)!=null){//第8列 row.getCell(7).setCellType(Cell.CELL_TYPE_NUMERIC); Type.setInstalltime( HSSFDateUtil.getJavaDate(row.getCell(7).getNumericCellValue())); } //double类型 if(row.getCell(14)!=null){//第15列 row.getCell(14).setCellType(Cell.CELL_TYPE_NUMERIC); Type.setX(row.getCell(14).getNumericCellValue()); } 第一步:添加Dependence <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency> 第二步:编写Controller类 package com.hot.analysis.controller.poi; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.hot.analysis.bean.dc.deviceType; import com.hot.analysis.service.dc.DCService; @RestController public class Excelfile { @Autowired private DCService dcService; //表单导入 @RequestMapping("/dc/Excelfile") public String upload(MultipartFile file, HttpServletRequest request) { try { List<deviceType> typeLists = new ArrayList<deviceType>(); System.out.println("开始"); //使用POI解析Excel文件 //如果是xls,使用HSSFWorkbook;2003年的excel 如果是xlsx,使用XSSFWorkbook 2007年excel HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream()); //根据名称获得指定Sheet对象 HSSFSheet hssfSheet = workbook.getSheetAt(0); for (Row row : hssfSheet) { deviceType Type = new deviceType(); int rowNum = row.getRowNum(); if(rowNum == 0){//跳出第一行 一般第一行都是表头没有数据意义 continue; } if(row.getCell(1)!=null){//第2列数据 row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); Type.setName(row.getCell(1).getStringCellValue()); } if(row.getCell(2)!=null){//第3列 row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); Type.setCode(row.getCell(2).getStringCellValue()); } // 转换为Integer类型 if(row.getCell(3)!=null){//第4列 row.getCell(3).setCellType(Cell.CELL_TYPE_STRING); Type.setAdduserid(Integer.parseInt(row.getCell(3).getStringCellValue())); } // 转换为日期类型 if(row.getCell(4)!=null){//第5列 row.getCell(4).setCellType(Cell.CELL_TYPE_NUMERIC); Type.setAddtime( HSSFDateUtil.getJavaDate(row.getCell(4).getNumericCellValue())); } typeLists.add(Type); } //调用service执行保存typeLists的方法 dcService.saveExcelList(typeLists); }catch(Exception e){ e.printStackTrace(); } return "操作成功!"; } } 第三步:编写service void saveExcelList(List<deviceType> typeLists); 第四步:编写serviceImpl @Override public void saveExcelList(List<deviceType> typeLists) { for (deviceType type : typeLists) { //调用mapper的保存方法 dcmapper.insertSelective(type); } } 第五步:编写Mapper void insertSelective(deviceType type); 第六步:编写Mapper.xml <insert id="insertSelective"> INSERT INTO t_module(name,code,adduserid,addtime) VALUES(#{name},#{code},#{adduserid},#{addtime}); </insert> 测试: ![70][] 结果数据库: ![70 1][] 测试成功!! 总结其实表格导入并不难:有几种转换的格式要记住 [70]: /images/20211009/8ac052fed7894bbea67ab37cf32f5cfc.png [70 1]: /images/20211009/a617593c76e246adaaef9de7cb4d27ba.png
还没有评论,来说两句吧...