要實現POI導出EXCEL形如
--A1(LV1)
----B1(LV2)
----B2(LV2)
------C1(LV3)
------C2(LV3)
----B3(LV2)
--A1(LV1)
一.能用到的數據字段
ID(主鍵)、PID(父科目ID)、NAME(名稱)、LEVEL(當前層級)
二.思路:
定義一個空的科目列表用來存放已經排好序的科目。創建一個迭代器傳入父節點查出它的所有子節點,遍歷子節點插入到科目列表的同時再次調用這個迭代器,這樣程序能夠在插入一個節點后如果發現有子節點繼續插入子節點。最后返回排好序的科目樹。
三.JAVA代碼
/** * * <p>Description: 根據根節點獲取嵌套科目樹</p> * @param root 根節點,只需itemCode * @return 嵌套科目樹 */ public List<Subject> getSubjectListByRoot(Subject root){ //定義一個空的科目列表 List<Subject> subjectTree = new ArrayList<Subject>(); //查詢出根節點的具體信息 root = this.subjectManager.searchSubjectByCode(root.getItemCode()); //將根節點插入到嵌套科目樹里,因為迭代出來的科目樹是沒有根節點的 subjectTree.add(root); //調用迭代器 subjectTree = SubjectIteration(root, subjectTree); return subjectTree; } /** * * <p>Description: 嵌套科目樹迭代器</p> * @param subject 父節點 * @param subjectTree 科目樹 * @return 嵌套科目樹 */ public List<Subject> SubjectIteration(Subject subject, List<Subject> subjectTree) { //根據父節點查子節點 List<Subject> subjects = this.subjectManager.searchListSubjectByParent(subject); if (subjects != null) { //遍歷子節點並迭代 for (Subject subjectResult : subjects) { //將節點插入到嵌套科目樹里 subjectTree.add(subjectResult); SubjectIteration(subjectResult,subjectTree); } } return subjectTree; }
四.SQL
根據父節點查子節點的一段SQL只需比對父節點的ID和子節點的PID即可
五.POI導出
/** * * <p>Description: 科目樹導出</p> * @param request * @param response * @return Excel文件 * @throws Exception */ public ModelAndView downloadExcel(HttpServletRequest request, HttpServletResponse response) throws Exception { //文件配置 String fileName = new String(("科目導出文件.xls").getBytes("UTF-8"), "iso-8859-1"); response.reset(); response.setHeader("Content-disposition", "attachment;filename=" + fileName); response.setContentType("application/vnd.ms-excel;charset=utf-8"); //創建POI-workbook文件 XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("科目樹"); //插入抬頭 PoiUtil.addRowSXSSF(sheet, 0, new String[] { "說明:", "level1", "level2", "level3", "level4", "level5", "level6", "level7", "level8" }); //調整sheet樣式 //紅 CellStyle styleRed = workbook.createCellStyle(); styleRed.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); styleRed.setFillForegroundColor(IndexedColors.RED.index); //綠 CellStyle styleGreen = workbook.createCellStyle(); styleGreen.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); styleGreen.setFillForegroundColor(IndexedColors.LIME.index); //灰 CellStyle styleGrey = workbook.createCellStyle(); styleGrey.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); styleGrey.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index); //白 CellStyle styleWhite = workbook.createCellStyle(); styleWhite.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); styleWhite.setFillForegroundColor(IndexedColors.WHITE.index); //金黃 CellStyle styleGold = workbook.createCellStyle(); styleGold.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); styleGold.setFillForegroundColor(IndexedColors.GOLD.index); //黃 CellStyle styleYellow = workbook.createCellStyle(); styleYellow.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); styleYellow.setFillForegroundColor(IndexedColors.YELLOW.index); //亮黃 CellStyle styleLight = workbook.createCellStyle(); styleLight.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); styleLight.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.index); //檸檬 CellStyle styleLemon = workbook.createCellStyle(); styleLemon.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); styleLemon.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.index); //設置抬頭顏色 XSSFRow row = sheet.getRow(0); Cell cell = row.getCell(1); cell.setCellStyle(styleRed); cell = row.getCell(2); cell.setCellStyle(styleGreen); cell = row.getCell(3); cell.setCellStyle(styleGrey); cell = row.getCell(4); cell.setCellStyle(styleWhite); cell = row.getCell(5); cell.setCellStyle(styleGold); cell = row.getCell(6); cell.setCellStyle(styleYellow); cell = row.getCell(7); cell.setCellStyle(styleLight); cell = row.getCell(8); cell.setCellStyle(styleLemon); //查詢Excel用科目樹 List<Subject> subjectTree = new ArrayList<Subject>(); Subject root = new Subject(); root.setItemCode("1001"); subjectTree = getSubjectListByRoot(root); //插入數據 int index = 0; for(Subject subject : subjectTree){ index = index + 1; Row dataRow = sheet.createRow(index); String lv = subject.getLevel(); if(lv.equals("1")){ Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(subject.getItemName()); dataCell.setCellStyle(styleRed); } else if(lv.equals("2")){ Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(subject.getItemName()); dataCell.setCellStyle(styleGreen); } else if(lv.equals("3")){ Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(subject.getItemName()); dataCell.setCellStyle(styleGrey); } else if(lv.equals("4")){ Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(subject.getItemName()); dataCell.setCellStyle(styleWhite); } else if(lv.equals("5")){ Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(subject.getItemName()); dataCell.setCellStyle(styleGold); } else if(lv.equals("6")){ Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(subject.getItemName()); dataCell.setCellStyle(styleYellow); } else if(lv.equals("7")){ Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(subject.getItemName()); dataCell.setCellStyle(styleLight); } else if(lv.equals("8")){ Cell dataCell = dataRow.createCell(0); dataCell.setCellValue(subject.getItemName()); dataCell.setCellStyle(styleLemon); } } sheet.setColumnWidth(0, 9000); //創建輸出流,生成文件 OutputStream out = new BufferedOutputStream(response.getOutputStream()); workbook.write(out); out.flush(); out.close(); return null; }