POI使用流程
POI使用流程
1、導入jar包 2、引入插件 <script type="text/javascript" src="${pageContext.request.contextPath}/jquery-2.1.1.min.js"></script> 3、在前臺寫一個button按鈕(考慮到服務器不在本地,所以做成下載的功能) <button?οnclick="toout()">導出</button> //按鈕有一個點擊事件,觸發后臺的action <script?type="text/javascript"> function?toout(){ window.location.href?=?"${pageContext.request.contextPath}/areaAction!areaList.jhtml"; } //兩表導出 function?exportOut(){ /*??window.location.href?=?"${pageContext.request.contextPath?}/userAction!exportList.jhtml";?*/ var?data?=?$("#index_form").serialize(); console.info(data); window.location.href=?"${pageContext.request.contextPath?}/userAction!exportTileList.jhtml?"?+?data; } </script> 4.進入后臺(dao—-service—action) dao和service主要查詢功能(ssh注解版框架) action類中: 導出——> 在util中引入工具類:ExportExcel.java、BaseAction.java 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();}} 導入<—— 在util中引入工具類:ExportExcel.java、BaseAction.java、fileUtil.java 1、在前臺寫一個按鈕 <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> 2、在<script>中寫方法 function detailUser(){var data = $("#input_form").serialize();console.info(data);window.location.href= "${pageContext.request.contextPath }/userAction!exportTileList.jhtml?" + data; 3、action?類 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);//創建對象,把對象放入listUser 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;}?
轉載于:https://www.cnblogs.com/12yang-ting/p/7521050.html
總結
- 上一篇: c# 日期格式化
- 下一篇: Jsoup(二)-- Jsoup查找DO