package cn.common.common.utils;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author Singer create by Singer email:singer-coder@qq.com
 * @packageName cn.common.common.utils
 * @Description: 导出Excel辅助类
 * @date 2022-03-22
 */
@Slf4j
public class ExportExcelHandler {

    /**
     * 设置导出错误Response
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/12/13
     * @param response 导出Resp
     */
    public static void setExportErrorResponse(HttpServletResponse response){
        response.reset();
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
    }

    /**
     * 设置导出Response
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/12/13
     * @param response 导出Resp
     * @param fileName 文件名称
     */
    public static void setExportResponse(HttpServletResponse response,String fileName){
        try {
            response.setHeader("Content-Disposition","attachment;filename="
                    + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
        } catch (UnsupportedEncodingException e) {
            log.error(">>>>>>>>>>>>>>>>>导出数据发生异常:{},{}<<<<<<<<<<<<<<<<<",e.getMessage(),e);
        }
        response.setContentType("application/octet-stream;charset=UTF-8");
    }
}
