org.apache.poi
poi-ooxml
3.9
net.sf.json-lib
json-lib
2.4
jdk15
下面是我们的数据库
前端使用Layui
成绩录入
然后我们使用Mybatisplus对Bus_score自动生成一些类
主要的业务逻辑代码
package com.bs.bus.common;
import com.bs.bus.entity.Score;
import com.bs.sys.common.excel.ExcelUtil;
import com.bs.sys.service.IDeptService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component
public class ScoreExcelParse {
@Autowired
private IDeptService deptService;
//Autowired注解,spring不推荐使用静态方法,在非controller中使用的话需要在类加载之前初始化autowired服务,再进行使用
public static ScoreExcelParse scoreExcelParse;
@PostConstruct
public void init() {
scoreExcelParse = this;
scoreExcelParse.deptService=this.deptService;
}
public static List ExceltoScore(File userUploadFile){
List list=new ArrayList<>();
Score score=null;
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(userUploadFile));
HSSFWorkbook wb = new HSSFWorkbook(fs);
//获取第一个sheet页
HSSFSheet sheet = wb.getSheetAt(0);
if(sheet!=null){
for(int rowNum =1;rowNum<=sheet.getLastRowNum();rowNum++){
HSSFRow row = sheet.getRow(rowNum);
if(row==null){
continue;
}
score=new Score();
//将单元格里每行数据存入ScoreVo对象中
score.setDeptname(ExcelUtil.formatCell(row.getCell(0)).split("\\.")[0]);
score.setTestname(ExcelUtil.formatCell(row.getCell(1)));
score.setUserid(Integer.parseInt(ExcelUtil.formatCell(row.getCell(2)).split("\\.")[0]));
score.setUsername(ExcelUtil.formatCell(row.getCell(3)).split("\\.")[0]);
score.setChinesescore(Integer.parseInt(ExcelUtil.formatCell(row.getCell(4)).split("\\.")[0]));
score.setMathscore(Integer.parseInt(ExcelUtil.formatCell(row.getCell(5)).split("\\.")[0]));
score.setEnglishscore(Integer.parseInt(ExcelUtil.formatCell(row.getCell(6)).split("\\.")[0]));
score.setTeacherwords(ExcelUtil.formatCell(row.getCell(7)));
score.setTotlescore(score.getChinesescore()+score.getEnglishscore()+score.getMathscore());
score.setTestid(Md5Util.generateHash(score.getTestname()).substring(0,6));
score.setDeptid(scoreExcelParse.deptService.getDeptByName(score.getDeptname()).getId());
list.add(score);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
控制器代码
package com.bs.bus.controller;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.bus.common.ScoreExcelParse;
import com.bs.bus.entity.Score;
import com.bs.bus.service.IScoreService;
import com.bs.bus.vo.ScoreVo;
import com.bs.sys.common.*;
import com.bs.sys.entity.Dept;
import com.bs.sys.entity.Notice;
import com.bs.sys.entity.Role;
import com.bs.sys.entity.User;
import com.bs.sys.service.IDeptService;
import com.bs.sys.vo.NoticeVo;
import com.bs.sys.vo.UsersExcelVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
*
* 前端控制器
*
*
* @author lzb
* @since 2020-04-15
*/
@RestController
@RequestMapping("/bus/score")
public class ScoreController {
@Autowired
private IScoreService scoreService;
@Autowired
private IDeptService deptService;
@RequestMapping("exceltoScore")
public ResultObj excelToScore(String excelpath){
try {
List list= new ScoreExcelParse().ExceltoScore((new File(AppFileUtils.UPLOAD_PATH+"/"+excelpath)));
for(Score score:list){
scoreService.save(score);
}
return ResultObj.ADD_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ResultObj.ADD_ERROR;
}
}
@RequestMapping("loadallscore")
public DataGridView loadAllScore(ScoreVo scoreVo){
//从缓存获取登录用户
User user = (User) WebUtils.getSession().getAttribute("user");
//写不同登录用户角色能查看的数据
IPage page = new Page(scoreVo.getPage(),scoreVo.getLimit());
QueryWrapper queryWrapper = new QueryWrapper();
//进行模糊查询
queryWrapper.like(StringUtils.isNotBlank(scoreVo.getTestname()),"testname",scoreVo.getTestname());
queryWrapper.like(StringUtils.isNotBlank(scoreVo.getUsername()),"username",scoreVo.getUsername());
if(scoreVo.getDeptid()==null||scoreVo.getDeptid()==1){
queryWrapper.like("deptid","");
}
else {
queryWrapper.like(StringUtils.isNotBlank(deptService.getById(scoreVo.getDeptid()).getName()),"deptname",deptService.getById(scoreVo.getDeptid()).getName());
}
queryWrapper.orderByAsc(("userid"));
scoreService.page(page,queryWrapper);
List list=page.getRecords();
return new DataGridView(page.getTotal(),page.getRecords());
}
@RequestMapping("delectscore")
public ResultObj delectScore(ScoreVo scoreVo){
try {
scoreService.removeById(scoreVo);
return ResultObj.DELETE_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ResultObj.DELETE_ERROR;
}
}
/**
* 批量删除公告
* @param scoreVo
* @return
*/
@RequestMapping("batchdelectscore")
public ResultObj batchDeleteScore(ScoreVo scoreVo){
try {
Collection idList = new ArrayList<>();
for (Integer id : scoreVo.getIds()) {
idList.add(id);
}
scoreService.removeByIds(idList);
return ResultObj.DELETE_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ResultObj.DELETE_ERROR;
}
}
}
最后的页面效果
整个项目源码请参考https://github.com/yorkmass/Yark-AdminMS