1. Sometimes when exporting Excel, you need to export by category. There are several subcategories under one major category, and there are several subcategories under the subcategories, just like the following picture:
It's not difficult to achieve this.
The idea is as follows: Circulate according to the major categories. For example, the above is to cycle according to the three major categories of Zhangjiang Campus, Xuhui Campus, and Lingang Campus, and then process the minor categories. Because this example does not involve merging, it only involves processing small categories. If you need to process small categories, you still need to deal with them. The specific implementation principle is the same as the minor categories;
Each cycle, record the house type of this cycle and the house type of the last cycle. When the two are the same, the end row to be merged ++. Otherwise, it means that the house type has been completed (provided that each type is ordered by in order, ensuring that the same type is adjacent) and begins to merge. The specific implementation is as follows:
2. Implement the code, the specific idea is already in the comments
public void expStatistics(String filePath,String campuscode) { try { WritableWorkbook wwb = Workbook.createWorkbook(new File(filePath)); JxlFormatUtil Jfu = new JxlFormatUtil(); WritableSheet ws = wwb.createSheet("House Report Statistics", 0); String[] tableHead = {"Campus Name: 20","House Type: 30","House Use: 30","User Area(): 20"}; for (int i = 0; i < tableHead.length; i++) {//The title style of each column ws.addCell(new Label(i, 0, tableHead[i].split(":")[0], JxlFormatUtil.wcHead)); ws.setColumnView(i, Integer.valueOf(tableHead[i].split(":")[1])); } int col = 1;//Start from line 1//The following variables are used to fuse the same content int perCol = col; String perPurpose = "";//The previous use type String purpose = "";//The current use type int startMergeCol = 1;//The first line to be fused int endMergeCol = 0;//The last line to be fused boolean flag = true;//Use to record whether it is the first loop//Export List<FcxtCampus> campusList = new ArrayList<FcxtCampus>(); if(null != campuscode && !"".equals(campuscode)){ campusList = campusdao.findCampusByCode(campuscode); }else { campusList = campusdao.findCampus(); } for (FcxtCampus campus: campusList) { List<FcxtBuild> builds = builddao.statisticsBuilds(campus.getCampuscode());//Get all house information if(null != builds && builds.size() > 0){ for (FcxtBuild build : builds) { int row = 0;//Start from column 0 ws.addCell(new Label(row++, col, campus.getCampusname(), JxlFormatUtil.wcCenter)); purpose = build.getUsefulpurpose(); if(flag){//If it is the first loop, initialize perPurpose so that the subsequent first judgment perPurpose = purpose; flag = false; } ws.addCell(new Label(row++, col, FcxtBuild.BUILD_USEFULPURPOSE.get(build.getUsefulpurpose()), JxlFormatUtil.wcCenter)); if(purpose.equals(perPurpose)){ endMergeCol++; }else{//When the two are different, the same lines before fusion ws.mergeCells(1, startMergeCol, 1, endMergeCol); startMergeCol = col; endMergeCol = startMergeCol; perPurpose = purpose; } ws.addCell(new Label(row++, col, FcxtBuild.BUILD_BUILDINGTYPE.get(build.getBuildingtype()), JxlFormatUtil.wcCenter)); ws.addCell(new Label(row++, col, build.getBuildarea().toString(), JxlFormatUtil.wcCenter)); col++; } ws.mergeCells(0, perCol, 0, col-1); perCol = col; } } wwb.write(); wwb.close(); } catch (Exception e) { e.printStackTrace(); } }Summarize
The above is a detailed explanation of the line ideas of merging the same content in the same column when exporting Java Excel. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!