`
风过无声
  • 浏览: 88257 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JXLS - EXCEL模板

 
阅读更多

1.简介

JXLS是一个开源的EXCEL模板包,特点有:

1)支持XLS和XML配置

2)使用JEXL填充内容

3)解耦底层EXCEL操作库,支持Apache POI和Java EXCEL API

2.实例

1)Employee.java

package com.siyuan.study.jxls.entity;

import java.util.Date;

public class Employee {
	private String name;
	private Date birthday;
	private double payment;
	private double bonus;

	public Employee() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public double getPayment() {
		return payment;
	}

	public void setPayment(double payment) {
		this.payment = payment;
	}

	public double getBonus() {
		return bonus;
	}

	public void setBonus(double bonus) {
		this.bonus = bonus;
	}

}

2)FirstDemo.java

package com.siyuan.study.jxls;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import com.siyuan.study.jxls.entity.Employee;

public class FirstDemo {
	
	public static List<Employee> generateSampleEmployeeData() {
		List<Employee> employees = new LinkedList<Employee>();
		for (int i = 0; i < 10; i++) {
			Employee employee = new Employee();
			employee.setName("siyuan" + i);
			employee.setBirthday(new Date());
			employee.setPayment((int)(Math.random() * 10000));
			employee.setBonus((int)(Math.random() * 1000));
			employees.add(employee);
		}
		return employees;
	}
	
	public static void main(String[] args) throws Exception {
		List<Employee> employees = generateSampleEmployeeData();
		InputStream templateIn = FirstDemo.class.getClassLoader()
				.getResourceAsStream("employeeTemplate.xlsx");
		OutputStream output = new FileOutputStream("F:/files/employee.xlsx");
		Context context = new Context();
		context.putVar("employees", employees);
		JxlsHelper.getInstance().processTemplate(templateIn, output, context);
	}

}

3)employeeTemplate.xlsx

见附件

4)格式化直接通过EXCEL单元格格式设置实现

3.参考资料

JXLS http://jxls.sourceforge.net/

JEXL http://commons.apache.org/proper/commons-jexl/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics