模板转载地址:https://www.cnblogs.com/zhangyangtao/p/9802948.html
直接上代码(我是基于ssm写的demo,导入文件目前只能读取.xls后缀的excel文件)
1 <!--导入的核心依赖--> 2 <dependency> 3 <groupId>net.sourceforge.jexcelapi</groupId> 4 <artifactId>jxl</artifactId> 5 <version>2.6.12</version> 6 </dependency>
1 //这是下载模板的方法 2 @RequestMapping("/downloadExcel.do") 3 @ResponseBody 4 public void downloadExcel(HttpServletResponse res, HttpServletRequest req) throws Exception { 5 //文件的名称 6 String fileName = "excelTemplate.xls"; 7 ServletOutputStream out; 8 res.setContentType("multipart/form-data"); 9 res.setCharacterEncoding("UTF-8"); 10 res.setContentType("text/html"); 11 //文件的路径 resource/template/1.xlsx 12 String filePath = getClass().getResource("/template/" + fileName).getPath(); 13 System.out.println(filePath); 14 String userAgent = req.getHeader("User-Agent"); 15 if (userAgent.contains("MSIE") || userAgent.contains("Trident")) { 16 fileName = java.net.URLEncoder.encode(fileName, "UTF-8"); 17 }else { 18 // 非IE浏览器的处理: 19 fileName = new String((fileName).getBytes("UTF-8"), "ISO-8859-1"); 20 } 21 filePath = URLDecoder.decode(filePath, "UTF-8"); 22 res.setHeader("Content-Disposition", "attachment;fileName=" + fileName); 23 FileInputStream inputStream = new FileInputStream(filePath); 24 out = res.getOutputStream(); 25 int b = 0; 26 byte[] buffer = new byte[1024]; 27 while ((b = inputStream.read(buffer)) != -1) { 28 // 4.写到输出流(out)中 29 out.write(buffer, 0, b); 30 } 31 inputStream.close(); 32 out.flush(); 33 out.close(); 34 // if (out != null) { 35 // out.flush(); 36 // out.close(); 37 // 38 // } 39 40 } 41
导入excel文件读取数据的方法
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-2)); 12 // 从数据行开始迭代每一行 13 for (int i = 2; i < rowNum; i++) { 14 // 先判断插入的数据是否和数据库的数据重复 15 System.out.println(sheet.getCell(0, i).getContents()); 16 if (userService.findUser(sheet.getCell(0, i).getContents()) > 0) { 17 continue; 18 } 19 User u = new User(); 20 // getCell(column,row),表示取得指定列指定行的单元格(Cell) 21 // getContents()获取单元格的内容,返回字符串数据。适用于字符型数据的单元格 22 // 使用实体类封装单元格数据 23 u.setName(sheet.getCell(0, i).getContents()); 24 u.setAge(sheet.getCell(1, i).getContents()); 25 u.setNickName(sheet.getCell(2, i).getContents()); 26 userService.saveUser(u); 27 System.out.println("成功插入:" + sheet.getCell(0, i).getContents()); 28 29 /* 30 * // 判断单元格的类型,单元格主要类型LABEL、NUMBER、DATE if (sheet.getCell(2, i).getType == 31 * CellType.NUMBER) { 32 * 33 * // 转化为数值型单元格 NumberCell numCell = (NumberCell) sheet.getCell(2, i); // 34 * NumberCell的getValue()方法取得单元格的数值型数据 info.setRscore(numCell.getValue()); 35 * 36 * } if (sheet.getCell(3, i).getType == CellType.NUMBER) { NumberCell numCell = 37 * (NumberCell) sheet.getCell(3, i); info.setRscore(numCell.getValue); } 38 * 39 * if (sheet.getCell(4, i).getType == CellType.DATE) { DateCell dateCell = 40 * (DateCell) sheet.getCell(4, i); // DateCell的getDate()方法取得单元格的日期型数据 41 * info.setDate(dateCell.getDate()); } 42 */ 43 } 44 fis.close(); 45 wk.close(); 46 }