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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值