赞
踩
展示的数据实体类
import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; import java.io.Serializable; import java.util.Date; @Data @TableName("hl_hotlinemessage_hurry_do") public class HotlinemessageHurryDo implements Serializable { @TableId(value = "guid") //excal 表格中不需要展示 @ExcelIgnore private String guid; /** * 工单号 */ @ExcelProperty(value = {"催办信息信息","催办单编号",.....}) private String id; /** * 发送时间(sendTime) */ // converter 转换器,导出的时候将date 类型转换 @ExcelProperty(value = {"催办信息信息","发送时间",.....},converter = LocalDateTimeStringConverter.class) private Date sentTime; }
转换类 导出的时候将date 类型转换
public class LocalDateTimeStringConverter implements Converter<Date> { @Override public Class supportJavaTypeKey() { return LocalDate.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public Date convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception { return DateHelper.getFormatDate(cellData.getStringValue(), "yyyy-MM-dd HH:mm:ss"); } @Override public CellData convertToExcelData(Date date, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception { String format = DateHelper.formatDateTime(date); return new CellData(format); } }
接口
@PostMapping("checkExportExcel") public void checkDownload(@RequestBody JSONObject params, HttpServletResponse response) { SystemUser currentUser = CurrentUser.getCurrentUser(); try { String fileName = DateUtils.getDate("yyyyMMddhhmmss") + "工单列表.xlsx"; response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); //获取导出数据 List<TaskhotlineExcel> taskhotlines = hlTaskhotlineService.getExportExcelList(params , currentUser); taskhotlines.stream().forEach(e->{ if(Objects.equals("1",e.getIsConfidential())){ e.setConsultantName("*******"); e.setContactAddress("*******"); e.setContactInformation("*******"); } }); EasyExcel.write(response.getOutputStream(), TaskhotlineExcel.class).excelType(ExcelTypeEnum.XLSX).sheet("工单列表").doWrite(taskhotlines); } } catch (IOException e) { e.printStackTrace(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。