import java.io.*;
import javax.swing.*;
import jxl.*; 
import jxl.write.*;
import java.io.File;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.swing.filechooser.*;
 
//import java.util.Locale;

public class YTO_coaxial
{
public static void main(String arg[]) throws IOException
{
double x[];
double y[];
double tg[]={30,17.77777778,30,30,15,12.22222222};
double tk[]={35,35,40,5,5,2};
String stk[]={"su ı.d. tgiriş","t yoğuşma","t yoğuşma","t buharlaşma","t buharlaşma","t buharlaşma"};
String isim[]; 
String baslik[]={"model","nominal KW","gercek KW","m1 kg/s","m2 kg/s","m3 kg/s","m4 kg/s","T1 derece C","T2 derece C","T3 derece C","T4 derece C","P1 bar","P2 bar","P3 bar","P4 bar" };        	
String baslik1[]={"nominal KW","gercek KW","m1 kg/s","m2 kg/s","m3 kg/s","m4 kg/s","T1  derece C","T2 derece C","T3 derece C","T4 derece C","P1 bar","P2 bar","P3 bar","P4 bar" };        	


steam b;
b=new steam();
double P=1.2;
String s1="YTO_coaxial.xls";
JFileChooser fc=new JFileChooser();
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("xls");
filter.setDescription("Excel dosyası açımı");
fc.setFileFilter(filter);
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {File file = fc.getSelectedFile();s1=file.getName(); }
File f=new File(s1);
int snl=s1.length();
StringBuffer sb1=new StringBuffer(s1);
sb1.insert(snl-4,"_1");
String s2=sb1.toString();
File f1=new File(s2);
try
{
Workbook workbook = Workbook.getWorkbook(f); 
String rn[]={"su ısı değiştirici","yoğuşturucu 1","yoğuşturucu 2","buharlaştırıcı 1","buharlaştırıcı 2","buharlaştırıcı 3"};
WritableWorkbook workbook1 = Workbook.createWorkbook(f1, workbook);  
int sn;
Sheet sheet;
WritableSheet wsheet;
  sheet = workbook.getSheet(0);	  
  Cell a1 = sheet.getCell(0,0); 
  String stringa1 = a1.getContents();
  int i=1; 
  int j=0;
  
  try
  {
  while(stringa1!="")
  {	  
  a1 = sheet.getCell(0,i);
  stringa1 = a1.getContents();
  //System.out.println(stringa1);
  i++; 	  
  }
  }catch(ArrayIndexOutOfBoundsException ai) {}
  int n=i-1;
  isim=new String[n];
  try
  {
  for(i=0;i<n;i++)
  {	  
  a1 = sheet.getCell(0,i);
  isim[i] = a1.getContents();
  //System.out.println(isim[i]);
  }
  }catch(ArrayIndexOutOfBoundsException ai) {}
  
  double a[][]=new double[11][n];
  
  for(i=0;i<n;i++)
  {	  
  for(j=0;j<11;j++)
  {  
	  a1 = sheet.getCell(j+1,i);
      if (a1.getType() == CellType.NUMBER)
      {
      NumberCell nc = (NumberCell) a1;
      a[j][i] = nc.getValue();
      } 
  }
  }
  Text.print(Text.T(a));
  double tc[][]=new double[6][n];
  try{	
  for(sn=0;sn<6;sn++)
  {  
  wsheet = workbook1.createSheet(rn[sn], sn+1);
  WritableCellFormat wcf=new WritableCellFormat();
  //wcf.setBackground(acolor[i]);
  wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); 
  double nominalW[]=new double[n];
  double tamW[]=new double[n];
  double tcik[][]=new double[4][n];
  double m[][]=new double[4][n];
  double tm[][]=new double[4][n];
  double pd[][]=new double[4][n];   
  for(i=0;i<n;i++)
  {
   nominalW[i]=a[0][i]*3.51685284;//KW
   tamW[i]=a[1+sn][i]*0.000290371;//KW
   m[0][i]=a[0][i]*0.227124707/3.6;
   m[1][i]=a[0][i]*0.227124707*2/3.6;
   m[2][i]=a[0][i]*0.227124707*3/3.6;
   m[3][i]=a[0][i]*0.227124707*4/3.6;
   pd[0][i]=a[7][i]*0.0689475728;
   pd[1][i]=a[8][i]*0.0689475728;
   pd[2][i]=a[9][i]*0.0689475728;
   pd[3][i]=a[10][i]*0.0689475728;
   double a11[]=b.property("tp",tg[sn],P); 
   double h1=a11[3];   
   double a2[]=b.property("tp",(tg[sn]+0.1),P);
   double h2=a2[3]; 
   double Cp=(h2-h1)/0.1;   
   double t2=tamW[i]/m[0][i]/Cp+tg[sn];
   tm[0][i]=t2;
   
   double a3[]=b.property("tp",tg[sn],P); 
   h1=a3[3];   
   double a4[]=b.property("tp",(tg[sn]+0.1),P);
   h2=a4[3]; 
   Cp=(h2-h1)/0.1;   
   t2=tamW[i]/m[1][i]/Cp+tg[sn];
   tm[1][i]=t2;   
   double a5[]=b.property("tp",tg[sn],P); 
   h1=a5[3];   
   double a6[]=b.property("tp",(tg[sn]+0.1),P);
   h2=a6[3]; 
   Cp=(h2-h1)/0.1;   
   t2=tamW[i]/m[2][i]/Cp+tg[sn];
   tm[2][i]=t2;   
   double a7[]=b.property("tp",tg[sn],P); 
   h1=a7[3];   
   double a8[]=b.property("tp",(tg[sn]+0.1),P);
   h2=a8[3]; 
   Cp=(h2-h1)/0.1;   
   t2=tamW[i]/m[3][i]/Cp+tg[sn];
   tm[3][i]=t2;   
  }
double bb[][]=new double[14][n];
for(i=0;i<n;i++)
{
bb[0][i]= nominalW[i];
bb[1][i]=tamW[i];
bb[2][i]=m[0][i];
bb[3][i]=m[1][i];
bb[4][i]=m[2][i];
bb[5][i]=m[3][i];
bb[6][i]=tm[0][i];
bb[7][i]=tm[1][i];
bb[8][i]=tm[2][i];
bb[9][i]=tm[3][i];
bb[10][i]=pd[0][i];
bb[11][i]=pd[1][i];
bb[12][i]=pd[2][i];
bb[13][i]=pd[3][i];
}	
//s1="Hesaplanmış Kalibre Değerler1.xls";
//s1=JOptionPane.showInputDialog("çıktı dosya adı(Hesaplanmış Kalibre Değerler1.xls",s1);

jxl.write.Number number;

jxl.write.Label l;
  l=new Label(0,1,"isim");
  l.setCellFormat(wcf);
  wsheet.addCell(l);
  l=new Label(1,1,rn[sn]);
  l.setCellFormat(wcf);
  wsheet.addCell(l);
  l=new Label(0,2,"t su giriş");
  l.setCellFormat(wcf);
  wsheet.addCell(l);
  number=new jxl.write.Number(1,2,tg[sn]);
  number.setCellFormat(wcf);
  wsheet.addCell(number);
  l=new Label(0,3,stk[sn]);
  l.setCellFormat(wcf);
  wsheet.addCell(l);
  number=new jxl.write.Number(1,3,tk[sn]);
  number.setCellFormat(wcf);
  wsheet.addCell(number);
    
  l=new Label(3,9,"su debisi");
  l.setCellFormat(wcf);
  wsheet.addCell(l);
  l=new Label(7,9,"su çıkış sıcaklığı");
  l.setCellFormat(wcf);
  wsheet.addCell(l);
  l=new Label(11,9,"su basınç düşümü");
  l.setCellFormat(wcf);
  wsheet.addCell(l);
  for(j=0;j<15;j++)
  {
  l=new Label(j,10,baslik[j]);
    l.setCellFormat(wcf);
    wsheet.addCell(l);
  }
  
  for(i=0;i<n;i++)
  {
	l=new Label(0,i+11,isim[i]);
	  l.setCellFormat(wcf);
    wsheet.addCell(l);  	  
  for(j=0;j<14;j++)
  {  	  
	  number = new jxl.write.Number(j+1,i+11,bb[j][i]);
      number.setCellFormat(wcf);
	  wsheet.addCell(number);
  }
  }  

Text.print(Text.T(bb),baslik1,rn[sn]);	
}
workbook1.write();
workbook1.close(); 
}catch(jxl.write.WriteException we) {System.out.println("write exception");}
}catch(jxl.read.biff.BiffException be) {System.err.println("BiffException");}  
}

}