1.编写一个DateConverter类,指定输出的日期格式:
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.data.WriteCellData;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author
*/
public class DateConverter implements Converter<Date> {
private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
@Override
public Class<Date> supportJavaTypeKey() {
return Date.class;
}
@Override
public WriteCellData<String> convertToExcelData(WriteConverterContext<Date> context) throws Exception {
Date date = context.getValue();
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
return new WriteCellData<>(sdf.format(date));
}
}
2.在导出数据的实体类加上下面的内容,DateConverter为上面创建的类:
@ExcelProperty(value = "入职日期", converter = DateConverter.class)
private Date birthday;
如此便可将Date类型的属性导出为指定日期格式的数据