后台查出来数据直接生成Excel文件然后输出
用到的依赖
commons-fileupload
commons-fileupload
1.3.1
com.alibaba
easyexcel
2.2.6
javax.servlet
servlet-api
org.apache.poi
poi
org.apache.poi
poi-ooxml
org.apache.poi
poi-ooxml-schemas
实体类
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = “房屋信息导出”)
public class ExportFwxxglVo implements Serializable {
private static final long serialVersionUID = 1L; @ExcelProperty(index = 0, value = "房屋管理类型") private Integer fwgllx; @ExcelProperty(index = 1, value = "房屋类别") private Integer fwlb; @ExcelProperty(index = 2, value = "土地性质") private Integer tdxz; @ExcelProperty(index = 3, value = "安全状态") private Integer aqzt; @ExcelProperty(index = 4, value = "房屋状态") private Integer fwzt; @ExcelProperty(index = 5, value = "房屋来源") private Integer fwly; @ExcelProperty(index = 6, value = "建筑面积") private BigDecimal jzmj; @ExcelProperty(index = 7, value = "房屋朝向") private String fwcx; @ExcelProperty(index = 8, value = "管理单位") private String gldw; @ExcelProperty(index = 9, value = "房屋地址") private String fwdz; @ExcelProperty(index = 10, value = "使用人") private String syr; @ExcelProperty(index = 11, value = "划拨的更新项目名称") private String hbdgxxmmc; @ExcelProperty(index = 12, value = "编码") private String rid;
}
**控制层** @PostMapping("/exportFwxxgl") @ApiOperation(value = "房屋信息导出", notes = "房屋信息导出") public void exportFwxxgl(@RequestBody DzglRids rids, HttpServletResponse response) throws IOException { ExcelExportUtil.setResponse(response); String filename = ExcelExportUtil.getExcelName("房屋信息管理列表数据", ".xlsx"); response.addHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename); fwxxglService.exportFwxxgl(rids, response.getOutputStream()); } **业务层** @Override public void exportFwxxgl(DzglRids rids, OutputStream outputStream) { if (rids == null) { throw new ApplicationException(CommonErrorCode.ERROR_ARGUMENT_NUM, "导出失败"); } if (CollectionUtils.isEmpty(rids.getListRid())) { throw new ApplicationException(CommonErrorCode.ERROR_ARGUMENT_NUM, "导出失败"); } QueryWrapperquery = new QueryWrapper<>(); query.in("rid", rids.getListRid()); query.select("rid", "zt", "fwgllx", "fwlb", "tdxz", "aqzt", "fwzt", "fwly", "jzmj", "fwcx", "gldw", "fwdz", "syr", "hbdgxxmmc"); List fwxxglList = iFwxxglMapper.selectList(query); ArrayList ar = new ArrayList<>(); for (Fwxxgl fwxxgl : fwxxglList) { if (fwxxgl.getZt() == null) { throw new ApplicationException(CommonErrorCode.ERROR_ARGUMENT_NUM, "导出失败"); } if (fwxxgl.getZt() != 2) { throw new ApplicationException(CommonErrorCode.ERROR_ARGUMENT_NUM, "审核未通过,导出失败"); } ExportFwxxglVo exportFwxxglVo = new ExportFwxxglVo(); BeanUtils.copyProperties(fwxxgl, exportFwxxglVo); ar.add(exportFwxxglVo); } ExcelWriter excelWriter = null; try { excelWriter = EasyExcel.write(new BufferedOutputStream(outputStream)).build(); //list = Lists.newArrayList(list); exportExcel(excelWriter, 0, "房屋信息管理列表数据", ar, ExportFwxxglVo.class); log.info("导出成功"); } catch (Exception e) { log.error("项目导出异常", e); throw new RuntimeException("项目导出文件失败:" + e.getMessage()); } finally { if (excelWriter != null) { excelWriter.finish(); } } } private void exportExcel(ExcelWriter excelWriter, Integer sheetNo, String sheetName, List> dataList, Class> object) { WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo, sheetName) .head(object) .build(); excelWriter.write(dataList, writeSheet); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)