公司封装方式导出excel过程

1、IPrinterInfoServiceRemote层

@ApiOperation(value = "个人测试-导出打印机信息excel")
@PostMapping("/export")
ResponseData<PrinterInfoExportFileAResp> exportPrinterInfoExportFile(@RequestBody RequestData<PrinterInfoExportFileAReq> req);

2、PrinterInfoServiceRemoteImpl层

此处的list中如果存在null的字段则会报空指针异常

@Override
public ResponseData<PrinterInfoExportFileAResp> exportPrinterInfoExportFile(RequestData<PrinterInfoExportFileAReq> req) {
    
    
    //1、查询要导出的数据list
    List<PrinterInfoListAResp> list = null;
    //2、根据是否传入ids 判断要导出的数据列表 传入ids为null时输出 所有的数据 否则 输出相应的数据
    if(req.getRequest().getIds().size() > 0){
    
    
        list = printerInfoService.getListByIds(req.getRequest().getIds());
    }else{
    
    
        Example example = new Example(PrinterInfo.class, true, false);
        example.createCriteria().andEqualTo(PrinterInfo.FIELD_MARK, MarkEnum.NO_DELETE.getCode());
        List<PrinterInfo> listByExample = printerInfoService.getListByExample(example, null);
        list = BeanCopyUtils.beanCopy(PrinterInfoListAResp.class, listByExample);
    }
    PrinterInfoExportFileAResp resp = PrinterInfoExportFileAResp.builder().exportUrl(printerInfoService.exportPrinterInfoURL(list)).build();
    return RestResultGenerator.genSuccessResult(resp);
}

3、PrinterInfoService层

//todo 导出打印机信息 测试完后待删除 
public String exportPrinterInfoURL(List<PrinterInfoListAResp> list) {
    
    
    List<PrinterInfoBean> data = BeanCopyUtils.beanCopy(PrinterInfoBean.class, list);
    try {
    
    
        XSSFWorkbook workbook = PoiUtils.exportExcel(data, PrinterInfoBean.class, "打印机信息", true, 10000);
        String staticPath = ClassUtils.getDefaultClassLoader().getResource("tmp").getPath();
        File file = new File(staticPath + File.separator + DateUtils.convert2Str() +"打印机信息导出文件"+".xlsx");
        FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
        workbook.close();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(file.getPath());
        String url = AliyunOssUtils.uploadFile(stringBuilder.toString(), file);
        file.delete();
        return String.format("https://%s", url);

    } catch (Exception e) {
    
    
        e.printStackTrace();
    }
    return null;
}

//todo 批量查询 测试完后待删除 
public List<PrinterInfoListAResp> getListByIds(List<Long> ids) {
    
    
    return printerInfoDao.batchGetListByIds(ids);
}

4、PrinterInfoDao层

public List<PrinterInfoListAResp> batchGetListByIds(List<Long> ids) {
    
    
    return printerInfoMapper.batchGetListByIds(ids);
}

5、IPrinterInfoMapper层

 List<PrinterInfoListAResp> batchGetListByIds(@Param(value = "ids") List<Long> ids);

6、mapper.xml层

<select id="batchGetListByIds"
        resultType="com.PrinterInfoListAResp">
        SELECT DISTINCT
        b.id,
        a.data_name AS dataName,
        b.printer_name AS printerName,
        b.remark,
        b.printer_ip AS printerIp,
        b.printer_port AS printerPort,
        b.printer_state AS printerState,
        b.create_time AS createTime,
        b.creator_name AS creatorName,
        b.last_modified_time AS lastModifiedTime,
        b.editor_name AS editorName
    FROM
        printer_info b
    LEFT JOIN printer_config c ON c.printer_info_id = b.id
    LEFT JOIN sys_dict_data a ON a.data_value = c.data_value AND a.dict_code = "B0041"
    WHERE
        b.mark = 1
        and
        b.id in
        <foreach open="(" close=")" collection="ids" item="item" separator=",">
             #{item}
        </foreach>
    ORDER BY
        b.create_time DESC
</select>

7、pojo层

@Data
@Builder
public class PrinterInfoExportFileAResp extends BaseAResp{
    
    

    /**
     * 导出文件阿里云url
     */
    @ApiModelProperty(value = "导出文件阿里云url")
    private String exportUrl;

}
@Data
public class PrinterInfoExportFileAReq extends BaseAReq{
    
    

    @ApiModelProperty(value = "打印机id集合")
    private List<Long> ids;

}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PrinterInfoBean {
    
    

    /**
     * id
     */
    @ExcelHeader(value = "id")
    private Long id;

    /**
     * 字典值 记录类型
     */
    @ExcelHeader(value = "记录类型")
    private String dataName;

    /**
     * 打印机名称
     */
    @ExcelHeader(value = "打印机名称")
    private String printerName;

    /**
     * 打印机描述
     */
    @ExcelHeader(value = "打印机描述")
    private String remark;

    /**
     * 打印机IP
     */
    @ExcelHeader(value = "打印机IP")
    private String printerIp;

    /**
     * 打印机端口
     */
    @ExcelHeader(value = "打印机端口")
    private String printerPort;

    /**
     * 是否有效 0-否,1-是
     */
    @ExcelHeader(value = "是否有效 0-否,1-是")
    private Integer printerState;

    /**
     * 创建时间 记录插入时间
     */
    @ExcelHeader(value = "创建时间")
    private Date createTime;

    /**
     * 创建人姓名
     */
    @ExcelHeader(value = "创建人姓名")
    private String creatorName;

    /**
     * 更新时间 记录最后更新时间
     */
    @ExcelHeader(value = "修改时间")
    private Date lastModifiedTime;

    /**
     * 编辑人姓名
     */
    @ExcelHeader(value = "修改人姓名")
    private String editorName;
    
}

猜你喜欢

转载自blog.csdn.net/Crush_kylin/article/details/119083211