• java后端导入excel将数据写入数据库


    参考:https://www.cnblogs.com/hanfeihanfei/p/7079210.html

     1 @RequestMapping("/importExcel.do")
     2     public void import2(String xlsPath) throws IOException, BiffException {
     3 
     4         // 导入已存在的Excel文件,获得只读的工作薄对象
     5         FileInputStream fis = new FileInputStream(xlsPath);
     6         Workbook wk = Workbook.getWorkbook(fis);
     7         // 获取第一张Sheet表
     8         Sheet sheet = wk.getSheet(0);
     9         // 获取总行数
    10         int rowNum = sheet.getRows();
    11         System.out.println("插入总行数:"+rowNum);
    12         // 从数据行开始迭代每一行
    13         for (int i = 0; i < rowNum; i++) {
    14             //先判断插入的数据是否和数据库的数据重复
    15             if(userService.findUser(sheet.getCell(0, i).getContents())>0) {
    16                 continue;
    17             }
    18             User u = new User();
    19             // getCell(column,row),表示取得指定列指定行的单元格(Cell)
    20             // getContents()获取单元格的内容,返回字符串数据。适用于字符型数据的单元格
    21             // 使用实体类封装单元格数据
    22             u.setName(sheet.getCell(0, i).getContents());
    23             u.setAge(sheet.getCell(1, i).getContents());
    24             u.setNickName(sheet.getCell(2, i).getContents());
    25             userService.saveUser(u);
    26             System.out.println("成功插入:"+sheet.getCell(0, i).getContents());
    27             
    28             
    29             
    30             /*// 判断单元格的类型,单元格主要类型LABEL、NUMBER、DATE
    31             if (sheet.getCell(2, i).getType == CellType.NUMBER) {
    32 
    33                 // 转化为数值型单元格
    34                 NumberCell numCell = (NumberCell) sheet.getCell(2, i);
    35                 // NumberCell的getValue()方法取得单元格的数值型数据
    36                 info.setRscore(numCell.getValue());
    37 
    38             }
    39             if (sheet.getCell(3, i).getType == CellType.NUMBER) {
    40                 NumberCell numCell = (NumberCell) sheet.getCell(3, i);
    41                 info.setRscore(numCell.getValue);
    42             }
    43 
    44             if (sheet.getCell(4, i).getType == CellType.DATE) {
    45                 DateCell dateCell = (DateCell) sheet.getCell(4, i);
    46                 // DateCell的getDate()方法取得单元格的日期型数据
    47                 info.setDate(dateCell.getDate());
    48             }*/
    49         }
    50         fis.close();
    51         wk.close();
    52     }

    注解都比较详细了

    前端代码(ie8和chrome前端获取的路径会出问题)

    <form>
            <input type="file" id="fileName" name="xlsPath" /> <input
                type="button" id="btn" value="提交">
        </form>
     1 $("#btn").click(function() {
     2             var xlsPath = document.getElementById("fileName").value;
     3             /* var path=getPath(xlsPath);
     4             alert(path); */
     5             $.ajax({
     6                 "url" : "${pageContext.request.contextPath}/importExcel.do",
     7                 "type" : "GET",
     8                 "data" : "xlsPath=" + xlsPath,
     9                 "dataType" : "json",
    10                 "success" : function(res) {
    11                     if (res.state == 1) {
    12                         alert(message);
    13                     } else {
    14                         alert(message);
    15                     }
    16                 }
    17             });
    18         });

    可以直接用

  • 相关阅读:
    nginx 配置https, 服务器是阿里云的ECS(亲测)
    jenkins 安装2.170版本 的问题汇中
    终于有人把“TCC分布式事务”实现原理讲明白了!
    springcloud(九) springboot Actuator + admin 监控
    springcloud(八) Hystrix监控
    springcloud(七) feign + Hystrix 整合 、
    springboot 2.0 自定义redis自动装配
    springboot 2.0 自动装配原理 以redis为例
    博文分类索引--Python
    【python】-- Ajax
  • 原文地址:https://www.cnblogs.com/zuoxh/p/9766977.html
Copyright © 2020-2023  润新知