1.java代码
package com.knife.jxls;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
public class TestJxls {
public static class Foo {
public String id;
public String name;
public int age;
public Foo(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
public static void main(String args[]) throws IOException {
List<Foo> foolist=new ArrayList<Foo>();
foolist.add(new Foo("001","lee",11));
foolist.add(new Foo("002","wang",14));
foolist.add(new Foo("003","zhang",19));
InputStream in = new FileInputStream("H:\\template.xlsx");
OutputStream out = new FileOutputStream("H:\\test1.xlsx");
Context context = new Context();
context.putVar("foolist", foolist);
JxlsHelper.getInstance().processTemplate(in, out, context);
}
}
2.批注
jx:area(lastCell = "D4")
jx:each(items="foolist" var="foo" lastCell="D4")
jx:if(condition="foo.age<14" lastCell="D4" areas=["A1:D4"])
3.结果
模板:
输出文件: