POI使用流程javascript
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-2.1.1.min.js"></script>
public void userList(){ //定义一个String类型的字符串去接收标题名称,sheet页名称 String title = "用户信息"; //定义String类型 数组,把表头信息拼接进去 String[] rowName = new String[]{"序号","姓名","密码","建立时间","修改时间"}; //因为excel是多种数据类型,因此咱们定义一个object数组接收,这样减小代码冗余,提升重用率 List<Object[]> dataList = new ArrayList<Object[]>(); try { //将导出的数据放入List集合 (多表的集合) List<User> userList = userService.userList(user); //遍历list集合放入对象里 for (int i = 0; i < userList.size(); i++) { //定义对象数组[] Object[] obj = new Object[rowName.length]; //根据表头rowName的长度,给对象赋值 obj[0] = userList.get(i).getId(); obj[1] = userList.get(i).getName(); obj[2] = userList.get(i).getPwd(); obj[3] = userList.get(i).getCreatedatetime(); obj[4] = userList.get(i).getModifydatetime(); //将赋完值的obj对象放入刚才定义的dataList里 dataList.add(obj); } //已经获得title, rowName, dataList;放入到我写的工具类里 ,工具类有title, rowName, dataList //全局变量 ExportExcel exportExcel = new ExportExcel(title, rowName, dataList); //运行导出export方法 exportExcel.export(); } catch (Exception e) { e.printStackTrace(); } }
<form id="input_form" action="${pageContext.request.contextPath }/userAction!inputUserFile.jhtml" method="post" enctype="multipart-form-data"> <input type="file" name="excleFile"> <input type="submit" value="导入文件"> </form>
function detailUser(){ var data = $("#input_form").serialize(); console.info(data); window.location.href= "${pageContext.request.contextPath }/userAction!exportTileList.jhtml?" + data;
private File excleFile;(生成get set方法) private String excleFileFileName;(生成get set方法) public void inputUserFile(){ //得到绝对路径至文件夹 String realPath = ServletActionContext.getServletContext().getRealPath(""); //得到上传后的文件名 String upLoadFile = FileUtil.upLoadFile(excleFile, excleFileFileName, "aa");//aa是tomcat下的文件夹 //文件在服务器的绝对路径 String FilePath = realPath + "/" + upLoadFile; System.out.println(FilePath); List<User> list = new ArrayList<User>(); //判断是不是.xls文件 版本不一样(.xls为2003版,.xlsx为2007版) if(upLoadFile.endsWith(".xls")){ try { //建立工做簿 HSSFWorkbook book = new HSSFWorkbook(new FileInputStream(new File(FilePath))); //得到当前sheet页 book.getSheetAt(0); //遍历sheet页 for (int i = 0; i < book.getNumberOfSheets(); i++) { //建立sheet页 HSSFSheet sheet = book.getSheetAt(i); //遍历当前第4行下表为3(前面3行为表头信息) for (int j = 3; j < sheet.getPhysicalNumberOfRows(); j++) { //建立行 HSSFRow row = sheet.getRow(j); //建立对象,把对象放入list User user1 = new User(); SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //经过id赋值 if(UserAction.getCellValue(row.getCell(0)) !=null &&!UserAction.getCellValue(row.getCell(0)).equals("")){ user1.setId(Long.valueOf(UserAction.getCellValue(row.getCell(0)))); } //姓名 user1.setName(UserAction.getCellValue(row.getCell(1))); user1.setPwd(UserAction.getCellValue(row.getCell(2))); //建立时间 if(UserAction.getCellValue(row.getCell(3))!=null&&!UserAction.getCellValue(row.getCell(3)).equals("")){ user1.setCreateDate(sdf.parse(UserAction.getCellValue(row.getCell(3)))); } //修改时间 if(UserAction.getCellValue(row.getCell(4))!=null&&!UserAction.getCellValue(row.getCell(4)).equals("")){ user1.setModifyDate(sdf.parse(UserAction.getCellValue(row.getCell(4)))); } //放入list集合 list.add(user1); } } //遍历集合 for (User user : list) { userService.addUser(user); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }else if(upLoadFile.endsWith(".xlsx")){ } } ———————————格式设置——————————— // 判断从Excel文件中解析出来数据的格式 private static String getCellValue(HSSFCell cell) { String value = null; // 简单的查检列类型 switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING:// 字符串 value = cell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_NUMERIC:// 数字 long dd = (long) cell.getNumericCellValue(); value = dd + ""; break; case HSSFCell.CELL_TYPE_BLANK: value = ""; break; case HSSFCell.CELL_TYPE_FORMULA: value = String.valueOf(cell.getCellFormula()); break; case HSSFCell.CELL_TYPE_BOOLEAN:// boolean型值 value = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: value = String.valueOf(cell.getErrorCellValue()); break; default: break; } return value; }