<div id="Exdlg" class="easyui-dialog" style="width:400px;height:300px;padding:10px 20px" closed="true" buttons="#dlg-buttons"> <form method="post" action="indexMenu/uploadEx.do" enctype="multipart/form-data"> 请选择文件:<input type="file" class="btn btn-default" name="file"><br/> <input type="submit" class="btn btn-default" value="提交"> </form> </div>
public void uploadEx(HttpServletRequest request, HttpServletResponse response, MultipartFile file)throws IOException, ParseException { List<UserPartner> newList = new ArrayList<UserPartner>(); String cellStr = null;// 单元格,最终按字符串处理 // 时间的格式转换 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); // 创建实体对象 需要你创建实体 UserPartner userPartner = new UserPartner(); //记录数 int nos = 0; try { // 判断是否有输入流 if (file.getSize()!= 0) { // 转换成输入流 InputStream is = file.getInputStream(); // 得到excel XSSFWorkbook workbook = new XSSFWorkbook(is); // 得到sheet XSSFSheet sheet = workbook.getSheetAt(0); // 开始循环遍历行,表头不处理,从1开始 for (int i = 1; i <= sheet.getLastRowNum(); i++) { userPartner = new UserPartner();// 实例化Student对象 XSSFRow row = sheet.getRow(i);// 获取行对象 if (row == null) {// 如果为空,不处理 continue; } // 循环遍历单元格 for (int j = 0; j < row.getLastCellNum(); j++) { XSSFCell cell = row.getCell((short)j); if (cell == null) {// 单元格为空设置cellStr为空串 cellStr = ""; } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {// 对数字处理,默认是double类型 cellStr = (int) cell.getNumericCellValue() + ""; } else {// 其余按照字符串处理 cellStr = cell.getStringCellValue(); } // 下面按照数据出现位置封装到bean中 if (j == 0) { userPartner.setId(Long.valueOf(cellStr)); // id } else if (j == 1) { userPartner.setPartenrOne(cellStr); } else if (j == 2) { userPartner.setPartenrOpentId(cellStr); } else if (j == 3) { userPartner.setPartenrName(cellStr); } else if (j == 4) { userPartner.setPartenrPhone(cellStr); } else if (j == 5) { userPartner.setWechatNickname(cellStr); } } userPartnerService.add(userPartner,request); } } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } response.setContentType("text/html; charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); // 在火狐下不设置返回类型的话,默认返回的是text/xml(xmldomcument)对象了。无法进行1==1比较,登不进系统。 PrintWriter out = response.getWriter(); if (nos > 0) { System.out.println("添加成功"); out.print("1"); } else { System.out.println("失败"); out.print("0"); } out.flush(); out.close(); }