上传文件页面回显_数据回显、删除以及excel导出
生活随笔
收集整理的這篇文章主要介紹了
上传文件页面回显_数据回显、删除以及excel导出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據回顯
當點擊某個按鈕跳轉某個頁面之前,發送請求到后臺進行數據查詢,最后將查詢結果返回給前端頁面,前端頁面獲取數據,最后呈現出來的效果是就回顯的效果。
示例代碼:
前端代碼:
Controller
Service
刪除功能,舉例,當一個部門需要將信息刪除,需要滿足該部門下沒有員工信息,沒有子部門信息,這樣才能將部門信息刪除,否則進行提示無法刪除。在EasyUI中也有類似的,比如樹,要刪除樹中的文件夾需要先判斷該文件夾中是否存有子文件等..
Service
//刪除@Overridepublic Result deleteDeptByDeptId(String[] deptIds) throws Exception {List<String> deptName = new ArrayList<>();for (int i = 0; i < deptIds.length; i++) {String deptId = deptIds[i];//判斷是否有子部門if (isHaveChildDept(deptId)) {DeptP deptP = deptPMapper.selectByPrimaryKey(deptId);deptName.add(deptP.getDeptName());continue;}//有員工的部門不能刪除if (isHaveUser(deptId)) {DeptP deptP = deptPMapper.selectByPrimaryKey(deptId);deptName.add(deptP.getDeptName());continue;}deptPMapper.deleteByPrimaryKey(deptId);}if (deptName.size() > 0) {return new Result(400, null, deptName);}return new Result(200, "刪除成功", null);}//判斷該部門是否有員工private boolean isHaveUser(String deptId) {UserPExample userPExample = new UserPExample();UserPExample.Criteria criteria = userPExample.createCriteria();criteria.andDeptIdEqualTo(deptId);List<UserP> userPList = userPMapper.selectByExample(userPExample);if (userPList != null && userPList.size() > 0) {return true;}return false;}//判斷該部門是否有子部門private boolean isHaveChildDept(String deptId) {DeptPExample deptPExample = new DeptPExample();DeptPExample.Criteria criteria = deptPExample.createCriteria();criteria.andParentIdEqualTo(deptId);List<DeptP> deptPList = deptPMapper.selectByExample(deptPExample);if (deptPList != null && deptPList.size() > 0) {return true;}return false;} }excel表格導出
依賴
<dependencies><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.10-FINAL</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.10-FINAL</version></dependency></dependencies>ExportExcelUtil
public class ExportExcelUtil {private CellStyle cs;/*** 描述:根據文件路徑獲取項目中的文件* * @param path* 文件路徑* @param filePath* @return* @throws Exception*/public File getExcelTplFile(String path, String filePath) throws Exception {String classDir = null;String fileBaseDir = null;File file = null;file = new File(path+filePath);if (!file.exists()) {throw new Exception("模板文件不存在!");}return file;}/*** 創建工作簿* @param file* @return* @throws Exception*/public Workbook getWorkbook(File file) throws Exception {FileInputStream fis = new FileInputStream(file);Workbook wb = new ImportExcelUtil().getWorkbook(fis, file.getName()); // 獲取工作薄return wb;}/*** 創建Sheet* @param wb* @param sheetName* @return* @throws Exception*/public Sheet getSheet(Workbook wb, String sheetName) throws Exception {cs = setSimpleCellStyle(wb); // Excel單元格樣式Sheet sheet = wb.getSheet(sheetName);return sheet;}/*** 創建Row* @param sheet* @return* @throws Exception*/public Row createRow(Sheet sheet) throws Exception {// 循環插入數據int lastRow = sheet.getLastRowNum() + 1; // 插入數據的數據ROWRow row = sheet.createRow(lastRow);return row;}/*** 創建Cell* @param row* @param CellNum* @return* @throws Exception*/public Cell createCell(Row row, int CellNum) throws Exception {Cell cell = row.createCell(CellNum);cell.setCellStyle(cs);return cell;}/*** 描述:設置簡單的Cell樣式* * @return*/public CellStyle setSimpleCellStyle(Workbook wb) {CellStyle cs = wb.createCellStyle();cs.setBorderBottom(CellStyle.BORDER_THIN); // 下邊框cs.setBorderLeft(CellStyle.BORDER_THIN);// 左邊框cs.setBorderTop(CellStyle.BORDER_THIN);// 上邊框cs.setBorderRight(CellStyle.BORDER_THIN);// 右邊框cs.setAlignment(CellStyle.ALIGN_CENTER); // 居中return cs;}}ImportExcelUtil
public class ImportExcelUtil {private final static String excel2003L =".xls"; //2003- 版本的excelprivate final static String excel2007U =".xlsx"; //2007+ 版本的excel/*** 描述:獲取IO流中的數據,組裝成List<List<Object>>對象* @param in,fileName* @return* @throws IOException */public List<List<Object>> getListByExcel(InputStream in,String fileName) throws Exception{List<List<Object>> list = null;//創建Excel工作薄Workbook work = this.getWorkbook(in,fileName);if(null == work){throw new Exception("創建Excel工作薄為空!");}Sheet sheet = null;Row row = null;Cell cell = null;list = new ArrayList<List<Object>>();//遍歷Excel中所有的sheetfor (int i = 0; i < work.getNumberOfSheets(); i++) {sheet = work.getSheetAt(i);if(sheet==null){continue;}//遍歷當前sheet中的所有行for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum()+1; j++) {row = sheet.getRow(j);if(row==null||row.getFirstCellNum()==j){continue;}//遍歷所有的列List<Object> li = new ArrayList<Object>();for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {cell = row.getCell(y);li.add(this.getCellValue(cell));}list.add(li);}}return list;}/*** 描述:根據文件后綴,自適應上傳文件的版本 * @param inStr,fileName* @return* @throws Exception*/public Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{Workbook wb = null;String fileType = fileName.substring(fileName.lastIndexOf("."));if(excel2003L.equals(fileType)){wb = new HSSFWorkbook(inStr); //2003-}else if(excel2007U.equals(fileType)){wb = new XSSFWorkbook(inStr); //2007+}else{throw new Exception("解析的文件格式有誤!");}return wb;}/*** 描述:對表格中數值進行格式化* @param cell* @return*/public Object getCellValue(Cell cell){Object value = null;DecimalFormat df = new DecimalFormat("0"); //格式化number String字符SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化DecimalFormat df2 = new DecimalFormat("0.00"); //格式化數字switch (cell.getCellType()) {case Cell.CELL_TYPE_STRING:value = cell.getRichStringCellValue().getString();break;case Cell.CELL_TYPE_NUMERIC:if("General".equals(cell.getCellStyle().getDataFormatString())){value = df.format(cell.getNumericCellValue());}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){value = sdf.format(cell.getDateCellValue());}else{value = df2.format(cell.getNumericCellValue());}break;case Cell.CELL_TYPE_BOOLEAN:value = cell.getBooleanCellValue();break;case Cell.CELL_TYPE_BLANK:value = "";break;default:break;}return value;} }方法
public void export(PageBean pageBean, Model model,HttpServletRequest request, HttpServletResponse response)throws Exception{//1、查詢要導出的數據PageBean pb = deptService.listDeptOfPage(pageBean);List<DeptVo> datas = (List<DeptVo>) pb.getDatas();//2、將數據寫入到excel表格中//創建導出工具類對象ExportExcelUtil excelUtil = new ExportExcelUtil();String realPath = request.getSession().getServletContext().getRealPath("/");String filePath = "/tpl/dept_export.xlsx";//模板File exceTpFile = excelUtil.getExcelTplFile(realPath,filePath);//工作簿Workbook workbook = excelUtil.getWorkbook(exceTpFile);//sheetSheet sheet = excelUtil.getSheet(workbook, "部門信息");for(int i = 0; i<datas.size();i++){DeptVo deptVo = datas.get(i);//行數Row row = excelUtil.createRow(sheet);Cell cell0 = excelUtil.createCell(row,0);cell0.setCellValue(deptVo.getDeptNo());Cell cell1 = excelUtil.createCell(row,1);cell1.setCellValue(deptVo.getParentDeptName());Cell cell2 = excelUtil.createCell(row,2);cell2.setCellValue(deptVo.getDeptName());Cell cell3 = excelUtil.createCell(row,3);cell3.setCellValue(deptVo.getState()==1?"啟用":"停用");}//3、把寫好的excel發送到客戶端String fileName="dept_list.xlsx";response.setContentType("application/ms-excel");response.setHeader("Content-disposition", "attachment;filename="+fileName);ServletOutputStream ouputStream = response.getOutputStream();workbook.write(ouputStream);ouputStream.flush();ouputStream.close();} 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的上传文件页面回显_数据回显、删除以及excel导出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三维点云数据处理软件供技术原理说明_海量
- 下一篇: ffmpeg 声音参数_ffmpeg转换