解决jeeplus注解导出integer变成日期格式,时间格式化失败问题
收藏
最近一个项目用到导出,想到自己写比较麻烦。就使用jeeplus自带的导出,结果发现一个问题,实体注解的地方只要不是string类型的都会出错,比如integer类型导出来变成日期格式,时间sql.date类型转成java.date失败,具体情况如下图。
img[/userfiles/fly/afe99946f1684278b5281ace8a3f763d/files/1605841442476.]
然后看了一下控制台打印日志是格式转化失败
原来的exportExcel
[pre]
/**
* 添加一个单元格
* @param row 添加的行
* @param column 添加列号
* @param val 添加值
* @param align 对齐方式(1:靠左;2:居中;3:靠右)
* @return 单元格对象
*/
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType){
Cell cell = row.createCell(column);
CellStyle style = styles.get("data"+(align>=1&&align<=3?align:""));
try {
if (val == null){
cell.setCellValue("");
} else if (val instanceof String) {
cell.setCellValue((String) val);
} else if (val instanceof Integer) {
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();
CellStyle styleDate = cell.getCellStyle ();
styleDate.cloneStyleFrom (style);
style.setDataFormat(format.getFormat("yyyy-MM-dd"));
cell.setCellValue((Date) styleDate);
} else {
if (fieldType != Class.class){
cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val));
}else{
cell.setCellValue((String)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
"fieldtype."+val.getClass().getSimpleName()+"Type")).getMethod("setValue", Object.class).invoke(null, val));
}
}
} catch (Exception ex) {
log.info("Set cell value ["+row.getRowNum()+","+column+"] error: " + ex.toString());
cell.setCellValue(val.toString());
}
cell.setCellStyle(style);
return cell;
}
[/pre]
看代码,作者好像是已经处理了数据格式,但是我也不知道为什么出不来,就稍微修改了一下
[pre]
/**
* 添加一个单元格
* @param row 添加的行
* @param column 添加列号
* @param val 添加值
* @param align 对齐方式(1:靠左;2:居中;3:靠右)
* @return 单元格对象
*/
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType){
Cell cell = row.createCell(column);
// CellStyle style = styles.get("data"+(align>=1&&align<=3?align:""));
CellStyle style = styles.get("data_column_"+column);
String cellFormatString = "@";
try {
if (val == null){
cell.setCellValue("");
} else if (val instanceof String) {
cell.setCellValue((String) val);
} else if (val instanceof Integer) {
cell.setCellValue((Integer) val);
cellFormatString = "0";
//
// CellStyle styleInteger = cell.getCellStyle();
// styleInteger.setDataFormat(wb.createDataFormat().getFormat("0"));
} else if (val instanceof Long) {
cell.setCellValue((Long) val);
cellFormatString = "0";
} else if (val instanceof Double) {
cell.setCellValue((Double) val);
cellFormatString = "0.00";
} else if (val instanceof Float) {
cell.setCellValue((Float) val);
cellFormatString = "0.00";
} else if (val instanceof Date) {
// DataFormat format = wb.createDataFormat();
// CellStyle styleDate = cell.getCellStyle ();
// styleDate.cloneStyleFrom (style);
// style.setDataFormat(format.getFormat("yyyy-MM-dd"));
// cell.setCellValue((Date) styleDate);
cell.setCellValue((Date) val);
cellFormatString = "yyyy-MM-dd";
} else {
if (fieldType != Class.class){
cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val));
}else{
cell.setCellValue((String)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
"fieldtype."+val.getClass().getSimpleName()+"Type")).getMethod("setValue", Object.class).invoke(null, val));
}
}
//处理数据格式问题
if (val != null){
if (style == null){
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"+(align>=1&&align<=3?align:"")));
style.setDataFormat(wb.createDataFormat().getFormat(cellFormatString));
styles.put("data_column_" + column, style);
}
cell.setCellStyle(style);
}
} catch (Exception ex) {
log.info("Set cell value ["+row.getRowNum()+","+column+"] error: " + ex.toString());
cell.setCellValue(val.toString());
}
cell.setCellStyle(style);
return cell;
}
[/pre]
具体各位可以调试一下