package net.northking.cctp.common.util.excelUtil;

import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: chaoyi.zhang
 * @Date: 2020/8/31 0031
 */
public class ExcelToolUtil {
    /**
     * 列字母改为序号 A-1
     * @param colStr
     * @return
     */
    public static int excelColStrToNum(String colStr) {
        int length = colStr.length();
        int num = 0;
        int result = 0;
        for(int i = 0; i < length; i++) {
            char ch = colStr.charAt(length - i - 1);
            num = (int)(ch - 'A' + 1) ;
            num *= Math.pow(26, i);
            result += num;
        }
        return result;
    }

    /**
     * 列序号换成字母 0-A
     * @param num
     * @return
     */
    public static String getExcelColumnLabel(int num) {
        String temp = "";
        double i = Math.floor(Math.log(25.0 * (num) / 26.0 + 1) / Math.log(26)) + 1;
        if (i > 1) {
            double sub = num - 26 * (Math.pow(26, i - 1) - 1) / 25;
            for (double j = i; j > 0; j--) {
                temp = temp + (char) (sub / Math.pow(26, j - 1) + 65);
                sub = sub % Math.pow(26, j - 1);
            }
        } else {
            temp = temp + (char) (num + 65);
        }
        return temp;
    }

    /**
     * 输出流处理
     * @param response
     * @param fileName
     */
    public static void dealResponse(HttpServletResponse response, String fileName){
        response.reset();
        response.setStatus(HttpStatus.OK.value());
        response.setHeader("Content-Type", "application/octet-stream;charset=UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
    }
}
