有些时候简单的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