vue easyexcel 导出excel post请求接参数
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.5</version> </dependency>
controller
@PostMapping("/list/export") public void exportProductionStopList( @RequestBody(required = false) ProductionStopInfoDTO productionStopInfoDTO, HttpServletResponse response) throws Exception { try { productionStopService.exportProductionStopList(productionStopInfoDTO, response); }catch (Exception e){ logger.error("导出失败", e); // 重置response response.reset(); response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); Map<String, String> map = MapUtils.newHashMap(); map.put("status", "failure"); map.put("message", "下载文件失败" + e.getMessage()); response.getWriter().println(JSON.toJSONString(map)); } }
service
@Override public void exportProductionStopList(ProductionStopInfoDTO productionStopInfoDTO, HttpServletResponse response) throws Exception {
//查询条件 QueryWrapper<Object> wrapper = getObjectQueryWrapper(productionStopInfoDTO); ArrayList<ProductionStopListVO> pageList = productionStopInfoMapper.queryProductionStopList(wrapper); //+ DateUtil.getCurrentDateStr("yyyyMMddHHmmss") setExcelRespProp(response, "某个列表"); pageList.stream().map(v -> { ProductionStopListVO productionStopListVO = (ProductionStopListVO) v; productionStopListVO.setStatusName(ProductionStopStatusEnum.getProductionStopStatus(productionStopListVO.getStatus())); return productionStopListVO; }).collect(Collectors.toList()); EasyExcel.write(response.getOutputStream(),ProductionStopListVO.class) .sheet("sheet1") .doWrite(pageList); } /** * 设置excel下载响应头属性 */ private void setExcelRespProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); }
model
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.qm.be.util.excel.LocalDateStringConverter;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
@Data @HeadRowHeight(20) public class ProductionStopListVO { /** * id */ @ExcelIgnore private String id; /** * 生产关闭编号 */ @ExcelProperty("生产关闭编号") @ColumnWidth(25) private String productionStopNo; /** * 产品类别 */ @ExcelProperty("产品类别") private String productCategory; /** * 产品家族 */ @ExcelProperty("产品家族") @ColumnWidth(15) private String productionFamily; /** * 负责人 */ @ExcelProperty("负责人") @ColumnWidth(15) private String ownerName; /** * 申请日期 */ @ExcelProperty(value ="申请日期" ,converter = LocalDateStringConverter.class) @DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern="yyyy-MM-dd") @ColumnWidth(15) private LocalDate createTime; /** * 状态名称 */ @ExcelProperty("状态") private String statusName; /** * 状态 */ @ExcelIgnore private Integer status; /** * 故障描述 */ @ExcelProperty("故障描述") @ColumnWidth(25) private String failureDescription; }
自定义时间转换
package com.qm.be.util.excel; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class LocalDateStringConverter implements Converter<LocalDate> { @Override public Class<?> supportJavaTypeKey() { return LocalDate.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { return LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd")); } @Override public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String format = formatter.format(value); return new WriteCellData(format); } }
package com.qm.be.util.excel; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeStringConverter implements Converter<LocalDateTime> { @Override public Class<?> supportJavaTypeKey() { return LocalDateTime.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); } @Override public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String format = formatter.format(value); return new WriteCellData(format); } }
vue
const requestExportList = (url, data) => { return new Promise((resolve, reject) => { axios.post(url, data, { responseType: "blob" }).then( res => { // debugger; const link = document.createElement("a"); const blob = new Blob([res.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }); link.style.display = "none"; link.href = URL.createObjectURL(blob); link.download = "某个列表.xlsx"; document.body.appendChild(link); link.click(); document.body.removeChild(link); resolve(); }, err => { reject(err); } ); }); }; const exportList = async () => { let createTimeStart = ""; let createTimeEnd = ""; // debugger; if (pageParams.timeline == null) { pageParams.timeline = []; } if (pageParams.timeline.length > 0) { createTimeStart = pageParams.timeline[0]; createTimeEnd = pageParams.timeline[1]; } // let res = await exportProductionStopList({}); // debugger; let rLoading = openLoading(); await requestExportList("/接口/list/export", { createTimeStart: createTimeStart, createTimeEnd: createTimeEnd, status: pageParams.status != "" ? parseInt(pageParams.status) : "", productFamily: pageParams.productFamily }) .then(res => {}) .finally(() => { rLoading.close(); }); // debugger; };