POI 注解方式 导入导出 excel , 只依赖于 POI包

package cn.com.guangduo.zjyxcg.common.util;

import java.util.Date;

import cn.com.guangduo.zjyxcg.common.util.excel.annotation.AnnExcelColumn;
import cn.com.guangduo.zjyxcg.common.util.excel.annotation.AnnExcelSheet;
@AnnExcelSheet(name="生产企业信息")
public class ExcelImportAndExportBean {
	@AnnExcelColumn(title="企业名称",width=-1)
	private String name;
	@AnnExcelColumn(title="注册资金(万元)",format="0.00",order=2)
	private Double capital;
	@AnnExcelColumn(title="传真",order=4)
	private String fax;
	@AnnExcelColumn(title="起始日期",format="yyyy年MM月dd日",width=200)
	private Date startDate;
	@AnnExcelColumn(title="年检年度",format="#年")
	private Integer year;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getCapital() {
		return capital;
	}
	public void setCapital(Double capital) {
		this.capital = capital;
	}
	public String getFax() {
		return fax;
	}
	public void setFax(String fax) {
		this.fax = fax;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Integer getYear() {
		return year;
	}
	public void setYear(Integer year) {
		this.year = year;
	}
	@Override
	public String toString() {
		return "ExcelImportAndExportBean [name=" + name + ", capital="
				+ capital + ", fax=" + fax + ", startDate=" + startDate
				+ ", year=" + year + "]";
	}
}


package cn.com.guangduo.zjyxcg.common.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import cn.com.guangduo.zjyxcg.common.util.excel.ExcelUtil;
import cn.com.guangduo.zjyxcg.common.util.excel.bean.IExcelExport;
import cn.com.guangduo.zjyxcg.common.util.excel.bean.IExcelImport;

public class ExcelImportAndExportTest {
	//么有junit
	void testImport(){
		IExcelImport imp = ExcelUtil.getImport();
	
	}
	public static void main(String[] args) throws Exception {
		//获取导入实例
		IExcelImport imp = ExcelUtil.getImport();
		//传入输入流 以及 注解了的bean对象 返回 对象数组
		List<ExcelImportAndExportBean> beans = imp.execute(new FileInputStream("E:/生产企业.xlsx"), ExcelImportAndExportBean.class);
		System.out.println("==========================================");
		for (ExcelImportAndExportBean bean : beans) {
			System.out.println(bean);
		}
		System.out.println("==========================================");
		//获取导出实例
		IExcelExport exp = ExcelUtil.getExport();
		//传入输入流, 数据, 以及 注解的 bean 对象
		//扩展名是xls,为了兼容,输出 1997~2013 格式的文档
		//起始可以根据 beans 的 类型确认 第三个参数的class,但是没找到获取的方法
		OutputStream os = new FileOutputStream("E:/导出的数据.xls");
		exp.execute(os, beans, ExcelImportAndExportBean.class);
		exp.setTitleStyel(null);//设置标题样式
		System.out.println("==========================================");
	}
}

package cn.com.guangduo.zjyxcg.common.util.excel.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import cn.com.guangduo.zjyxcg.common.enums.ICommonEnum;
import cn.com.guangduo.zjyxcg.common.util.excel.enums.ExcelColumnType;

/**
 * 导入导出字段的注解 
 * 
 * @author zhang.xiaolei
 * @createDate 2015年5月27日
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnExcelColumn {
	/**
	 * Excel 标题名称
	 */
	String title(); 
	/**
	 * 输出的的格式
	 */
	String format() default ""; 
	/**
	 * 在什么时间处理,导入, 导出, 全部 
	 */
	ExcelColumnType type() default ExcelColumnType.ALL; 
	/**
	 * 导出时标题的顺序 由小到大
	 */
	int order() default 0;
	/**
	 * 导入时候的简单验证 
	 * FIXME 以后再处理
	 */
	String validate() default "";
	/**
	 * 枚举类型的相互转换,必须是 ICommonEnum 的实现
	 *  FIXME 暂未实现
	 */
	Class<ICommonEnum> enumType() default ICommonEnum.class;
	/**
	 * 列宽,<br/>0 默认值,为不处理,
	 *           <br/>-1 自动宽度,
	 *          <br/> 其他值 就会设置为宽度
	 *  
	 * @default 0
	 */
	int width() default 0;
}

哈哈, 直接上代码,实在人,具体内容见附件。

功能还没有完胜,用起来基本没有问题,可能有些类型转换问题, 遇到的加上就好了!

String,Double,Date , Integer  这几种类型毫无问题!



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值