簡單的實體類格式(使用模板進行行列的填充)
/***
* @param index
*? ? ? ? ? ? 從第Excel第幾行開始設置值的索引
* @param fieldNames
*? ? ? ? ? ? 實體類屬性數組
* @param dataset
*? ? ? ? ? ? 數據源
* @param sheet
*? ? ? ? ? ? sheet對象
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> void createSheetRowsByList(int index, String[] fieldNames,
Collection dataset, HSSFSheet sheet, HSSFCellStyle boderStyle) {
// 遍歷集合數據,產生數據行
Iterator<T> it = dataset.iterator();
T t;
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher;
String fieldName; // 遍歷實體類字段的名稱
String getMethodName; // 通過反射拼接字段的get方法名
Cell cell; // 要填充的單元格
Class tCls;
Method getMethod;
Object value;
String textValue;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
while (it.hasNext()) {
index++;
Row row = sheet.createRow(index);
t = (T) it.next();
// 利用反射,根據JavaBean屬性的先后順序,動態調用getXxx()方法得到屬性值
for (int i = 0; i < fieldNames.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(boderStyle);
fieldName = fieldNames[i];
getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
tCls = t.getClass();
getMethod = tCls.getMethod(getMethodName, new Class[] {});
value = getMethod.invoke(t, new Object[] {});
// 判斷值的類型后進行強制類型轉換
textValue = null;
if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Float) {
textValue = String.valueOf((Float) value);
cell.setCellValue(textValue);
} else if (value instanceof Double) {
textValue = String.valueOf((Double) value);
cell.setCellValue(textValue);
} else if (value instanceof Long) {
cell.setCellValue((Long) value);
}
if (value instanceof Boolean) {
textValue = "是";
if (!(Boolean) value) {
textValue = "否";
}
} else if (value instanceof Date) {
textValue = sdf.format((Date) value);
} else {
// 其它數據類型都當作字符串簡單處理
if (value != null) {
textValue = value.toString();
}
}
if (textValue != null) {
matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是數字當作double處理
cell.setCellValue(Double.parseDouble(textValue));
} else {
cell.setCellValue(textValue);
}
}
} // end for
}// end while
} catch (Exception e) {
e.printStackTrace();
} finally {
// 清理資源
}
}
簡單的鍵值對格式(使用模板進行行列的填充)
private<T> void createSheetRowsByMap( int index ,String[] fieldNames, Collection dataset , XSSFSheet sheet){
? ? // 遍歷集合數據,產生數據行?
Iterator<HashMap<String,? Object>> it = dataset.iterator();?
? ? RichTextString richString;?
? ? Pattern p = Pattern.compile("^//d+(//.//d+)?$");?
? ? Matcher matcher;?
? ? String fieldName;?
? ? Cell cell;?
? ? Object value;?
? ? String textValue;?
? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");?
? ? while (it.hasNext()) {?
? ? ? ? index++;?
? ? ? ? HashMap<String,? Object> map = it.next();?
? ? ? ? Row row = sheet.createRow(index);?
? ? ? ? for (int i = 0; i < fieldNames.length; i++) {?
? ? ? ? ? ? cell = row.createCell(i);?
? ? ? ? ? ? fieldName = fieldNames[i];?
? ? ? ? ? ? try {?
? ? ? ? ? ? ? ? value = map.get(fieldName);
? ? ? ? ? ? ? ? // 判斷值的類型后進行強制類型轉換?
? ? ? ? ? ? ? ? textValue = null;?
? ? ? ? ? ? ? ? if (value instanceof Integer) {?
? ? ? ? ? ? ? ? ? ? cell.setCellValue((Integer) value);
? ? ? ? ? ? ? ? } else if (value instanceof Float) {?
? ? ? ? ? ? ? ? ? ? textValue = String.valueOf((Float) value);?
? ? ? ? ? ? ? ? ? ? cell.setCellValue(textValue);?
? ? ? ? ? ? ? ? } else if (value instanceof Double) {?
? ? ? ? ? ? ? ? ? ? textValue = String.valueOf((Double) value);
? ? ? ? ? ? ? ? ? ? cell.setCellValue(textValue);?
? ? ? ? ? ? ? ? } else if (value instanceof Long) {?
? ? ? ? ? ? ? ? ? ? cell.setCellValue((Long) value);?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? if (value instanceof Boolean) {?
? ? ? ? ? ? ? ? ? ? textValue = "是";?
? ? ? ? ? ? ? ? ? ? if (!(Boolean) value) {?
? ? ? ? ? ? ? ? ? ? ? ? textValue = "否";?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? } else if (value instanceof Date) {?
? ? ? ? ? ? ? ? ? ? textValue = sdf.format((Date) value);?
? ? ? ? ? ? ? ? } else {?
? ? ? ? ? ? ? ? ? ? // 其它數據類型都當作字符串簡單處理?
? ? ? ? ? ? ? ? ? ? if (value != null) {?
? ? ? ? ? ? ? ? ? ? ? ? textValue = value.toString();?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? if (textValue != null) {?
? ? ? ? ? ? ? ? ? ? matcher = p.matcher(textValue);?
? ? ? ? ? ? ? ? ? ? if (matcher.matches()) {?
? ? ? ? ? ? ? ? ? ? ? ? // 是數字當作double處理?
? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(Double.parseDouble(textValue));?
? ? ? ? ? ? ? ? ? ? } else {?
? ? ? ? ? ? ? ? ? ? richString = new XSSFRichTextString(textValue);?
? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(richString);?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? } catch (SecurityException e) {?
? ? ? ? ? ? ? ? e.printStackTrace();?
? ? ? ? ? ? } catch (IllegalArgumentException e) {?
? ? ? ? ? ? ? ? e.printStackTrace();?
? ? ? ? ? ? }? finally {?
? ? ? ? ? ? ? ? // 清理資源?
? ? ? ? ? ? }
? ? ? ? }?
? ? }?
}
每個單元格帶樣式的
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
? ? private static <T> void setSheetRows(String version,String[] fieldNames,int index, Collection dataset ,Sheet sheet,CellStyle style){
? ? // 遍歷集合數據,產生數據行?
? ? Iterator<HashMap<String,? Object>> it = dataset.iterator();?
? ? RichTextString richString;?
? ? Pattern p = Pattern.compile("^//d+(//.//d+)?$");?
? ? Matcher matcher;?
? ? String fieldName;?
? ? Cell cell;?
? ? Object value;?
? ? String textValue;?
? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");?
? ? DecimalFormat df = new DecimalFormat("##.##");
? ? while (it.hasNext()) {?
? ? index++;?
? ? Row row = sheet.createRow(index);?
? ? HashMap<String,? Object> map = it.next();?
? ? for (int i = 0; i < fieldNames.length; i++) {?
? ? cell = row.createCell(i);?
? ? cell.setCellStyle(style);
? ? //field = fields[i];?
? ? fieldName = fieldNames[i];?
? ? try {?
? ? // 判斷值的類型后進行強制類型轉換?
? ? value = map.get(fieldName);
? ? textValue = null;?
? ? if (value instanceof Integer) {?
? ? cell.setCellValue((Integer) value);
? ? } else if (value instanceof Float) {?
? ? cell.setCellValue(df.format((Float) value));?
? ? } else if (value instanceof Double) {?
? ? cell.setCellValue(df.format((Double) value));?
? ? } else if (value instanceof Long) {?
? ? cell.setCellValue((Long) value);?
? ? }else if (value instanceof BigDecimal) {?
? ? cell.setCellValue(((BigDecimal) value).doubleValue());?
? ? } else if (value instanceof Boolean) {?
? ? textValue = "是";?
? ? if (!(Boolean) value) {?
? ? textValue = "否";?
? ? }?
? ? } else if (value instanceof Date) {?
? ? textValue = sdf.format((Date) value);?
? ? } else {?
? ? // 其它數據類型都當作字符串簡單處理?
? ? if (value != null) {?
? ? textValue = value.toString();?
? ? }?
? ? }?
? ? if (textValue != null) {?
? ? matcher = p.matcher(textValue);?
? ? if (matcher.matches()) {?
? ? // 是數字當作double處理?
? ? cell.setCellValue(Double.parseDouble(textValue));?
? ? } else {?
? ? if(StringUtils.isBlank(version) || EXCEl_FILE_2007.equals(version.trim())){?
? ? richString = new XSSFRichTextString(textValue);
? ? }else{
? ? richString = new HSSFRichTextString(textValue);
? ? }
? ? cell.setCellValue(richString);?
? ? }?
? ? }?
? ? } catch (SecurityException e) {?
? ? e.printStackTrace();?
? ? }? catch (IllegalArgumentException e) {?
? ? e.printStackTrace();?
? ? } finally {?
? ? // 清理資源?
? ? }
? ? //sheet.autoSizeColumn((short)i);
? ? }?
? ? }?
? ? }