import javax.tools.*;
import java.util.*;
import java.lang.Math.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;
import jxl.*; 
import jxl.write.*;
import jxl.format.*;
import java.io.File;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.swing.filechooser.*;
import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.text.*;
import java.util.Locale;
// This class reads, writes and plot data into/from a given excel table
// 
public class excel
{
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];
try{
  File f=new File(filename);	  
  jxl.Workbook workbook = jxl.Workbook.getWorkbook(f);
  int nos=workbook.getNumberOfSheets();
  Sheet sheet= workbook.getSheet(pageno);
  Cell a1 = sheet.getCell(x0,y0);
  LabelCell lc = (LabelCell)a1;
  for(int i=0;i<nx;i++)
  { for(int j=0;j<ny;j++)
   {  a1 = sheet.getCell(x0+i,y0+j);
      lc = ( LabelCell) a1;
      a[i][j] = lc.getString();
   } 
  }
}catch(jxl.read.biff.BiffException be) {System.err.println("BiffException");}
catch(IOException bio) {System.err.println("Input output exception");}
  return a;
}

public static String[][] write_String_from_excel_to_text(String textfilename,String excelfilename, int pageno,String xy0,String xyn)
{String a[][]=read_String_from_excel(excelfilename,pageno,xy0,xyn);
  try {
  PrintWriter fout=new PrintWriter(new BufferedWriter(new FileWriter(textfilename)));
  int n=a.length;
  int m=a[0].length;
  for(int j=0;j<m;j++)
  {for(int i=0;i<n;i++)
   {fout.print(a[i][j]+"\t ");}
   fout.println("\n");
  }
  }
 catch(IOException e1) {System.err.println("girdi hatası.");}
  return a;
}

public static String[][] read_String_from_excel(String filename, int pageno,String xy0,String xyn)
{int n0[]=excelcode(xy0);
 int n1[]=excelcode(xyn);
 int nx=n1[0]-n0[0]+1;
 int ny=n1[1]-n0[1]+1;
 return read_String_from_excel(filename,pageno,n0[0],n0[1],nx,ny);
}

public static String[][] read_String_from_excel(String filename, int pageno,String xy0,int nx,int ny)
{int n0[]=excelcode(xy0);
 return read_String_from_excel(filename,pageno,n0[0],n0[1],nx,ny);
}

public static double[][] read_double_from_excel(String filename, String pagetitle,int x0,int y0,int nx,int ny)
{
double a[][]=new double[nx][ny];
try{
  File f=new File(filename);	  
  jxl.Workbook workbook = jxl.Workbook.getWorkbook(f);
  int nos=workbook.getNumberOfSheets();
  String sheets="";
  int pageno=0;
  Sheet sheet;
  for(int i=0;i<nos;i++)
  {sheet = workbook.getSheet(i);
   sheets=sheet.getName();
   if(sheets.equals(pagetitle)) {pageno=i;break;} 
  }
  sheet= workbook.getSheet(pageno);
  Cell a1 = sheet.getCell(x0,y0);
  NumberCell nc = (NumberCell) a1;
  for(int i=0;i<nx;i++)
  { for(int j=0;j<ny;j++)
   {  a1 = sheet.getCell(x0+i,y0+j);
      nc = (NumberCell) a1;
      a[i][j] = nc.getValue();
   }
 
  }
}catch(jxl.read.biff.BiffException be) {System.err.println("BiffException");}
catch(IOException bio) {System.err.println("Input output exception");}
  return a;
}

public static double[][] read_double_from_excel(String filename, String pagetitle,String xy0,String xyn)
{int n0[]=excelcode(xy0);
 int n1[]=excelcode(xyn);
 int nx=n1[0]-n0[0]+1;
 int ny=n1[1]-n0[1]+1;
 System.out.println("nx="+nx+"ny="+ny);
 return read_double_from_excel(filename,pagetitle,n0[0],n0[1],nx,ny);
}

public static double[][] read_double_from_excel(String filename, String pagetitle,String xy0,int nx,int ny)
{int n0[]=excelcode(xy0);
 return read_double_from_excel(filename,pagetitle,n0[0],n0[1],nx,ny);
}

public static double[][] read_double_from_excel(String filename, int pageno,int x0,int y0,int nx,int ny)
{
double a[][]=new double[nx][ny];
try{
  File f=new File(filename);	  
  jxl.Workbook workbook = jxl.Workbook.getWorkbook(f);
  int nos=workbook.getNumberOfSheets();
  Sheet sheet= workbook.getSheet(pageno);
  Cell a1 = sheet.getCell(x0,y0);
  NumberCell nc = (NumberCell) a1;
  for(int i=0;i<nx;i++)
  { for(int j=0;j<ny;j++)
   {  a1 = sheet.getCell(x0+i,y0+j);
      nc = (NumberCell) a1;
      a[i][j] = nc.getValue();
   }
  }
}catch(jxl.read.biff.BiffException be) {System.err.println("BiffException");}
catch(IOException bio) {System.err.println("Input output exception");}
  return a;
}

