import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.*;
import java.util.*;
import javax.swing.*;
// reading from excel
public class apachePOI3
{
public static String[][] read_String_from_excel1(String filename, int pageno)
{
    Workbook Work=wb(filename);
    String s[][]=new String [2000][2000];
    int i=0,j=0,i1=0,j1=0;
    Sheet sheet1 = Work.getSheetAt(pageno);
    Iterator rowItr = sheet1.rowIterator();  
    while ( rowItr.hasNext() ) 
    {   j=0;
	    Row row = (Row) rowItr.next();             
        Iterator cellItr = row.cellIterator();
        while ( cellItr.hasNext() ) 
        { s[i][j]=toString(cellItr);
          //in case of an empty line stop iteration
          if(s[i][j].charAt(0)==' ') {i=i1;j=j1;break;}
          //System.out.print("i="+i+"j="+j+"*"+s[i][j]+"* ");
          j++;j1=j;
        } 
   i++;i1=i;
   }
   int n=i1;
   int m=j1;
   String s1[][]=new String [n][m];
   for(i=0;i<n;i++)
   {for(j=0;j<m;j++)
     {s1[i][j]=s[i][j];}
   }
   return s1;
}	
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;
  System.out.println("nx0="+nx0+"nx1="+nx1+"nx="+nx);
  for(int i=0;i<nx;i++)
  {   Row r=sheet.getRow(i);
      if(r==null) break;
      if(i==nx0)
      {ny0= r.getFirstCellNum();
	   ny1= r.getLastCellNum();
	   ny=ny1-ny0;
	   System.out.println("ny0="+ny0+"ny1="+ny1+"ny="+ny);
       a=new String[nx][ny];
      }
	  for(int j=0;j<ny;j++)
      {  Cell c=r.getCell(j+ny0,Row.RETURN_NULL_AND_BLANK);
         if(c==null) a[i][j]="null";
         else a[i][j] =toString(c);
      } 
  }
} 
  catch(ArrayIndexOutOfBoundsException e2) {System.err.println("wrong array dimension");}
  catch(NullPointerException e3) {System.err.println("null pointer exception");}
  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);
}
}