springboot项目中导出接口实现

springboot项目中导出接口实现,第1张

springboot项目中导出接口实现 springboot项目中导出接口实现

返回结果对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultInfo implements Serializable {
    private static final long serialVersionUID = -3986667778876817461L;
    private String code;
    private String msg;
    private T data;

    public ResultInfo(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }
}

状态枚举类

public enum ResponseStatusEnum {
    SUCCESS("000000", "成功"),
    FAILED("000001", "失败");
    private String code;
    private String msg;
    ResponseStatusEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public String getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

导出结果工具类封装

import com.cmbchina.riskcontroll.controller.RiskController;
import com.cmbchina.riskcontroll.statusenum.ResponseStatusEnum;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;


@Component
public class ExportUtils {

    private static final Logger logger = LoggerFactory.getLogger(RiskController.class);

    //创建HSSFWorkbook并相应到客户端
    public static void createHSSFWorkbook(FileInfo fileInfo,HttpServletResponse response){
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtils.getHssfWorkbook(fileInfo.getSheetName(), fileInfo.getTitle(), fileInfo.getContent(), null);
        //响应到客户端
        try {
            ExportUtils.setResponseHeader(response, fileInfo.getFileName());
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            logger.error("导出excel异常", e);
        }
    }

    
    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            fileName = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1.name());
        } catch (UnsupportedEncodingException e) {
            logger.error("编码转换异常", e);
        }
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            logger.error("设置响应流方法异常", ex);
        }
    }

    public static ResultInfo checkExportCount(ResultInfo resultInfo, int count,int limitCount) {
        //        判断数据是否大于10000条,如果大于10000条给出提示需要设置筛选条件
        if (count > limitCount) {
            resultInfo.setCode(ResponseStatusEnum.FAILED.getCode());
            resultInfo.setMsg("导出数据结果集不能够超出"+limitCount+"条,请缩小导出范围");
        }
        return resultInfo;
    }
}

具体接口

    @RequestMapping("/exportList")
    public ResultInfo export(@RequestBody Object object, HttpServletResponse response){
        ResultInfo resultInfo=ResultUtil.getSuccessResult();
        int count =userService.getCount(object);
        resultInfo= ExportUtils.checkExportCount(resultInfo,count,limitCount);
        if (!ResponseStatusEnum.SUCCESS.getCode().equals(resultInfo.getCode())){
            return resultInfo;
        }
        object.setPageSize(limitCount);
        object.setCurrentPage(1);
        List list=userService.getList(object);
        // 对应字段中文解释
        String[] title={"aa","bb","cc","dd","ee"};
        FileInfo fileInfo=new FileInfo();
        fileInfo.setTitle(title);
        fileInfo.setFileName("文件名"+System.currentTimeMillis()+".xls");
        fileInfo.setSheetName("sheet名");
        String[][] content= new String[list.size()][title.length];
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i=0;i					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5676662.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)