1. 程式人生 > >JXLS2.4導出Excel

JXLS2.4導出Excel

examples dep ont 屬性 stream tro item string 利用

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

JXLS2.4導出Excel