4、进阶之Jxls2的if-else逻辑判断

Jxls2
placeholder image
admin 发布于:2018-04-04 14:49:58
阅读:loading

基本介绍

本章节将在导出逻辑中使用if-else判断逻辑来实现不同报表的需求。在1.0中是只有if标签,是没有else的,有的时候想使用一些复杂的条件判断时存在多个时就写的比较复杂了,实际上有了if-else就也是可以写成if-elseif-else结构逻辑的。本示例主要围绕单个if判断(显示所有数据中name属性中含有字符’j’的所有数据)、3if判断(当属性bonus值的在不同范围内所展示不同的背景色、文本颜色)、if-else逻辑(当名称中含有字符’j’则显示含j,否则显示不含j)涉及到jx:if表达式函数。

注意了如果真有类似3if判断的逻辑时,更好的是使用Excel中自带的条件格式来实现,至于实现方式请参看我在1.0版本时整理的资料。

参考代码

package cn.chendd.examples;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.math.BigDecimal;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.List;

 

import org.jxls.common.Context;

import org.jxls.util.JxlsHelper;

 

import cn.chendd.examples.vo.Employee;

 

/**

 * 简单列表导出--将绩效大于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

 

}

 

代码说明

上述代码中我们可以看出由后台设置了 “employees”的集合列表数据结构。

模板介绍

image.png


image.png


image.png

模板说明

上述3个模板中有3组示例,所有的判断逻辑均是使用String类型的indexOf来的,实际也可以使用于等号判断等其他逻辑上。

运行示例

image.png


image.png


image.png

运行说明

示例中分为3种情况的if判断,其中《if判断》中只显示name属性中包含有字符’j’的数据;《3if判断》中则根据payment属性中的值的大小来显示其对应的样式(背景色、文本颜色);《if-else判断》中则对其全量数据进行循环输出,E列显示模板中指定的数据值。

相关下载

out_simpleIf.xls

本示例代码会在后文中提供。

上述许多内容已经过时和过期了,留存本篇文章仅为方便个人查看,原始文章的信息参考:

原始链接:https://www.chendd.cn/information/viewInformation/other/227.a

最后更新:2018-04-04 14:49:58

访问次数:2250

评论次数:1+

点赞个数:1,[相当给力:2,路过一下:2]

 点赞


 发表评论

当前回复:作者

 评论列表


留言区