public static double[][] read_double_from_excel(String filename, int pageno,int x0,int y0,int nx)
{
try{
  File f=new File(filename);	  
  jxl.Workbook workbook = jxl.Workbook.getWorkbook(f);
  int nos=workbook.getNumberOfSheets();
  Sheet sheet= workbook.getSheet(pageno);
    //dolu excel satırlarını say
  String stringa1="xx";
  Cell a1 = sheet.getCell(x0,y0);
  int j=0;
  try
  {
  while(stringa1!="")
  {	  
  a1 = sheet.getCell(x0,j);
  stringa1 = a1.getContents();
  j++; 	  
  }
  }catch(ArrayIndexOutOfBoundsException ai) {}  
  //bu excell sayfası için satır sayısı = n 
  int ny=j-2;
  double a[][]=new double[nx][ny];  
  a1 = sheet.getCell(x0,y0);
  NumberCell nc = (NumberCell) a1;
  for(int i=0;i<nx;i++)
  { for(j=0;j<ny;j++)
   {  a1 = sheet.getCell(x0+i,y0+j);
      nc = (NumberCell) a1;
      a[i][j] = nc.getValue();
   } 
  }
  return a;
}catch(jxl.read.biff.BiffException be) {System.err.println("BiffException");}
catch(IOException bio) {System.err.println("Input output exception");}
double b[][]=new double[1][1];
return b;
}

public static double[][] read_double_from_excel(String filename, String pagetitle,int x0,int y0,int nx)
{
try{
  File f=new File(filename);	  
  jxl.Workbook workbook = jxl.Workbook.getWorkbook(f);
  int nos=workbook.getNumberOfSheets();
  String sheets="";
  int pageno=0;
  Sheet sheet;
  for(int i=0;i<nos;i++)
  {sheet = workbook.getSheet(i);
   sheets=sheet.getName();
   if(sheets.equals(pagetitle)) {pageno=i;break;} 
  }
  sheet= workbook.getSheet(pageno);
    //dolu excel satırlarını say
  String stringa1="xx";
  Cell a1 = sheet.getCell(x0,y0);
  int j=0;
  try
  {
  while(stringa1!="")
  {	  
  a1 = sheet.getCell(x0,j);
  stringa1 = a1.getContents();
  j++; 	  
  }
  }catch(ArrayIndexOutOfBoundsException ai) {}  
  //bu excell sayfası için satır sayısı = n 
  int ny=j-2;
  double a[][]=new double[nx][ny];
  a1 = sheet.getCell(x0,y0);
  NumberCell nc = (NumberCell) a1;
  for(int i=0;i<nx;i++)
  { for(j=0;j<ny;j++)
   {  a1 = sheet.getCell(x0+i,y0+j);
      nc = (NumberCell) a1;
      a[i][j] = nc.getValue();
   }
 
  }
  return a;
}catch(jxl.read.biff.BiffException be) {System.err.println("BiffException");}
catch(IOException bio) {System.err.println("Input output exception");}
double b[][]=new double[1][1];
return b;
}

public static double[][] read_double_from_excel(String filename, int pageno,String xy0,String xyn)
{int n0[]=excelcode(xy0);
 int n1[]=excelcode(xyn);
 int nx=n1[0]-n0[0]+1;
 int ny=n1[1]-n0[1]+1;
 //System.out.println("nx="+nx+"ny="+ny);
 return read_double_from_excel(filename,pageno,n0[0],n0[1],nx,ny);
}

public static double[][] read_double_from_excel(String filename, int pageno,String xy0,int nx,int ny)
{int n0[]=excelcode(xy0);
 return read_double_from_excel(filename,pageno,n0[0],n0[1],nx,ny);
}
public static double[][] read_double_from_excel(String filename, int pageno,String xy0,int nx)
{int n0[]=excelcode(xy0);
 return read_double_from_excel(filename,pageno,n0[0],n0[1],nx);
}
public static double[][] read_double_from_excel(String filename, String pagetitle,String xy0,int nx)
{int n0[]=excelcode(xy0);
 return read_double_from_excel(filename,pagetitle,n0[0],n0[1],nx);
}


public static void write_double_to_excel(String filename, String pagetitle,String xy0,double a[][]) throws IOException
{ int n0[]=excelcode(xy0);
int n=a.length;
int m=a[0].length;
try
{
WritableWorkbook workbook = Workbook.createWorkbook(new File(filename)); 
WritableSheet sheet = workbook.createSheet(pagetitle, 0); 
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
jxl.write.Number nx = new jxl.write.Number(n0[0]+i, n0[1]+j,a[i][j]); 
sheet.addCell(nx); 
}
}
workbook.write();
workbook.close();  
}catch(jxl.write.WriteException we) {System.out.println("write exception");}
}

