ex表格导入代码

package com.cn.xxx;


@PermInfo(value = "成绩管理", pval = "xx:xx:xx")
@RestController
@RequestMapping("/xx")
public class Controller {
    @Autowired
    private XXXX xxxx;


    /**
     * Excel模板导入考试成绩
     */
    @RequestMapping(value = "/saveExamGradesExcel", method = RequestMethod.POST)
    @RequiresPermissions("detect:examResult:saveExamGradesExcel")
    @PermInfo("成绩导入")
    public Json saveExamGradesExcel(@RequestBody @RequestParam("file") MultipartFile file, Entity en) {
        if (en.getSemesterId() == null || en.getSemesterId() == 0) {
            return Json.fail("msg", "请选择xx");
        }

        //初始化对象
        XXX XXX= new XXX();

        Workbook workbook = null;
        XSSFWorkbook workbook2 = null;
        HSSFWorkbook workbook3 = null;
        String message = null;
        try {
            //获取excel文件名
            String excleName = file.getOriginalFilename();
            //获取输入流
            InputStream inputStream = file.getInputStream();
            workbook2 = new XSSFWorkbook(inputStream);
            inputStream.close();

            //工作表对象
            XSSFSheet xssfSheet = workbook2.getSheetAt(0);
            Map<Integer, String> map = ExcelToPicUtil.getPictures2(xssfSheet);
            //总行数
            int rowLength = xssfSheet.getLastRowNum() + 1;
            //工作表的列
            Row row = xssfSheet.getRow(0);
            //总列数
            int colLength = row.getLastCellNum();
            //得到指定的单元格
            Cell cell = row.getCell(0);
            System.out.println("开始导入校验");
            for (int i = 0; i < rowLength; i++) {
                row = xssfSheet.getRow(i);

                if (i == 0 && colLength > 3) {
                    for (int j = 0; j < colLength; j++) {
                        cell = row.getCell(j);
                        //获取数据
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String moduleName = cell.getStringCellValue();
                    }
                }
                //从第二行开始获取数据
                if (i != 0) {
                    int temp = 0;
                    for (int j = 0; j < colLength; j++) {
//                        System.out.println("第" + i + "行" + "第" + j + "列");
                        cell = row.getCell(j);
                        //第一个格子
                        if (j == 0) {
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            String stuNo = cell.getStringCellValue();
                            //stuNo应该是这个格子的信息
                            if (stuNo == null || stuNo.equals("")) {
                                messages.add("第" + i + "行" + "第" + j + "列错误,xxx,请重新导入");
                                continue;
                            } else {
                                //查学号在不在
                                int countNo = stuMapper.selectCount(stuNo);
                                if (countNo == 0) {
                                    messages.add("第" + i + "行" + "第" + j + "列错误,xxx,请重新导入");
                                    continue;
                                } else {
                                    获取数据
                                }
                            }

                        }
//                    if (j == 1) {
//                        cell.setCellType(Cell.CELL_TYPE_STRING);
//                        String stuName = cell.getStringCellValue();
//                        stu.setStuName(stuName);
//                    }
                        if (j >= 2) {
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            String moduleScore = cell.getStringCellValue();
                            if (moduleScore == null || moduleScore.equals("")) {
                                messages.add("第" + i + "行" + "第" + j + "列错误,xxx");
	                            continue;
                            } else {
                                获取数据
                            }
                        }
                    }
                }
            }
            //将所有格式正确的数据存入集合中,最后在这里将其添加到考试成绩表和模块成绩表
            if (list != null && list.size() > 0) {
                for (Entity item : list) {
                    //插入成绩
                    scoreMapper.insert(item);
                }
            }
            if (messages.size() > 0) {
                messages.add("false");
            }

            return Json.succ("msg", "模板导入考试成绩成功!").data("messages", messages);
        } catch (Exception e) {
            e.printStackTrace();
            return Json.fail("模板导入考试成绩失败,请联系管理员");
        }

    }

上一篇:【Laravel3.0.0源码阅读分析】消息类message.php


下一篇:java对redis的基本操作(初识)