//下面是实体类
package com.test.model;
import java.util.Date;
//模板数据实体
public class UserTmpl {
private int id;
private String name;
private Date birthday;
private String identity;
private String phone;
private String address;
private String Email;
public UserTmpl() {
super();
}
public UserTmpl(int id, String name, Date birthday, String identity, String phone, String address, String email) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.identity = identity;
this.phone = phone;
this.address = address;
Email = email;
}
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getIdentity() {
return identity;
}
public void setIdentity(String identity) {
this.identity = identity;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
}
//下面是导出execl的类
package com.test.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.log4j.Logger;
import net.sf.excelutils.ExcelException;
import net.sf.excelutils.ExcelUtils;
/**
* @Description: 导出Excel的工具类
* @Author : chenhao
* @Date : 2015-8-3 下午2:47:28
*/
public class ExportUtil {
static Logger log = Logger.getLogger(ExportUtil.class.getName());
/**
* @Description:导出Excel方法
* @Auther : 陈豪
* @Date: 2015-8-03 下午3:16:54
* @param tmplPath 模板读取路径
* @param tmplName 模板名称(不包含后缀)
* @param objFilePath 目标文件生成路径
* @param objFileName 目标文件名(不包含后缀)
* @param resourceList 待生成数据集合(List数据源)
* @return 0导出成功|1某参数为空|2系统异常|-1模板文件不存在
*/
public int ExportExcel(String tmplPath,String tmplName,String objFilePath,String objFileName,List resourceList){
//参数非空
if(tmplPath.equals(null)||tmplName.equals(null)||objFilePath.equals(null)||objFileName.equals(null)||resourceList.equals("null")||resourceList.size()<1||resourceList==null){
System.out.println("传入参数有误或有空值,中断执行");
return 1;
}
//模板路径及名称
tmplPath=tmplPath + "/" + tmplName + ".xls";
log.info("模板文件存放目录:"+tmplPath);
//目标路径及名称
String mbFileName = objFilePath + "/" + objFileName+ ".xls";
log.info("目标文件生成目录:"+tmplPath);
//添加要生成的值
ExcelUtils.addValue("reportList", resourceList);
try {
//检测模板文件是否存在
File lcfile = new File(tmplPath);
if(!lcfile.exists()){
log.error("模板文件不存在,请确认其位置");
return -1;
}
//检测目标路径是否存在,不存在则创建
File file =new File(objFilePath);
if(!file.exists() && !file.isDirectory()){
file .mkdir();
}
//导出Excel
ExcelUtils.export(tmplPath, new FileOutputStream(mbFileName));
}catch (FileNotFoundException e) {
log.error("导出异常:",e.fillInStackTrace());
return 2;
}catch (ExcelException e) {
log.error("导出异常:",e.fillInStackTrace());
return 2;
}
log.info("导出成功,文件生成目录:"+mbFileName);
return 0;
}
}
//截下来是excel模板
编号 姓名 出生日期 身份证号 联系电话 现住址 联系邮箱
#foreach user in ${reportList}
${user.id} ${user.name} ${user.birthday} ${user.identity} ${user.phone} ${user.address} ${user.email}
#end
//依赖包
commons-beanutils.jar
commons-logging.jar
excelutils-1.41.jar
log4j-1.2.13.jar
poi-2.5.1.jar
来源:oschina
链接:https://my.oschina.net/u/2659774/blog/630525