java poi导入excel表格数据通常都很实用,本案例使用了前端layui框架table表格来显示数据,poi读取excel表格的项目源码下载地址:http://pan.baidu.com/s/1jIh4DJC 密码:dz3f,先看看效果,如图。
我们将使用poi导入下面这种格式的excel表格,excel工作薄中有2张sheet工作表,如图所示。
下面来开始学习如何使用poi读取excel表格。
第一步:pom.xml中引入poi maven,建议使用poi 3.16,因为以下版本有漏洞。
<!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.16</version> </dependency>
第二步:poi导入项目的前端jsp页面,代码如下,使用了ajax提交form表单file数据,并返回数据显示在前端界面中。
<%-- Created by IntelliJ IDEA. User: ts Date: 2017/9/1 Time: 22:39 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>poi导入excel</title> <script type="text/javascript" src="./lib/jquery/1.9.1/jquery.min.js"></script> <!-- 使用了前端框架layui显示表格,自己去官网下载引入 --> <link type="text/css" href="./lib/layui-v2.0.2/css/layui.css" rel="stylesheet"> <script type="text/javascript" src="./lib/layui-v2.0.2/layui.all.js"></script> </head> <body> <div style="margin-top: 100px;margin-left: 200px;"> <form id="formId" enctype="multipart/form-data"> 选择文件:<input type="file" name="file"> <input type="button" onclick="submitForm();" value="导入excel"> </form> <hr/> 您导入的excel用户数据如下:<br/> <table class="layui-table" style="width: 800px"> <thead> <tr> <th>ID</th> <th>用户名</th> <th>密码</th> <th>电话</th> <th>地址</th> <th>分数</th> <th>操作</th> </tr> </thead> <tbody id="tbody"> </tbody> </table> </div> <script> function submitForm(){ //ajax提交带有文件上传的form表单 var formData = new FormData($("#formId")[0]); $.ajax({ url:"./user/importExcel", type:"POST", data:formData, processData: false, contentType: false, success:function (data) { var content = ""; $.each(data.rows, function (i, o) { content += "<tr>"; content += "<td>"+o.id+"</td>"; content += "<td>"+o.username+"</td>"; content += "<td>"+o.password+"</td>"; content += "<td>"+o.phone+"</td>"; content += "<td>"+o.address+"</td>"; content += "<td>"+o.score+"</td>"; content += "<td><a>删除</a></td>"; content += "</tr>"; }); $('#tbody').html(content); }, error:function(erro){ alert("提交错误!"); } }); } </script> </body> </html>
第三步:controller实现file文件上传接口,并将excel数据解析存储到mysql数据库中,代码如下。
/** * 直接使用InputStream file接收参数也可以 * MultipartFile为springmvc框架对象 * @param file * @return */ @RequestMapping(value="/user/importExcel",method=RequestMethod.POST) @ResponseBody public Map<String, Object>importExcel(MultipartFile file) throws Exception { Map<String, Object> resultMap = new HashMap<String, Object>(); InputStream in = file.getInputStream(); XSSFWorkbook workbook = new XSSFWorkbook(in); for (int i=0;i<workbook.getNumberOfSheets();i++){ List<User> list = new ArrayList<User>(); //获取工作薄中的工作表 XSSFSheet sheet = workbook.getSheetAt(i); //遍历工作表的行,从第3行开始读数据(索引从0开始) for (int x=2;x<sheet.getPhysicalNumberOfRows();x++){ User user = new User(); XSSFRow row = sheet.getRow(x); //如果此工作表有数据 if (row != null){ //遍历每行中每列的数据 for (int y = 0; y < row.getPhysicalNumberOfCells(); y++) { //获取单元格 XSSFCell cell = row.getCell(y); //获取单元格的值 cell.setCellType(CellType.STRING); String cellValue = cell.getStringCellValue(); if (y == 0){ user.setId(null); }else if (y == 1){ user.setUsername(cellValue == null?"":cellValue); }else if (y == 2){ user.setPassword(cellValue == null?"":cellValue); }else if (y == 3){ user.setPhone(cellValue == null?"":cellValue); }else if (y == 4){ user.setAddress(cellValue == null?"":cellValue); }else if (y == 5){ user.setScore(cellValue == null?0:new Integer(cellValue)); } } } list.add(user); } //每5行数据插入到数据库,以免造成内存溢出(实际中远远不止这么大) if (list.size()%5 == 0){ userService.insetUserList(list); list.clear(); } } //读取完毕则关闭流 in.close(); workbook.close(); //将用户数据显示在前台 List<User> list2 = userMapper.selectAll(); resultMap.put("rows",list2); return resultMap; }
不懂的可以下载项目源码下来自己研究,很简单的。