首页 > 代码库 > poi 通过模板反射通用导出excel
poi 通过模板反射通用导出excel
package util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ExcelUtil{
/**
* 使用poi通过反射导出Excel
*
* @param data 需要导出表格的数据
* @param saveFilePath 导出文件所在路径
* @param filePath 模板表格路径
* @param dataRowBeginIndex 数据开始行
*
* @return 成功返回true 失败返回false
* @throws Exception
*/
public static boolean translateSheet(
List data,String filePath, String saveFilePath,int dataRowBeginIndex) throws Exception {
FileInputStream fis = new FileInputStream(filePath);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row =sheet.getRow(0);
FileOutputStream out = new FileOutputStream(saveFilePath); // 向d://test.xls中写数据
// 遍历集合数据,产生数据行
Iterator it = data.iterator();
boolean flag = true;
try {
while (it.hasNext()) {
row = sheet.createRow(dataRowBeginIndex++);//若不是在已有Excel表格后面追加数据 则使用该条语句
// 创建单元格,并设置值
T t = (T) it.next();
// 利用反射,根据类属性的先后顺序
Field[] fields = t.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (field.toString().contains("static")) {
continue;
}
HSSFCell cell = row.createCell(i);
String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName,new Class[] {});
Object value = http://www.mamicode.com/getMethod.invoke(t, new Object[] {});
// 判断值的类型后进行强制类型转换
String textValue = http://www.mamicode.com/null;
if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd");
textValue = http://www.mamicode.com/sdf.format(date);
} else {
// 其它数据类型都当作字符串简单处理
if (value =http://www.mamicode.com/= null) {
valuehttp://www.mamicode.com/= "";
}
textValue = http://www.mamicode.com/value.toString();
}
if (textValue != null) {
Pattern p = Pattern.compile("^//d+(//.//d+)?{1}quot;");
Matcher matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
} else {
cell.setCellValue(textValue);
}
}
}
}
} catch (Exception e) {
flag = false;
e.printStackTrace();
} finally {
out.flush();
wb.write(out);
out.close();
}
System.out.println("导出完毕");
return flag;
}
public static void main(String[] args) {
List list = new ArrayList();
// Book book =new Book();
// book.setName("asd");
// book.setId(123);
User stu = new User("jack","20");
User stu1 = new User("jack","asd");
System.out.println(stu);
list.add(stu);
list.add(stu1);
try {
translateSheet(list,"D:/temp.xls","d:/test.xls",1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
poi 通过模板反射通用导出excel