1. 程式人生 > >poi 通過模板反射通用導出excel

poi 通過模板反射通用導出excel

gets 集合 save red 返回 workbook get sfc lag

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 = getMethod.invoke(t, new Object[] {});
// 判斷值的類型後進行強制類型轉換
String textValue = null;
if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd");
textValue = sdf.format(date);
} else {
// 其它數據類型都當作字符串簡單處理
if (value == null) {
value = "";
}
textValue = 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