zoukankan      html  css  js  c++  java
  • Java 导出 Excel 列号数字与字母互相转换工具

    package test;
    
    /**
     * Deal with Excel column indexToStr and strToIndex
     * @author 
     * @version 2015-7-8
     * @see
     */
    public class ExcelColumn {
    
        public static void main(String[] args) {
            String colstr = "AA";
            int colIndex = excelColStrToNum(colstr, colstr.length());
            System.out.println("'" + colstr + "' column index of " + colIndex);
    
            colIndex = 26;
            colstr = excelColIndexToStr(colIndex);
            System.out.println(colIndex + " column in excel of " + colstr);
    
            colstr = "AAAA";
            colIndex = excelColStrToNum(colstr, colstr.length());
            System.out.println("'" + colstr + "' column index of " + colIndex);
    
            colIndex = 466948;
            colstr = excelColIndexToStr(colIndex);
            System.out.println(colIndex + " column in excel of " + colstr);
        }
    
        /**
         * Excel column index begin 1
         * @param colStr
         * @param length
         * @return
         */
        public static int excelColStrToNum(String colStr, int 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;
        }
    
        /**
         * Excel column index begin 1
         * @param columnIndex
         * @return
         */
        public static String excelColIndexToStr(int columnIndex) {
            if (columnIndex <= 0) {
                return null;
            }
            String columnStr = "";
            columnIndex--;
            do {
                if (columnStr.length() > 0) {
                    columnIndex--;
                }
                columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
                columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);
            } while (columnIndex > 0);
            return columnStr;
        }
    }
  • 相关阅读:
    postman参数化—上个接口返回数据作为下个接口入参
    postman设置token为全局变量
    postman请求https协议接口
    安装VMware 置灰正确解决办法
    Cron 表达式详解
    Jmeter + ant + jenkins轻量级接口自动化测试
    Jmeter CSV 参数化/检查点/断言
    Android专项测试-cpu,流量
    Android专项测试-获取启动时间
    腾讯云测试工程师--面试总结
  • 原文地址:https://www.cnblogs.com/yysbolg/p/10323124.html
Copyright © 2011-2022 走看看