 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 apachePOI5
{
public static void write_to_excel(String filename, int pageno,String xy0,Object a[][])
{
int n0[]=excelcode(xy0);
int nx=n0[0];
int ny=n0[1];
Workbook workbook =wb1(filename);
String s1=""+pageno;
 Sheet sheet= workbook.createSheet(s1);
 int n=a.length;
 int m=a[0].length;
 for(int i=0;i<n;i++)
 { Row row=sheet.createRow(nx+i);
   for(int j=0;j<m;j++)
   {Cell cell=row.createCell(ny+j);
    String adress=new CellReference(cell).formatAsString();
    cell.setCellValue((String)a[i][j]);
   }
 }
try {   
     FileOutputStream out=new FileOutputStream(filename);
     workbook.write(out);
     out.close();
    } catch (Exception e) {e.printStackTrace();} 
}
public static boolean isLetter(char x)
{ boolean x1;
  if((x>='A' && x<='Z') || (x>='a' && x<='z')) x1=true;
  else x1=false;  
  return x1;	
}

public static boolean isNumber(char x)
{ boolean x1;
  if(x>='0' && x<='9') x1=true;
  else x1=false;
  return x1;	
}

public static int charnumber(char x)
{ 
  char b1[]={'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'};
  char b2[]={'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'};
  for(int i=0;i<26;i++)
  {if(x==b1[i] || x==b2[i]) return i;}
  return 0;
}

public static int[] excelcode(String s)
{  s=s.trim();
   int len = s.length();
   //System.out.println("excelcode = "+s+"len="+len);
 int n[]=new int[2];
 String s1="";
 char a[]=new char[len];
 int n1[]=new int[2];
 for(int i=0;i<len;i++)
 {a[i]=s.charAt(i);
 }
 if(isLetter(a[0]) && isLetter(a[1]))
 {
 n1[1]=(charnumber(a[0])+1)*26+charnumber(a[1]);
 for(int i=2;i<len;i++) s1+=a[i];
 }
 else
 {n1[1]=charnumber(a[0]);
 for(int i=1;i<len;i++) s1+=a[i];
 }
 n1[0]=Integer.parseInt(s1)-1;
 return n1;
}
public static Workbook wb1(String filename)
{   String s2=word(filename);
    Workbook Work=new HSSFWorkbook();
    try {   
    if(s2.equals("xls"))
    {Work=new HSSFWorkbook();}
    else
    {Work= new SXSSFWorkbook() ;} 
    } catch (Exception e) {e.printStackTrace();} 
    return Work;	
}
     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 void main(String arg[])
{    
     String s[][]={{"0","1","2","3","4","5","6","7","8","9"},
                   {"10","11","12","13","14","15","16","17","18","19"},
                   {"20","21","22","23","24","25","26","27","28","29"}};
     write_to_excel("DATA2.xls",1,"B2",s);;
}
}
