import java.util.*;
import javax.swing.*;
// reading from excel
public class W13EX2
{
public static String[][] read_String_from_excel(String filename, int pageno)
{
  String a[][]=new String[10][10];	  
  try{
  int nx,ny=0,nx0=0,ny0=0,nx1=0,ny1=0;
  Workbook workbook =wb(filename);
  Sheet sheet= workbook.getSheetAt(pageno);
  nx1=sheet.getLastRowNum();
  nx0=sheet.getFirstRowNum();
  nx=nx1-nx0;
  for(int i=0;i<nx;i++)
  {   Row r=sheet.getRow(i+nx0);
      if(i==nx0)
      {ny0= r.getFirstCellNum();
	   ny1= r.getLastCellNum();
	   ny=ny1-ny0;
       a=new String[nx][ny];
      }
	  for(int j=0;j<ny;j++)
      {  Cell c=r.getCell(j+ny0,Row.RETURN_BLANK_AS_NULL);
	     a[i][j] =toString(c);
      } 
  }
} 
  catch(ArrayIndexOutOfBoundsException e2) {System.err.println("wrong array dimension");}
  return a;
}

public static String toString(Cell cell)
{
            String s="";
            if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN) 
            { s=cell.getBooleanCellValue() + "\t\t";}
            else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC)
            { s=cell.getNumericCellValue() + "\t\t";}
            else if(cell.getCellType()==Cell.CELL_TYPE_STRING)
            {s=cell.getStringCellValue() + "\t\t";}
            else if(cell.getCellType()==Cell.CELL_TYPE_FORMULA)
            {s=cell.getNumericCellValue() + "\t\t";}
            return s;
}
     public static String word(String s)
     {  //returns extension group of file name 
     StringTokenizer token=new StringTokenizer(s,".");
     int m=token.countTokens();
     String b[]=new String[m];
     int j=0;           
     while(token.hasMoreTokens())
     {
     String ax=(String)token.nextToken();
     b[j++]=ax;
     }
     return b[m-1];
     }
public static Workbook wb(String filename)
{
    String s2=word(filename);
    Workbook Work=new HSSFWorkbook();
    try {   
     FileInputStream inputStr= new FileInputStream(filename);
    if(s2.equals("xls"))
    {Work=new HSSFWorkbook(inputStr);}
    else
    {Work= new XSSFWorkbook(inputStr) ;} 
    } catch (Exception e) {e.printStackTrace();} 
    return Work;	
}    

public static void main(String arg[])
{    
     String s[][]=read_String_from_excel("DATA1.xls",0);
     JTableP.print(s);
}
}
