导出文件(下载文件)

 public void downloadExcel(Class c, List list, HttpServletResponse response, String fileName) {
        try {
            ExportParams exportParams = new ExportParams();
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, c, list);
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
            response.setHeader("content-Type", "application/vnd.ms-excel");
            workbook.write(response.getOutputStream());
            workbook.close();
            response.flushBuffer();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new CheckException("导出excel出错!");
        }
    }

导出的传输类,在需要导出的字段上添加注解 @Excel(name=“物料编码”):

@Data
public class AgencyConsumableDto{

    @Excel(name="物料编码")
    @ApiModelProperty(value = "物料编码")
    private String consumableCode;
    @Excel(name="物料名称")
    @ApiModelProperty(value = "物料名称")
    private String consumableName;
    @Excel(name="物料分类")
    @ApiModelProperty(value = "物料分类名称")
    private String consumableTypeName;
    @Excel(name="品牌")
    @ApiModelProperty(value = "品牌")
    private String brand;
    @Excel(name="规格型号")
    @ApiModelProperty(value = "规格型号")
    private String model;
    @Excel(name="单位")
    @ApiModelProperty(value = "单位")
    private String unit;
    @Excel(name="物料条码")
    @ApiModelProperty(value = "物料条码")
    private String barCode;
    @Excel(name="采购员")
    @ApiModelProperty(value = "采购员")
    private String buyer;
    @Excel(name="供应商")
    @ApiModelProperty(value = "供应商")
    private String supplier;
    @Excel(name="成本计算方法")
    @ApiModelProperty(value = "成本计算方法")
    private String computingMethod;



}

测试:

    @ApiOperation("导出全部耗材")
    @EmployeePermission("consumable:agencyConsumable:exportAllConsumable")
    @GetMapping("/exportAllConsumable")
    public Result exportAllConsumable(HttpServletResponse response) throws Exception {
        List<AgencyConsumableDto> expList = agencyConsumableService.getAllConsumable();
        try {
            ExcelUtil.downloadExcel(AgencyConsumableDto.class,expList,response,"耗材_"+System.currentTimeMillis()+".xls");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OperationFailedException("导出全部耗材失败");
        }
        return Result.success();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值