public <T extends Object> HSSFWorkbook object2Excel(HSSFWorkbook wb, String sheetName,
Collection<T> list, String propertyKeys, String propertyShowKeys, PropertyExtractor exporter)
throws Exception {
HSSFSheet sheet = wb.createSheet(sheetName);
HSSFRow row = null;
HSSFCell cell = null;
Object cellVal = null;
/** **************** 取得传入的list列名称和显示名称 ********** */
String[] pKeys = Tokenizer2StringArray(propertyKeys, ",");
String[] pShowKeys = Tokenizer2StringArray(propertyShowKeys, ",");
/** *************** insert data to excel*************** */
// 创建(标题)
row = sheet.createRow(0); // 建立新行
// 显示标题列名�?
for (int i = 0; i < pShowKeys.length; i++) {
cell = row.createCell(i); // 建立新cell
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(new HSSFRichTextString(pShowKeys[i]));
}
// 逐行取数�?
int rowId = 1;// 数据行号(从�?2行开始填充数�?)
for (Iterator<T> iter = list.iterator(); iter.hasNext(); rowId++) {
row = sheet.createRow(rowId); // 建立新行
T obj = iter.next();
// 生成每一
for (int i = 0; i < pKeys.length; i++) {
cell = row.createCell(i); // 建立新cell
cellVal = exporter.getPropertyValue(obj, pKeys[i]);
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
String cellValue = "";
if (null != cellVal) {
cellValue = cellVal.toString();
}
if (cellVal instanceof Float) {
cellValue = numberformat.format(cellVal);
}
cell.setCellValue(new HSSFRichTextString(cellValue));
}
}
return wb;
}