导出excel,感觉通用,请提建议。谢谢

1. OutputExcelFile.java   工具类只开放outputExcel一个方法。

package com.test.output;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.test.vo.ExcelVo;

/**
 * 导出excel文件,相关配置参考classpath:excel.properties文件
 * @author hejie
 * @since 2010-8-5
 */
public class OutputExcelFile {
	private static Properties pro = new Properties();
	static{
		InputStream is = OutputExcelFile.class.getClassLoader().getResourceAsStream("excel.properties");
		try {
			pro.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 到处excel的主要方法
	 * @param os 文件输出流
	 * @param list<VO> 数据集合
	 * @throws IOException  
	 * @throws RowsExceededException
	 * @throws WriteException
	 * @throws ClassNotFoundException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public static void outputExcel(OutputStream os,List list,String className) throws IOException, RowsExceededException, WriteException, SecurityException, ClassNotFoundException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		WritableWorkbook book = Workbook.createWorkbook(os);  //excel工作薄
		WritableSheet sheet=book.createSheet("第yi页",2);  //excel的一个页,工作表
		//
		toExcel(sheet,list,className);
		
		//写入数据并关闭文件   
		book.write();   
		book.close();   
		os.close();
	}
	/**
	 * 正是向excel中写入数据,主要是遍历集合和写入栏目
	 * @param sheet 页码
	 * @param list  数据集合
	 * @throws RowsExceededException
	 * @throws WriteException
	 * @throws ClassNotFoundException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private static void toExcel(WritableSheet sheet,List list,String className) throws RowsExceededException, WriteException, SecurityException, ClassNotFoundException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		int x = 0;
		int y = 0;
		List fieldlist = getField(className);  //得到要显示的所有属性
		int count = fieldlist.size();
		System.out.println(count);
		for(int i=0;i<count;i++){
			sheet.addCell(isType(pro.get(fieldlist.get(i)), x++, 0));//栏目添加到cell
		}
		for (int i = 0; i < list.size(); i++) {
			Object ob = (Object) list.get(i);
			toSheet(sheet, ob , ++y  , fieldlist);//添加要显示的数据到cell
		}
	}
	/**
	 * 将每一个vo数据写到一行中。
	 * @param sheet  页码
	 * @param ev   数据vo对象
	 * @param y    坐标,此vo写入第几行。
	 * @throws RowsExceededException
	 * @throws WriteException
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	private static void toSheet(WritableSheet sheet,Object ob,int y,List flist) throws RowsExceededException, WriteException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		int x = 0;
		List showData = showDataSpecific(flist,ob);//通过显示的具体值
		int count = showData.size();
		for(int i=0;i<count;i++){
			sheet.addCell(isType(showData.get(i), x++, y));//将值写入到cell
		}
	}
	/**
	 * 将数据填入cell,分类型。缺少很多类型没有判断如date,image
	 * @param ob  要填入cell中的数据
	 * @param x  填入cell的坐标x
	 * @param y  填入cell的坐标y
	 * @return  返回cell
	 */
	private static WritableCell isType(Object ob,int x , int y){ 
		WritableCell cell = null;
		if(ob instanceof  Integer ){
			cell = new Number(x,y,(Integer) ob);
		}else if(ob instanceof Double){
			cell = new Number(x,y,(Double) ob);
		}else{
			cell = new Label(x,y,ob.toString());
		}
		return cell;
	}
	/**
	 * 获得要显示属性
	 * @param className
	 * @return
	 * @throws SecurityException
	 * @throws ClassNotFoundException
	 */
	private static List getField(String className) throws SecurityException, ClassNotFoundException{
		Field[] fs = Class.forName(className).getDeclaredFields();
		List list = new ArrayList();
		for(int i=0;i<fs.length;i++){
			String temp = className+"."+fs[i].getName();
			String value = (String) pro.get(temp);
			if(value!=null&&!"".equals(value.trim()))
				list.add(temp);
		}
		return list;
	}
	/**
	 * 得到一个对象的数据用于显示
	 * @param flist  要显示属性
	 * @param ob  用于显示的单个对象
	 * @return showData 用于显示的一条数据
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	private static List showDataSpecific(List<String> flist , Object ob) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
//		sheet.addCell(isType(ev.getId(), x++, y));
//		Method m = ob.getClass().getMethod("get");
		List showData = new ArrayList();
		for(String s:flist){
			String[] ss = s.split("[.]");
			String field = ss[ss.length-1];
			String fieldU = field.substring(0,1).toUpperCase()+field.substring(1);
			Method m = ob.getClass().getMethod("get"+fieldU);
			Object value = m.invoke(ob);
			showData.add(value);
		}
		return showData;
	}
	//测试
//	public static void main(String[] arg) throws Exception{
//		Date d = new Date();
//		String fname = 1900 +d.getYear()+""+(1+d.getMonth())+""+d.getDate()+""+d.getTime()%1000000;
//		OutputStream os = new FileOutputStream(fname+"rp.xls");
		OutputExcelFile oef = new OutputExcelFile();
//		System.out.println(os);
//		
//		List<ExcelVo> list = new ArrayList<ExcelVo>();
//		ExcelVo ev = new ExcelVo();
//		ev.setAge(18);
//		ev.setId(1);
//		ev.setName("hejie");
//		ev.setPassword("123456he");
//		list.add(ev);
//		
		List<MyExcelVo> list = new ArrayList<MyExcelVo>();
		MyExcelVo mev = new MyExcelVo();
		mev.setId(1);
		mev.setName("hejie");
		list.add(mev);
//		OutputExcelFile.outputExcel(os,list,"com.test.vo.ExcelVo");
//	}
}

2.配置文件

#使用说明:要显示的字段的格式     类路径+要显示的字段=显示的文字
#该类通过此配置文件 配置可以导出多个vo
com.test.vo.ExcelVo.id=卡号
com.test.vo.ExcelVo.name=名字
com.test.vo.ExcelVo.age=年龄
com.test.vo.ExcelVo.password=密码
#
com.test.vo.MyExcelVo.name=名字

  表示我要显示ExcelVo的id,name,age,password。 如果是MyExcelVo对象就显示name属性

 

*基于 jxl-2.4.2.jar 包开发。占时不支持图片,data的类型数据。只有数字和字符。渴望大家修改传给我!

  我的邮箱:hejiejie12@126.com   qq:282123706   谢谢。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值