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; }