Excel 導出數(shù)據(jù)

簡單的實體類格式(使用模板進行行列的填充)

/***

* @param index

*? ? ? ? ? ? 從第Excel第幾行開始設(shè)置值的索引

* @param fieldNames

*? ? ? ? ? ? 實體類屬性數(shù)組

* @param dataset

*? ? ? ? ? ? 數(shù)據(jù)源

* @param sheet

*? ? ? ? ? ? sheet對象

*/

@SuppressWarnings({ "unchecked", "rawtypes" })

public <T> void createSheetRowsByList(int index, String[] fieldNames,

Collection dataset, HSSFSheet sheet, HSSFCellStyle boderStyle) {

// 遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行

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();

// 利用反射,根據(jù)JavaBean屬性的先后順序,動態(tài)調(diào)用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[] {});

// 判斷值的類型后進行強制類型轉(zhuǎn)換

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 {

// 其它數(shù)據(jù)類型都當作字符串簡單處理

if (value != null) {

textValue = value.toString();

}

}

if (textValue != null) {

matcher = p.matcher(textValue);

if (matcher.matches()) {

// 是數(shù)字當作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){

? ? // 遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行?

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);

? ? ? ? ? ? ? ? // 判斷值的類型后進行強制類型轉(zhuǎn)換?

? ? ? ? ? ? ? ? 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 {?

? ? ? ? ? ? ? ? ? ? // 其它數(shù)據(jù)類型都當作字符串簡單處理?

? ? ? ? ? ? ? ? ? ? if (value != null) {?

? ? ? ? ? ? ? ? ? ? ? ? textValue = value.toString();?

? ? ? ? ? ? ? ? ? ? }?

? ? ? ? ? ? ? ? }?

? ? ? ? ? ? ? ? if (textValue != null) {?

? ? ? ? ? ? ? ? ? ? matcher = p.matcher(textValue);?

? ? ? ? ? ? ? ? ? ? if (matcher.matches()) {?

? ? ? ? ? ? ? ? ? ? ? ? // 是數(shù)字當作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){

? ? // 遍歷集合數(shù)據(jù),產(chǎn)生數(shù)據(jù)行?

? ? 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 {?

? ? // 判斷值的類型后進行強制類型轉(zhuǎn)換?

? ? 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 {?

? ? // 其它數(shù)據(jù)類型都當作字符串簡單處理?

? ? if (value != null) {?

? ? textValue = value.toString();?

? ? }?

? ? }?

? ? if (textValue != null) {?

? ? matcher = p.matcher(textValue);?

? ? if (matcher.matches()) {?

? ? // 是數(shù)字當作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);

? ? }?

? ? }?

? ? }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容