失效链接处理 |
完成Excel导入mysql思路 PDF 下载
本站整理下载:
提取码:zjdj
相关截图:
![]()
主要内容:
完成Excel导入mysql思路
要想完成这个操作,我们得设置好工具类
1.设计数据库
我们得根据表的字段来进行数据库的设计
2.基础类的创建
创建数据库表对应的pojo、dao、service、serviceImpl文件,只需要生成insert方法
3.设置页面
我们得有一个文件上传页面,用来提交文件
4.设置controller类
接受文件的二进制流,如果二进制流为空,则文件上传失败,得返回一个提示页面。 如果读取到了二进
制流,我们则需要调用工具类进行处理。
5.设置工具类
工具类需要读取文件的行数以及列数,我们可以通过for循环来读取每一行的数据,然后把每一行的数
据使用for循环调用set方法把每一列当成一个属性写入pojo中,由于行数不止一行,可以利用链表进行
操作。最后把生成的链表返回controller中。
6.写入数据库
controller层把返回的链表进行遍历,每一次遍历便调用一次service中的insert方法,把数据写入到数
据库中
完成代码如下
ExcelController
package classthree.test.controller; import classthree.test.pojo.ExcellBean;
import classthree.test.service.ExceldataService; import classthree.test.util.ExcelUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.util.List; import java.util.Map; @Controller public class ExcelController { @Resource private ExceldataService exceldataService; @GetMapping("/index") public String index() { return "index"; }@RequestMapping("/uploadExcel") @ResponseBody public String uploadExcel(@RequestParam("file") MultipartFile file, Map<String, Object> map) { String name = file.getOriginalFilename(); if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) { return "文件格式错误"; }List<ExcellBean> list = null; try {list = ExcelUtils.excelToShopIdList(file.getInputStream()); if (list == null || list.size() <= 0) { return "导入的数据为空"; }//excel的数据保存到数据库 try {for (ExcellBean excel : list) { System.out.println(excel.toString()); exceldataService.insert(excel); } } catch (Exception e) { System.out.println(e.getMessage()); return e.getMessage(); } } catch (Exception e) { System.out.println(e.getMessage()); return e.getMessage(); }return "保存成功"; } }
ExcelUtils
package classthree.test.util; import classthree.test.pojo.ExcellBean; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** 操作excel * */ public class ExcelUtils { public static List<ExcellBean> excelToShopIdList(InputStream inputStream) { List<ExcellBean> list = new ArrayList<>(); Workbook workbook = null; try {workbook = WorkbookFactory.create(inputStream); inputStream.close(); //工作表对象 Sheet sheet = workbook.getSheetAt(0); //总行数 int rowLength = sheet.getLastRowNum(); System.out.println("总行数有多少行" + rowLength); //工作表的列 Row row = sheet.getRow(0); //总列数 int colLength = row.getLastCellNum(); System.out.println("总列数有多少列" + colLength); //得到指定的单元格 Cell cell = row.getCell(0); for (int i = 1; i <= rowLength; i++) { ExcellBean jiFenExcel = new ExcellBean(); row = sheet.getRow(i); for (int j = 0; j < colLength; j++) { cell = row.getCell(j); // System.out.print(cell + ","); if (cell != null) { cell.setCellType(Cell.CELL_TYPE_STRING); String data = cell.getStringCellValue(); data = data.trim(); if (j == 0) { jiFenExcel.setId(Integer.parseInt(data)); } else if (j == 1) { jiFenExcel.setSid(Integer.parseInt(data)); } else if (j == 2) { jiFenExcel.setName(data);
ExceldataDao
ExcellBean
} else if (j == 3) { jiFenExcel.setRoom(data); }else if (j == 4) { jiFenExcel.setComment(data); }// } } }list.add(jiFenExcel); // System.out.println("===="); } } catch (Exception e) { }return list; } }package classthree.test.dao; import classthree.test.pojo.ExcellBean; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; /*** (Exceldata)表数据库访问层 ** @author makejava * @since 2020-05-28 00:01:26 */ @Mapper @Repository public interface ExceldataDao { int insert(ExcellBean excellBean); }
|