java生成EXCEL表單簡單demo
//下面是實體類
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