导入Excel表格

导入poi的jar包

Java代码:

 public String importGradeMessage(MultipartFile file) throws Exception{
        InputStream in=null;
        try {
            List<AClassDO> aClassDOList=new ArrayList<>();
            //得到上传的文件,转换成输入流
            in = file.getInputStream();

            //得到Excel
            Workbook readWb = null;
            //得到sheet
            Sheet readSheet =null;
            //判断导入Excel的格式
            if(file.getContentType().equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")){
                readWb = new XSSFWorkbook(in);
                readSheet=readWb.getSheetAt(0);
            } else {
                readWb = new HSSFWorkbook(in);
                readSheet=readWb.getSheetAt(0);
            }

            for (Row r : readSheet) {
                //第一行不是数据不需要添加到teacher中
                if (r.getRowNum() < 1) {
                    continue;
                }
                //实例化AGradeDO对象
                AClassDO aClassDO = new AClassDO();
                /...操作数据../
                
            }
           //批量插入数据
            batchInsert(aClassDOList,userAccount);
            return "插入数据成功!";
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (in!=null)
                in.close();
        }
        return "其他异常导致上传失败!";
    }

html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form id="form" action="提交路径" method="post">
        <input type="file" id="file" name="file" value="Excel表格">
        <input type="submit" id="submit" value="上传Excel表格">
    </form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_38723919/article/details/80019959
今日推荐