JXLS2.4导出Excel

1.添加依赖:http://mvnrepository.com/artifact/org.jxls
<dependency>  
  <groupId>org.jxls</groupId>  
  <artifactId>jxls</artifactId>  
  <version>2.4.3</version>  
</dependency>
2.创建Excel模板
Jxls默认支持Apache JEXL表达式语言,用于在模板中操作Java对象的属性及方法,类似于EL表达式。
如图:
注:
Jxls利用Excel的 批注声明各种命令,
jx:each()  这是一个遍历注释,默认向下增加一行。官网写可以横向遍历(增加direction 参数)
上图中:A1单元格的批注: jx:area(lastCell="D4"),定义模板区域是A1:D4
A4单元格的批注: jx:each(items="employees" var="employee" lastCell="D4"),定义Each循环命令 ,lastCell="D4 表示Each命令的操作区域是A4:D4
3.java代码
/**
 * 简单列表导出--将绩效大于2000的显示绿色,大于1000小于2000的显示黄色,小于1000的显示红色
 * @author chendd
 */
public class SimpleIfJxls {
 
   public static void main(String[] args) throws Exception {
      //构造集合数据
      List<Employee> employees = new ArrayList<Employee>();
      Calendar time = Calendar.getInstance();
      time.set(2015, 5, 20);
      Date date = time.getTime();
      employees.add(new Employee("lishengle" , date , new BigDecimal(5000) , new BigDecimal(800)));
      employees.add(new Employee("jiajitao" , date , new BigDecimal(6000) , new BigDecimal(1200)));
      employees.add(new Employee("jiangjunjian" , date , new BigDecimal(8500) , new BigDecimal(2500)));
      employees.add(new Employee("sunming" , date , new BigDecimal(17000) , new BigDecimal(1500)));
      employees.add(new Employee("chendd" , date , new BigDecimal(17000) , new BigDecimal(1500)));
      employees.add(new Employee("zenxianrong" , date , new BigDecimal(7500) , new BigDecimal(3500)));
      //载入模板
      InputStream is = SimpleIfJxls.class.getClass().getResourceAsStream("/cn/chendd/examples/templates/simpleIf.xls");
      Context context = new Context();
      context.putVar("employees", employees);
      context.putVar("title", "员工信息列表");
      OutputStream os = new FileOutputStream(new File("d:\\test\\out_simpleIf.xls"));
      //指定Sheet文件解析
      JxlsHelper.getInstance().processTemplate(is, os, context);
      os.flush();
      os.close();
      is.close();
   }
 
}
 
public class Employee {
 
   private String name;
   private Date birthDate;
   private BigDecimal payment;
   private BigDecimal bonus;
  
   public Employee() {
      super();
   }
 
   public Employee(String name, Date birthDate, BigDecimal payment,
         BigDecimal bonus) {
      super();
      this.name = name;
      this.birthDate = birthDate;
      this.payment = payment;
      this.bonus = bonus;
   }
 
   //省略getter/setter
 
}

更多参考:https://www.chendd.cn/information/viewInformation/other/224.a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值