您当前的位置: 首页 >  Java

qq_34412985

暂无认证

  • 0浏览

    0关注

    1061博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

java 实现excel中的数据导入到数据库的功能

qq_34412985 发布时间:2019-07-18 23:19:56 ,浏览量:0

java web项目导入excel获取数据,是实用频率非常高的功能,通过做了几个这样的功能之后,现将此功能总结出了,为了以后自己方便使用,也为大家实现此功能做一个参考.

项目框架

1,后台:spring+springmvc+mybatis

2,前台: bootstrap+jquery+ajax

3,项目管理:maven

说明.excel处理函数需要引入poi的jar包,在pom.xml引入一下代码

                org.apache.poi         poi         3.8                                       commons-codec                 commons-codec                                       org.apache.poi         poi-ooxml         3.8    

别的框架大体上也是可以的,只需稍微调整,如有问题,大家可留言讨论

实现的功能说明:将用户信息(姓名,性别,年龄)通过excel上传,并保存到数据库

具体代码如下

1,前台html代码

                 选择文件                                                                       上传

前台页面效果

excel上传样式

excel内容展示

excel内容

2,JS代码

    var User = function(){         this.init = function(){             //模拟上传excel              $("#uploadEventBtn").unbind("click").bind("click",function(){                  $("#uploadEventFile").click();              });              $("#uploadEventFile").bind("change",function(){                  $("#uploadEventPath").attr("value",$("#uploadEventFile").val());              });         };         //点击上传按钮         this.uploadBtn = function(){             var uploadEventFile = $("#uploadEventFile").val();             if(uploadEventFile == ''){                 alert("请选择excel,再上传");             }else if(uploadEventFile.lastIndexOf(".xls") 1 && sheet.getRow(0) != null) {                 this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();             }             List userList = new ArrayList();             // 循环Excel行数             for (int r = 1; r < totalRows; r++) {                 Row row = sheet.getRow(r);                 if (row == null) {                     continue;                 }                 User user = new User();                 // 循环Excel的列                 for (int c = 0; c < this.totalCells; c++) {                     Cell cell = row.getCell(c);                     if (null != cell) {                         if (c == 0) {                             // 如果是纯数字,比如你写的是25,cell.getNumericCellValue()获得是25.0,通过截取字符串去掉.0获得25                             if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {                                 String name = String.valueOf(cell.getNumericCellValue());                                 user.setName(name.substring(0, name.length() - 2 > 0 ? name.length() - 2 : 1));// 名称                             } else {                                 user.setName(cell.getStringCellValue());// 名称                             }                         } else if (c == 1) {                             if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {                                 String sex = String.valueOf(cell.getNumericCellValue());                                 user.setSex(sex.substring(0, sex.length() - 2 > 0 ? sex.length() - 2 : 1));// 性别                             } else {                                 user.setSex(cell.getStringCellValue());// 性别                             }                         } else if (c == 2) {                             if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {                                 String age = String.valueOf(cell.getNumericCellValue());                                 user.setAge(age.substring(0, age.length() - 2 > 0 ? age.length() - 2 : 1));// 年龄                             } else {                                 user.setAge(cell.getStringCellValue());// 年龄                             }                         }                     }                 }                 // 添加到list                 userList.add(user);             }             return userList;         }               /**          * 验证EXCEL文件          * @param filePath          * @return          */         public boolean validateExcel(String filePath) {             if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {                 errorMsg = "文件名不是excel格式";                 return false;             }             return true;         }               // @描述:是否是2003的excel,返回true是2003         public static boolean isExcel2003(String filePath) {             return filePath.matches("^.+\\.(?i)(xls)$");         }               // @描述:是否是2007的excel,返回true是2007         public static boolean isExcel2007(String filePath) {             return filePath.matches("^.+\\.(?i)(xlsx)$");         }     }

关注
打赏
1653291990
查看更多评论
立即登录/注册

微信扫码登录

0.0406s