实体bean导出为excel
package com.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
/**
* 实体bean导出为excel
*
* Created by zyb on 16/8/5.
*/
public class BeanExportExcel<T> {
@SuppressWarnings({ "VariableArgumentMethod", "Annotation" })
public static void main(String... args) {
/* Expert expert = new Expert();
String[] headers = {"ID", "姓名", "身份证号", "性别", "生日", "手机", "电话", "省", "市", "地址", "邮编", "单位", "职务", "邮箱", "特殊人才类别", "专家类别", "职称", "行业", "专业", "个人简介", "图片", "添加时间", "更新时间", "排序", "状态"};
List<Expert> list = new ArrayList<Expert>();
list.add(new Expert("张三", "123441221", "男", "2312321", "231321", "312312", "21312321", "21312312", "eqweqweqw", "dweqqweqweq", "张三", "123441221", "男", "2312321", "231321", "312312", "21312321", "21312312", "eqweqweqw", "dweqqweqweq", "dsadas", "dasas", 1231, "dasdas"));
list.add(new Expert("张三", "123441221", "男", "2312321", "231321", "312312", "21312321", "21312312", "eqweqweqw", "dweqqweqweq", "张三", "123441221", "男", "2312321", "231321", "312312", "21312321", "21312312", "eqweqweqw", "dweqqweqweq", "dsadas", "dasas", 1232, "dasdas"));
BeanExportExcel<Expert> expertExportExcel = new BeanExportExcel<Expert>();
try {
OutputStream outputStream = new FileOutputStream("/Users/ZYB/Desktop/a.xls");
expertExportExcel.exportExcel("测试", headers, list, outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
}
/**
* @param sheetName 表格sheetName
* @param headers 表格属性列名数组
* @param dataset 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。此方法支持的
* javabean属性的数据类型有基本数据类型及String,Date,byte[](图片数据)
* @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中,后缀只能为xls
* 例如:/Users/ZYB/Desktop/a.xls
*/
public void exportExcel(String sheetName, String[] headers, Collection<T> dataset, OutputStream out) {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(sheetName);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
//style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //背景
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//居中
// 生成一个字体
HSSFFont font = workbook.createFont();
//font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 13);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
//style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
//style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
// 声明一个画图的顶级管理器
// HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
// HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
// 设置注释内容
// comment.setString(new HSSFRichTextString("这是张勇波做的导出功能!"));
// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
// comment.setAuthor("张勇波");
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行
Iterator<T> it = dataset.iterator();
int index = 0;
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
T t = it.next();
// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
Field[] fields = t.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style2);
Field field = fields[i];
String fieldName = field.getName();
String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Class<?> tCls = t.getClass();
Method getMethod = null;
try {
getMethod = tCls.getMethod(getMethodName);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Object value = null;
try {
if (getMethod != null) {
value = getMethod.invoke(t);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
String val = "";
if (value != null && !"null".equals(value) && !"".equals(value)) {
val = value.toString().trim();
}
if (0 < val.length() && val.length() < 9 && StringUtils.isNumeric(val)) {
// 是数字当作long处理
cell.setCellValue(Long.parseLong(val));
} else {
HSSFRichTextString richString = new HSSFRichTextString(val);
// HSSFFont font3 = workbook.createFont();
//font3.setColor(HSSFColor.BLUE.index);
// richString.applyFont(font3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(richString);
}
}
}
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}