All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.tecsinapse.dataio.util.SpreadsheetUtil Maven / Gradle / Ivy

The newest version!
/*
 * Tecsinapse Data Input and Output
 *
 * License: GNU Lesser General Public License (LGPL), version 3 or later
 * See the LICENSE file in the root directory or .
 */
package br.com.tecsinapse.dataio.util;

import com.google.common.base.Strings;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class SpreadsheetUtil {

    public static int getColumnIndexByColumnName(String columnName) {
        columnName = getColumnFromCellName(columnName.toUpperCase());
        int value = 0;
        for (int i = 0; i < columnName.length(); i++) {
            int delta = (columnName.charAt(i)) - 64;
            value = value * 26 + delta;
        }
        return value - 1;
    }

    private static String getColumnFromCellName(String cellReference) {
        if (Strings.isNullOrEmpty(cellReference)) {
            return "";
        }

        return cellReference.split("[0-9]*$")[0];
    }

    public static String getColumnNameByColumnIndex(int columnIndex) {
        String s = "";
        do {
            s = (char) ('A' + (columnIndex % 26)) + s;
            columnIndex /= 26;
        } while (columnIndex-- > 0);
        return s;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy