JXLS 2.4.0系列教程(三)——嵌套循环是怎么作到的

注:本文代码在第一篇文章基础上修改而成,请务必先阅读第一篇文章。html

http://www.cnblogs.com/foxlee1024/p/7616987.htmljava

本文也不会过多的讲解模板中遍历表达式的写法和说明,请先阅读第二篇文章。工具

http://www.cnblogs.com/foxlee1024/p/7617120.htmlthis

 

  本来第三篇文章我打算写sheet分页的实现的,后来发现难度比第四篇循环嵌套复杂一点点,咱们本着按部就班的原则,因此先讲讲怎么实现循环嵌套。spa

说明是嵌套循环?说白了就是大循环套小循环,请看下图:翻译

 

 

  咱们设想一下,有一条哆啦A梦的流水生产线,生产线上在生成这哆啦A梦。咱们知道,哆啦A梦的口袋中有不少不一样的道具,那么咱们在生成的时候就把这些道具预先放进哆啦A梦的口袋吧。设计

  每个产品的哆啦A梦拥有的道具都是不同的,这样咱们在表中就须要到了两个循环,第一层是哆啦A梦名称和拥有的道具,第二层循环是拥有的道具名称和道具功能。excel

  Main方法中导出的代码和原来没什么不一样,因此咱们先看看哆啦A梦的javabean是怎么设计的。code

public class Doraemon {
    private String name; // 哆啦A梦的名字
    private List<Tool> tools; // 拥有的道具,这是一个链表,存放的是Tool类
    
    public Doraemon(String name, List<Tool> tools) {
        super();
        this.name = name;
        this.tools = tools;
    }
    
    public Doraemon() {
    }
    
    /** 如下省略全部get/set方法,请自行添加 */
}

 

  接下来咱们看看Tool类:htm

public class Tool {
    private String toolName; // 道具名
    private String toolFunction; // 道具功能
    
    public Tool(String toolName, String toolFunction) {
        super();
        this.toolName = toolName;
        this.toolFunction = toolFunction;
    }
    
    public Tool() {
    }
    
    /** 如下省略全部get/set方法,请自行添加 */
}

 

  如今你们看明白了吗?其实就是在Doraemon 类中加入了一个List链表,泛型为Tool。能够预想的是,只要一层层建立好哆啦A梦这个对象(包括他的道具)后,再把多个多啦A梦放进一个链表中,而后传给Jxls工具就能够生成excel报表了。

  如今咱们看看Main方法是怎么写的,除了生成哆啦A梦对象的代码外,其余彻底没有改动。

public class TestMain2 {

    public static void main(String[] args) throws Exception {
        String templatePath = "E:/template6.xls";
        OutputStream os = new FileOutputStream("E:/out6.xls");
        
        Tool tool1 = new Tool("任意门","想去哪就去哪");
        Tool tool2 = new Tool("竹蜻蜓","想飞哪就飞哪");
        Tool tool3 = new Tool("空气炮","空气炮");
        Tool tool4 = new Tool("翻译饼干","翻译饼干");
        
        List<Doraemon> list = new ArrayList<Doraemon>();
        
        //制做一个哆啦A梦
        Doraemon doraemon1 = new Doraemon();
        //制做一号哆啦A梦的道具
        List<Tool> toolList1 = new ArrayList<Tool>();
        toolList1.add(tool1);
        toolList1.add(tool2);
        //设定一号哆啦A梦信息
        doraemon1.setName("哆啦A梦一号");
        doraemon1.setTools(toolList1);
        
        //制做一个哆啦A梦
        Doraemon doraemon2 = new Doraemon();
        //制做二号哆啦A梦的道具
        List<Tool> toolList2 = new ArrayList<Tool>();
        toolList2.add(tool3);
        toolList2.add(tool4);
        toolList2.add(tool1);
        //设定二号哆啦A梦信息
        doraemon2.setName("哆啦A梦二号");
        doraemon2.setTools(toolList2);
        
        list.add(doraemon1);
        list.add(doraemon2);
        
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("data", list);    
        
        JxlsUtils.exportExcel(templatePath, os, model);
        os.close();
        System.out.println("完成");
    }
}

 

  Main方法很少解释,就是new出一个新的哆啦A梦后给他set入对应的数据(包括名字和拥有的道具),而后把一众哆啦A梦放进一个链表中,再传进model中。

  重点是咱们看看模板应该怎么写。

 

 

  第一句不用多说,设定模板区域(我随便写的,能够写大一点):

jx:area(lastCell="I7")

  第二句是第一层循环,items是Main方法中model放入的键名,里面存放有装着生产线全部哆啦A梦对象的一个链表。他的var值是dora,在获取产品名称时就要写成${dora.name}。lastCell的值写C4,D4都行。

jx:each(items="data" var="dora" lastCell="D4")

  第三句是第二层循环,是哆啦A梦对象(Doraemon)里的tools属性,是个链表。因为上一层循环中Doraemon对象已经在var值中被命名成dora,因此第二层循环的items写成dora.tools。而且var值为tool。这就意味着Doraemon对象tools属性中的单条遍历记录被命名成tool。因此就能够在表达式中用${tool.toolName}方法获取道具的名字(toolName)了。

jx:each(items="dora.tools" var="tool" lastCell="D4")

  不明白的同窗请仔细品味一下,items至关于一个链表List,而var至关于链表中存放的单个对象。要使用这些对象就要给他们在var中命名。

  写好这三句注解后,就能够回到main方法中执行代码了。

 

相关文章
相关标签/搜索