Results 1 to 3 of 3

Thread: Excel Column

  1. #1
    Join Date
    Aug 2007
    Rep Power

    Default Excel Column


    Presently, I am using excel interface with JAVA using jcom library

    As you know, Excel has columns: A,B,C....Z,AA,AB,AC,......,AZ,BA,BB,....BZ,CA... and so on

    My problem is that I want to create a function in JAVA whose prototype is:
    String getColName(String col,int add)
    you can understand working of this function by following examples:
    getColName("H",0) returns "H", getColName("H",3) returns "K"
    getColName("Y",3) returns "AB",getColName("AZ",5) returns "HE"
    And So On

    I hope, u understand that what kind of function, I want to create

    Please tell me the logic behind it.


  2. #2
    Join Date
    Apr 2008
    Fort Wayne, Indiana
    Rep Power


    This should do what you want. This however does not have functionality to accept ZZ and output AAA. I didnt bother seeing as how excel wont even reach ZZ. This shouldnt be hard to add though if someone wants to put it in.

    Java Code:
      static String getColName(String temp, int over) {
        int value;
        int last;
        char[] tempch = temp.toCharArray();
        last = tempch.length - 1;
        value = tempch[last] + over;
        tempch[last] = (char) value;
        if (value > (int) 'Z') {
          tempch[last] = (int) 'A';
          over = (value - (int) 'Z' - 1);
          if (last == 0) {
            tempch = (("A") + String.copyValueOf(tempch)).toCharArray();
          } else {
            tempch[last - 1] = (char) ((int) tempch[last - 1] + 1);
          tempch = getColName(String.copyValueOf(tempch), over).toCharArray();
        return String.copyValueOf(tempch);

  3. #3
    Join Date
    Aug 2007
    Rep Power

    Default Best Logic for Excel Column

    Your logic is very simple. Thanx for it.

    However here is exact solution of this problem:
    It will returns "AAA" for getColName("ZZ",1)

    Thanx Again...

    Problem solved.

Similar Threads

  1. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 06:10 PM
  2. How to sort column in JTable
    By johnt in forum AWT / Swing
    Replies: 3
    Last Post: 06-14-2008, 05:48 AM
  3. CSV column titles
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-24-2008, 03:45 PM
  4. Display Line# and Column# in JSP
    By loganathan.lakshmanan in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 01-17-2008, 12:37 PM
  5. add a jlist column
    By Alan in forum JCreator
    Replies: 1
    Last Post: 05-28-2007, 04:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts