今天往excel遍历添加内容时发现,好多奇葩格式,很是头疼,有时候我再想要都是string就没这么多问题了,但是事与愿违啊,所以找了个工具类
public void addCell(HSSFRow row, int column, Object val, HSSFWorkbook wb,CellStyle style) {
Cell cell = row.createCell(column);
try {
if (val == null) {
cell.setCellValue("");
} else if (val instanceof String) {
cell.setCellValue((String) val);
} else if (val instanceof Integer) {
if ((Integer) val == 1 ) {
String a = "是";
cell.setCellValue(a);
}else if ((Integer) val == 0) {
String a = "否";
cell.setCellValue(a);
}else {
cell.setCellValue((Integer) val);
}
} else if (val instanceof Long) {
cell.setCellValue((Long) val);
} else if (val instanceof Double) {
cell.setCellValue((Double) val);
} else if (val instanceof Float) {
cell.setCellValue((Float) val);
} else if (val instanceof Date) {
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy/MM/dd"));
cell.setCellValue((Date) val);
} else if (val instanceof BigDecimal) {
double doubleVal = ((BigDecimal) val).doubleValue();
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("¥#,##0.00"));
cell.setCellValue(doubleVal);
}
cell.setCellStyle(style);
} catch (Exception ex) {
log.info("Set cell value [" + row.getRowNum() + "," + column + "] error: " + ex.toString());
cell.setCellValue(val.toString());
}
}
}
基本是常用到的类型,把0转成是,1转成否等,记录下,以后没准还用的着