public static void write_double_to_excel(String filename, String pagetitle,double a[][]) throws IOException
{ write_double_to_excel(filename,pagetitle,"A1",a);}

public static void write_double_to_excel(String filename,double a[][]) throws IOException
{ write_double_to_excel(filename,"Page 1","A1",a);}

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)
{int len = s.length();
 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[0]=(charnumber(a[0])+1)*26+charnumber(a[1]);
 for(int i=2;i<len;i++) s1+=a[i];
 }
 else
 {n1[0]=charnumber(a[0]);
 for(int i=1;i<len;i++) s1+=a[i];
 }
 n1[1]=Integer.parseInt(s1)-1;
 return n1;
}
    public static String filename()    
    {  String dosyaismi="a.xls";
	   JFileChooser fc=new JFileChooser();
       ExampleFileFilter filter = new ExampleFileFilter();
       filter.addExtension("xls");
       filter.setDescription("read excel file");
       fc.setFileFilter(filter);
       if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();dosyaismi=file.getName(); };
       return dosyaismi;
    }
    
    public static String[] pagenames(String filename)
    {
	String a[]=new String[1];
	a[0]="error file name is not read";    
	try{
	File f=new File(filename);
	jxl.Workbook workbook;
	String pagenames[];	  
    workbook = jxl.Workbook.getWorkbook(f);
    int nos=workbook.getNumberOfSheets();
    pagenames=new String[nos];
    String sheets="";
    int pageno=0;
    Sheet sheet;
    for(int i=0;i<nos;i++)
    {sheet = workbook.getSheet(i);
	 pagenames[i] = sheet.getName();
	return pagenames;
	}
	}catch(IOException e){System.err.println("Input -output error");}    
     catch(jxl.read.biff.BiffException e1){System.err.println("excel Input -output error");}
	return a;
    }
       
    public static String[] filename_and_pagenames()
    {
    String filename=filename();
    String a[]=pagenames(filename);
    int n=a.length;
    int n1=n+1;
    String b[]=new String[n1];
    b[0]=filename;
    for(int i=1;i<n1;i++)
    {b[i]=a[i-1];}
    return b;  
    }
public static void plot(String filename, String pagetitle,int x0,int y0,int nx,int ny)
{
double a[][]=read_double_from_excel(filename,pagetitle,x0,y0,nx,ny);
Plot pp=new Plot(a);
pp.plot();
}

public static void plot(String filename, String pagetitle,String xy0,String xyn)
{
double a[][]=read_double_from_excel(filename,pagetitle,xy0,xyn);
Plot pp=new Plot(a);
pp.plot();
}

public static void plot(String filename, String pagetitle,String xy0,int nx,int ny)
{
double a[][]=read_double_from_excel( filename, pagetitle,xy0,nx,ny);
Plot pp=new Plot(a);
pp.plot();
}

public static void plot(String filename, int pageno,int x0,int y0,int nx,int ny)
{
double a[][]=read_double_from_excel(filename,pageno,x0,y0,nx,ny);
Plot pp=new Plot(a);
pp.plot();}

public static void plot(String filename, int pageno,int x0,int y0,int nx)
{
double a[][]=read_double_from_excel(filename,pageno,x0,y0,nx);
Plot pp=new Plot(a);
pp.plot();
}

public static void plot(String filename, String pagetitle,int x0,int y0,int nx)
{
double a[][]=read_double_from_excel(filename,pagetitle,x0,y0,nx);
Plot pp=new Plot(a);
pp.plot();
}

public static void plot(String filename, int pageno,String xy0,String xyn)
{
double a[][]=read_double_from_excel(filename,pageno,xy0,xyn);
Plot pp=new Plot(a);
pp.plot();
}

public static void plot(String filename, int pageno,String xy0,int nx,int ny)
{
double a[][]=read_double_from_excel(filename,pageno,xy0,nx,ny);
Plot pp=new Plot(a);
pp.plot();
}

public static void plot(String filename, int pageno,String xy0,int nx)
{
double a[][]=read_double_from_excel(filename,pageno,xy0,nx);
Plot pp=new Plot(a);
pp.plot();
}

public static void plot(String filename, String pagetitle,String xy0,int nx)
{
double a[][]=read_double_from_excel(filename,pagetitle,xy0,nx);
Plot pp=new Plot(a);
pp.plot();
}
public static void main(String arg[])
{
double a[][]=read_double_from_excel("DATA4.xls",0,"B1","C20"); 
Text.print(Text.T(a));
}             

}