<strong>
</strong>
由于工作需要,负责做了两个Excel的导入导出功能,由于发现之前的代码繁琐效率低下,所以自己根据网上的案例实现了一个比较简单的导入导出方法,分享出来。希望能听取到其他开发人员的意见,继续改进。
一.编写Excel中每行对应的实体类,本文案例实体类如下:
package com.ultrapower.eoms.ultrabpp.runtime.custom.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import com.ultrapower.eoms.ultrabpp.runtime.custom.annotation.excel;
@Entity
@Table(name = "ITOM_RES_AIS")
/**
* 到货入库实体类
* @author liyao
*
*/
public class ItomResAisModel implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6723406945323051088L;
private String keyid;
private String bppid;
private String restype;
private String manufacturers;
private String modell;
private String resnum;
private String reflag;
private String isflag;
private String machine;
private String devservi;
private String devsrvcontract;
private String strstarttime;
private String strendtime;
private String momerysize;
private String netband;
private String hdinfor;
private String itlwh;
private String erowid;
private String supplierid;
public ItomResAisModel() {
super();
// TODO Auto-generated constructor stub
}
@Id
@GeneratedValue(generator="system_uuid")
@GenericGenerator(name="system_uuid",strategy="uuid")
public String getKeyid() {
return keyid;
}
public void setKeyid(String keyid) {
this.keyid = keyid;
}
public String getBppid() {
return bppid;
}
public void setBppid(String bppid) {
this.bppid = bppid;
}
//资源类型
@excel(id="restype",comment="资源类型",validator="notnull",colnum=0)
public String getRestype() {
return restype;
}
public void setRestype(String restype) {
this.restype = restype;
}
//生产厂商
@excel(id="manufacturers",comment="厂商",validator="notnull",colnum=1)
public String getManufacturers() {
return manufacturers;
}
public void setManufacturers(String manufacturers) {
this.manufacturers = manufacturers;
}
//型号
@excel(id="modell",comment="型号",validator="notnull",colnum=2)
public String getModell() {
return modell;
}
public void setModell(String modell) {
this.modell = modell;
}
//资源数量
@excel(id="resnum",comment="资源数量",validator="isnum",colnum=3)
public String getResnum() {
return resnum;
}
public void setResnum(String resnum) {
this.resnum = resnum;
}
public String getReflag() {
return reflag;
}
public void setReflag(String reflag) {
this.reflag = reflag;
}
public String getIsflag() {
return isflag;
}
public void setIsflag(String isflag) {
this.isflag = isflag;
}
public String getMachine() {
return machine;
}
public void setMachine(String machine) {
this.machine = machine;
}
public String getDevservi() {
return devservi;
}
public void setDevservi(String devservi) {
this.devservi = devservi;
}
public String getDevsrvcontract() {
return devsrvcontract;
}
public void setDevsrvcontract(String devsrvcontract) {
this.devsrvcontract = devsrvcontract;
}
public String getStrstarttime() {
return strstarttime;
}
public void setStrstarttime(String strstarttime) {
this.strstarttime = strstarttime;
}
public String getStrendtime() {
return strendtime;
}
public void setStrendtime(String strendtime) {
this.strendtime = strendtime;
}
@excel(id="momerysize",comment="内存大小",validator="",colnum=5)
public String getMomerysize() {
return momerysize;
}
public void setMomerysize(String momerysize) {
this.momerysize = momerysize;
}
@excel(id="netband",comment="网卡带宽",validator="",colnum=6)
public String getNetband() {
return netband;
}
public void setNetband(String netband) {
this.netband = netband;
}
@excel(id="hdinfor",comment="硬盘信息",validator="",colnum=7)
public String getHdinfor() {
return hdinfor;
}
public void setHdinfor(String hdinfor) {
this.hdinfor = hdinfor;
}
@excel(id="itlwh",comment="规格",validator="ismeet",colnum=8)
public String getItlwh() {
return itlwh;
}
public void setItlwh(String itlwh) {
this.itlwh = itlwh;
}
public String getErowid() {
return erowid;
}
public void setErowid(String erowid) {
this.erowid = erowid;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((hdinfor == null) ? 0 : hdinfor.hashCode());
result = prime * result + ((itlwh == null) ? 0 : itlwh.hashCode());
result = prime * result
+ ((manufacturers == null) ? 0 : manufacturers.hashCode());
result = prime * result + ((modell == null) ? 0 : modell.hashCode());
result = prime * result
+ ((momerysize == null) ? 0 : momerysize.hashCode());
result = prime * result + ((netband == null) ? 0 : netband.hashCode());
result = prime * result + ((resnum == null) ? 0 : resnum.hashCode());
result = prime * result + ((restype == null) ? 0 : restype.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ItomResAisModel other = (ItomResAisModel) obj;
if (hdinfor == null) {
if (other.hdinfor != null)
return false;
} else if (!hdinfor.equals(other.hdinfor))
return false;
if (itlwh == null) {
if (other.itlwh != null)
return false;
} else if (!itlwh.equals(other.itlwh))
return false;
if (manufacturers == null) {
if (other.manufacturers != null)
return false;
} else if (!manufacturers.equals(other.manufacturers))
return false;
if (modell == null) {
if (other.modell != null)
return false;
} else if (!modell.equals(other.modell))
return false;
if (momerysize == null) {
if (other.momerysize != null)
return false;
} else if (!momerysize.equals(other.momerysize))
return false;
if (netband == null) {
if (other.netband != null)
return false;
} else if (!netband.equals(other.netband))
return false;
if (resnum == null) {
if (other.resnum != null)
return false;
} else if (!resnum.equals(other.resnum))
return false;
if (restype == null) {
if (other.restype != null)
return false;
} else if (!restype.equals(other.restype))
return false;
return true;
}
@excel(id="supplierid",comment="供货商",validator="",colnum=4)
public String getSupplierid() {
return supplierid;
}
public void setSupplierid(String supplierid) {
this.supplierid = supplierid;
}
@Override
public String toString() {
return bppid + " " + restype + " " + manufacturers + " "
+ modell + " " + resnum + " " + reflag + " " + isflag + " "
+ machine + " " + devservi + " " + devsrvcontract + " "
+ strstarttime + " " + strendtime + " " + momerysize + " "
+ netband + " " + hdinfor + " " + itlwh;
}
}
可以见到在某些字段上我加了一个excel的注解,这个注解是我自定义的,代码如下,目的是维护一些导入时需要的字段信息和验证规则,注解类代码如下:
package com.ultrapower.eoms.ultrabpp.runtime.custom.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.METHOD})
public @interface excel {
public String id();
public String comment();
public String validator();
public int colnum();
}
package com.ultrapower.eoms.ultrabpp.runtime.custom.interfaces;
import java.io.Serializable;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
/**
* 实现基本的动作
*
* @author Administrator
*
* @param <T>
*/
public interface ExcelTemplateInterface<T> {
/**
* 检测excel本身是否有重复数据
*/
public boolean isExcelRepeatObject(T o);
/**
* 检查execl表格内容,给错误信息进行赋值
*
* @param i
* @param object
* @return
*/
public boolean rowValidator(int i, T o);
/**
* 检查是否为空行 public static boolean isEmptyRow(BomcFireWallEntity o) { if
* (o.toString().trim().equals("")) { ret