Java导出excel时合并同一列中相同内容的行思路详解

时间:2022-10-26 14:08:37

一、有时候导出excel时需要按类别导出,一大类下好几个小类,小类下又有好几个小小类,就像下图:

Java导出excel时合并同一列中相同内容的行思路详解

要实现这个也不难,

思路如下:按照大类来循环,如上就是按照张江校区、徐汇校区、临港校区三个大类循环,然后再处理小类,因为本例小小类不涉及合并,所以只涉及处理小类,如果需要处理小小类,还需要在处理一下,具体实现原理同小类;

      每次循环时记录下此次循环的房屋类型和上次循环的房屋类型,两者相同时,要合并的结束行++,否者,说明这个房屋类型已经循环完毕(前提是各类型都按顺序order by 了,保证相同类型相邻),开始融合。具体实现如下:

二、实现代码,具体思路已经在注释中了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public void expstatistics(string filepath,string campuscode) {
   try {
     writableworkbook wwb = workbook.createworkbook(new file(filepath));
     jxlformatutil jfu = new jxlformatutil();
     writablesheet ws = wwb.createsheet("房屋报表统计", 0);
     string[] tablehead = {"校区名称:20","房屋类型:30","房屋用途:30","使用面积(㎡):20"};
     for (int i = 0; i < tablehead.length; i++) {//每一列的标题样式
       ws.addcell(new label(i, 0, tablehead[i].split(":")[0], jxlformatutil.wchead));
       ws.setcolumnview(i, integer.valueof(tablehead[i].split(":")[1]));
     }
     int col = 1;//从第1行开始
     //以下变量用来融合相同内容 的行
     int percol = col;
     string perpurpose = "";//上一个用途类型
     string purpose = "";//当前用途类型
     int startmergecol = 1;//要融合的第一行
     int endmergecol = 0;//要融合的最后行
     boolean flag = true;//用来记录是否是第一次循环
     //根据搜索条件导出
     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());//获取所有房屋信息
       if(null != builds && builds.size() > 0){
         for (fcxtbuild build : builds) {
           int row = 0;//从第0列开始
           ws.addcell(new label(row++, col, campus.getcampusname(), jxlformatutil.wccenter));
           purpose = build.getusefulpurpose();
           if(flag){//如果是第一次循环,将perpurpose初始化,以便后面的第一次判断
             perpurpose = purpose;
             flag = false;
           }
           ws.addcell(new label(row++, col, fcxtbuild.build_usefulpurpose.get(build.getusefulpurpose()), jxlformatutil.wccenter));
           if(purpose.equals(perpurpose)){
             endmergecol++;
           }else{//两者不相同时,融合之前相同的行
             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();
   }
 }

总结

以上所述是小编给大家介绍的java导出excel时合并同一列中相同内容的行思路详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/hpuiotcl/archive/2018/06/07/9150229.html