Spring+Struts2+Hibernate+Apache POI 实现的Excel2007导入导出功能代码分享

<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
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值