JAVA 高级报表导出Excel

有些时候简单的excel数据导出并不能真正满足我们的需求,有的时候我们导出之后的excel需要非常复杂,例如下面我们的这种的:


我们非常难于处理,下面我给大家介绍一种解决方案。

用到的jar包:




代码:

People.java

package entity;

public class People {
	
	private int id;
	private String name;
	private String nickname;
	private int age;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}
Export.java:

package demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
import entity.People;

public class Export {
	
		public void export(){
		
	        List<People> list = getData();
			String file=this.getClass().getClassLoader().getResource("")+"";
			String files=file.substring(6, file.length()-16);
			String templateFileName =files+"template/people.xls";	
			long time = System.currentTimeMillis();
			String destFileName =files+"outfiles/people"+time+".xls";
			Map<String, List<People>> map = new HashMap<String, List<People>>();
			map.put("people", list);
			XLSTransformer transformer = new XLSTransformer();
			try {
				
				transformer.transformXLS(templateFileName, map, destFileName);
				
			} catch (ParsePropertyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	public static List<People> getData(){
		
		List<People> list = new ArrayList<People>();
		
		for (int i = 1;i<=10;i++){
			
			People p = new People();
			p.setId(i);
			p.setName("王晓明"+i);
			p.setNickname("王晓明"+i);
			p.setAge(i+1);
			
			list.add(p);
		}
		return list;
		
	}
	
	
	public static void main(String[] args) {
		
		
		new Export().export();
		
	}

}

运行代码之后:



源码下载链接:http://download.csdn.net/detail/gaoxuaiguoyi/9462598


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值