package com.tch.test.t1;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExportExcel {
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
*
* Created on 2013-4-3
* <p>Discription:导出excel工具方法</p>
* @param file 导出目标文件 source 导出的数据源 columnNames每列的名字
* @param fieldNames 属性名 className 类名
* @author:
* @update:[日期YYYY-MM-DD] [更改人姓名]
* @return void
*/
public static byte[] exportExcel(List<?> source,String[] columnNames,String[] fieldNames){
byte[] b = null;
if(source == null || columnNames == null || fieldNames == null){
return b;
}
if(columnNames.length != fieldNames.length){
return b;
}
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFCell hc = null;
HSSFRow hr = null;
HSSFSheet hs = null;
HSSFCellStyle cellStyle = hwb.createCellStyle();
HSSFFont cellFont = hwb.createFont();
cellStyle.setFont(cellFont);
hs = hwb.createSheet();
hr = hs.createRow((short) 0);
int length = columnNames.length;
/**
* 设置excel的表头
*/
for(int i=0;i<length;i++){
hc = hr.createCell((short)i);
hc.setEncoding(HSSFCell.ENCODING_UTF_16);
hc.setCellValue(columnNames[i]);
}
String[] result = null;
for(int i=0;i<source.size();i++){
result = ObjectToArray(source.get(i),fieldNames);
if(result == null || result.length == 0 || length != result.length){
break;
}
hr = hs.createRow((short) (i+1));
for(int j=0;j<length;j++){
hc = hr.createCell((short)j);
hc.setEncoding(HSSFCell.ENCODING_UTF_16);
hc.setCellValue(result[j]);
}
}
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
hwb.write(outputStream);
b = outputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
/**
*
* Created on 2013-4-3
* <p>Discription:把属性值按照给定的属性的顺序存储到数组里面返回</br>
* 例如:User,给定的属性名为username,password,ipAddress</br>
* 则返回的数组为对应的username,password,ipAddress的值</p>
* @author:
* @update:[日期YYYY-MM-DD] [更改人姓名]
* @return String[]
*/
@SuppressWarnings("unchecked")
public static String[] ObjectToArray(Object model,String[] fieldNames) {
String[] result = new String[fieldNames.length];
try {
Class clazz = model.getClass();
// Field field = null;
Type type = null;
Method method= null;
String property = null;
boolean isComplexType = false;
String complexFieldName = null;
for(int i=0;i<fieldNames.length;i++){
isComplexType = false;
property = fieldNames[i];
if(property.contains(".")){ //包含" . " 说明是复杂类型
int index = property.indexOf(".");
isComplexType = true;
complexFieldName = property.substring(index+1);
property = property.substring(0, index);
}
// field = clazz.getDeclaredField(property);
// System.out.println("attribute name:"+property);
// type = field.getGenericType();
// System.out.println("Generictype name:"+type.toString()+" type: "+field.getType());
method = clazz.getMethod("get"+property.substring(0, 1).toUpperCase()+property.substring(1, property.length()));
if(method != null){
if(isComplexType){
result[i] = ObjectToArray(method.invoke(model), new String[]{complexFieldName})[0];
}else{
if(type != null && "class java.util.Date".equals(type.toString())){
result[i] = String.valueOf(formatDate((Date)method.invoke(model)));
}else{
result[i] = String.valueOf(method.invoke(model));
}System.out.println(result[i]);
}
}
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
private static String formatDate(Date date) {
return format.format(date);
}
}