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 apachePOI1
{
public static String[][] read_String_from_excel(String filename, int pageno,int x0,int y0,int nx,int ny)
{
  String a[][]=new String[nx][ny];
  int nx1,ny1;	  
  Workbook workbook =wb(filename);
  Sheet sheet= workbook.getSheetAt(pageno);
  int totalrows=sheet.getLastRowNum();
  if(totalrows<nx) nx1=totalrows;
  else nx1=nx;
  for(int i=0;i<nx1;i++)
  {   Row r=sheet.getRow(x0+i);
      int totalCells = r.getPhysicalNumberOfCells();
      if(totalCells<ny) ny1=totalCells;
      else ny1=ny;
	  for(int j=0;j<ny1;j++)
      {  Cell c=r.getCell((y0+j),Row.RETURN_BLANK_AS_NULL);
           {a[i][j] =toString(c);}
      } 
  }
  return a;
}r

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);

}
}