该系统是在SpringBoot+tkmybitis ssm三大框架的基础上编写的
用到的jar包依赖
服务端首先加入websocket依赖,如下:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
1.先写Controller层
@Function
@RequestMapping("/device/deviceTypeManage/addListDeviceType")
public @ResponseBody String upload(MultipartFile file, HttpServletRequest request) {
try { List<Type> typeLists = new ArrayList<Type>();
//保存到项目路径下的 upload中 如果是导入数据就可以不用保存文件了
/* String path = request.getSession().getServletContext().getRealPath("upload");
String fileName = file.getOriginalFilename();
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs(); }
//保存
file.transferTo(targetFile); */
//使用POI解析Excel文件new FileInputStream(file)
System.out.println("开始");
//使用POI解析Excel文件
//如果是xls,使用HSSFWorkbook;2003年的excel 如果是xlsx,使用XSSFWorkbook 2007年excel
//HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(targetFile));
HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
//XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(targetFile));
//根据名称获得指定Sheet对象
HSSFSheet hssfSheet = workbook.getSheetAt(0);
for (Row row : hssfSheet) {
int rowNum = row.getRowNum();
if(rowNum == 0){//跳出第一行数据 第一行一般都是表头 没有插入意义
continue;
}
//获取每列的数据 数据类型要与实体类保持一致
//Cell.CELL_TYPE_STRING:string类型 Cell.CELL_TYPE_NUMERIC:Integer类型
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); row.getCell(2).setCellType(Cell.CELL_TYPE_STRING); row.getCell(3).setCellType(Cell.CELL_TYPE_NUMERIC); row.getCell(4).setCellType(Cell.CELL_TYPE_STRING); row.getCell(5).setCellType(Cell.CELL_TYPE_NUMERIC); //设置日期转换格式 2017/10/12 15:39:45 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); // 由于id是自增的可以不用设置 //Integer typeId = Integer.parseInt(row.getCell(0).getStringCellValue()); String typeName =row.getCell(1).getStringCellValue(); Integer enterpriseId = Integer.parseInt(row.getCell(2).getStringCellValue()); Date createTime = HSSFDateUtil.getJavaDate(row.getCell(3).getNumericCellValue()); String createUser = row.getCell(4).getStringCellValue(); Date updateTime = HSSFDateUtil.getJavaDate(row.getCell(5).getNumericCellValue()); Type Type = new Type(); Type.setTypeName(typeName); //Type.setTypeId(typeId); 由于id是自增的可以不用设置 Type.setEnterpriseId(enterpriseId); Type.setCreateTime(createTime); Type.setCreateUser(createUser); Type.setUpdateTime(updateTime); //deviceTypeMapper.insertSelective(Type); typeLists.add(Type);// id = Type.getTypeId(); } //调用service执行保存type的方法 deviceTypeService.saveList(typeLists); }catch(Exception e){ e.printStackTrace(); } return "操作成功!"; }
2.写service层
public void saveList(List<Type> typeLists){
//循环遍历保持到数据库 这样就不用在mapper.xml文件中写批量插入方法 性能方面暂时不考虑
for (Type type : typeLists) {
//调用mapper的保存方法
deviceTypeMapper.insertSelective(type);
}
//insertSelective方法是tkmybatis自动生成的插入数据库的方法,不需要自己手写 如果读者没有该方法可以自己手动写入插入//方法 或者调用自己之前写好的插入方法效果是一样的这样就写完了
测试一下
//来个Excel表 xls 2003版的
数据库: