Monday, October 18, 2010

Java Program for finding row and colum No in Jxl.

Most off time jxl programmer face the problem of getting the value of Cell  by its name.You can use following class for getting row and column parameter. You have to create only new object of this class  with  namebox in xlsheet (Cell name)as input parameter and call the the get method of row and column  .
e.g.
JxlColumnRow jcr=new JxlColumnRow("B12");
Cell cs= sheetObject.getCell(jcr.getColumn(),jcr.geRow());

//Code Section
public class JxlColumnRow {
    private String[] letters = new String[] {"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    private String nameBox="";
    public JxlColumnRow(String nameBx)
    {
        nameBox=nameBx;
    }
    public int getRow()
    {
        String ans = "";
        for(int i=1;i<nameBox.length()+1;i++)
        {
            if(isInteger(nameBox.substring(i-1,i)))
            {
                ans = nameBox.substring(i-1);
                break;
            }
        }
        return Integer.parseInt(ans)-1;
    }
       

    public int getColumn()
    {
        int ans = 0;
        String col = "";
        for(int i=0;i<nameBox.length();i++)
        {
            String temp = nameBox.substring(i,i+1);
            if(isInteger(temp))
            {
                col = nameBox.substring(0,i);
                break;
            }
        }
        for(int i=0;i<col.length();i++)
        {
            int count = col.length();
            String temp = col.substring(i,i+1);
            for(int x=0;x<letters.length;x++)
            {
                if(temp.equals(letters[x]))
                {
                    int tempCount = count-i;
                    ans += (Math.pow(26,tempCount-1)*x);
                    break;
                }
            }
        }
        return ans-1;
    }
   
    //Dirty method to determine if inputted string is an integer
    public boolean isInteger( String input )
    {
    try {
        Integer.parseInt( input );
        return true;
    }
    catch( Exception e ) {
        return false;
    }
    }
 

}

No comments:

Post a Comment