Descarga del formulario Java

Descarga de formulario muy fácil de usar

Insertar descripción de la imagen aquí

Primero agregue paquetes de dependencia al archivo pom

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.6</version>        
 </dependency>

AutomaticNegationRes es la clase de retorno de la respuesta de la interfaz

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel
public class AutomaticNegationRes implements Serializable {
    
    
    @ApiModelProperty(notes = "主键id")
    private Long id;

    @ApiModelProperty(notes = "关键词id")
    private String keywordId;

    @ApiModelProperty(notes = "渠道")
    private String platform;

    @ApiModelProperty(notes = "账号")
    private String account;

    @ApiModelProperty(notes = "计划")
    private String plan;

    @ApiModelProperty(notes = "单元")
    private String unit;

    @ApiModelProperty(notes = "关键词")
    private String keyword;

    @ApiModelProperty(notes = "账面商机成本")
    private Double cost;

    @ApiModelProperty(notes = "日账面消费")
    private Double consume;

    @ApiModelProperty(notes = "商机数")
    private Integer business;



}

KeywordResExcel es la plantilla de descarga de Excel para descargar

import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("关键字对象实体")
public class KeywordResExcel {
    
    


    @ExcelProperty("渠道")
    @ApiModelProperty(notes = "渠道")
    private String platform;

    @ExcelProperty("账户")
    @ApiModelProperty(notes = "账户")
    private String account;

    @ExcelProperty("推广计划")
    @ApiModelProperty(notes = "推广计划")
    private String plan;

    @ExcelProperty("推广单元")
    @ApiModelProperty(notes = "推广单元")
    private String unit;

    @ExcelProperty("关键词")
    @ApiModelProperty(notes = "关键词")
    private String keyword;

    @ExcelProperty("账面商机成本")
    @ApiModelProperty(notes = "账面商机成本")
    private Double cost;

    @ExcelProperty("日账面消费")
    @ApiModelProperty(notes = "日账面消费")
    private Double consume;

    @ExcelProperty("商机数")
    @ApiModelProperty(notes = "商机数")
    private Integer business;
}

Escriba el método de implementación en serviceImpl

  @Override
    public RestfulResult downKeyword(List<String> ids, HttpServletResponse response) {
    
    
        List<AutomaticNegationRes> list = automaticNegationMapper.getByIds(ids);
        List<KeywordResExcel> excelList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
    
    
            for (AutomaticNegationRes channelRevenuePo : list) {
    
    
                KeywordResExcel keywordResExcel = new KeywordResExcel();
                BeanUtils.copyProperties(channelRevenuePo, keywordResExcel);
                excelList.add(keywordResExcel);
            }
        }
        String targetFilePath = String.valueOf(System.currentTimeMillis()).concat(".xlsx");
        response.setHeader("Content-Disposition", "attachment;filename=" + targetFilePath);
        try {
    
    
            EasyExcel.write(response.getOutputStream(), KeywordResExcel.class).sheet("关键词数据集合").doWrite(excelList);
        } catch (IOException e) {
    
    
            log.error("下载异常,{}",e.getMessage());
            return RestfulResult.failure(ResCode.KEYWORD_DATA_DOWNLOAD_EXCEPTION);
        }
        return RestfulResult.success();
    }

Llame al método de descarga en el controlador.

   /**
     * 下载导出选中的关键词集合
     *
     * @return 导出选中的关键词excel
     */
    @ApiOperation(value = "导出选中的关键词", notes = "导出选中的关键词")
    @GetMapping("/datas/download")
    public RestfulResult downKeyword(@RequestParam("ids")List<String> ids,  HttpServletResponse response) {
    
    
        return automaticNegationService.downKeyword(ids, response);

    }

La función de exportación de Excel está completa, es muy fácil de usar.

Insertar descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/qq_43012298/article/details/121253536
Recomendado
Clasificación