Saturday 14 December 2013

Conversion Html tc to excel

Convert Selenium IDE  HTML TC in to HTML format 


so we can run excel in Keyword driven Framework..

Src Given Below jst Add apache poi jars to ur Eclipse Project.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import javax.script.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExportToExcel {

/**
 * @param args
 * @throws IOException 
 */
int Stepno=0;
String str="";
String temp="";
static String Outputfile="";
List <String> Action=new LinkedList<String>();
List <String> Locator=new LinkedList<String>();
List <String> Value=new LinkedList<String>();
int i=4;
public void Export(String path) throws ScriptException, IOException
{
/*ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");*/
File f=new File(path);
 BufferedReader br = new BufferedReader(new FileReader(f));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append('\n');
            line = br.readLine();
        }
        str += sb.toString();
        
        //str="<td>aman</td>";
        String a[]=str.split("<td>");
        for (i=1;i<a.length;i=i+3)
        {
         temp=a[i].substring(0, a[i].indexOf('<'));
         Action.add(temp);
        } 
        for (i=2;i<a.length;i=i+3)
        {
         temp=a[i].substring(0, a[i].indexOf('<'));
         Locator.add(temp);
        } 
        for (i=3;i<a.length;i=i+3)
        {
         temp=a[i].substring(0, a[i].indexOf('<'));
         Value.add(temp);
        }      
        
      System.out.println("Action List is : "+Action);
      System.out.println("Locator List is : "+Locator);
      System.out.println("Value List is : "+Value);
        
    } finally {
        br.close();
    }  
     }
public void writetoExcel(String excelFileName) throws InvalidFormatException, IOException
{
//String excelFileName = "";//name of excel file
 
String sheetName = "Sheet1";//name of sheet
 
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(sheetName) ;
XSSFRow r1 = sheet.createRow(0);
XSSFCell Srnocell = r1.createCell(0);
Srnocell.setCellValue("Srno");
XSSFCell actioncell = r1.createCell(1);
actioncell.setCellValue("Action");
XSSFCell loccell = r1.createCell(2);
loccell.setCellValue("Locator");
XSSFCell valuecell = r1.createCell(3);
valuecell.setCellValue("Value");
for (int r=1;r <4; r++ )
{
XSSFRow row = sheet.createRow(r);
Stepno++; 
//iterating c number of columns
XSSFCell cell = row.createCell(0);
cell.setCellValue(Stepno);
    XSSFCell cell1 = row.createCell(1);
cell1.setCellValue(Action.get(r-1)); 
System.out.println(Action.get(r-1));
 
    XSSFCell cell2 = row.createCell(2);
cell2.setCellValue(Locator.get(r-1)); 
System.out.println(Locator.get(r-1));
    XSSFCell cell3 = row.createCell(3);
cell3.setCellValue(Value.get(r-1));
System.out.println(Value.get(r-1));
}
 
FileOutputStream fileOut = new FileOutputStream(excelFileName);
 
//write this workbook to an Outputstream.
wb.write(fileOut);
fileOut.flush();
fileOut.close();
System.out.println("Your excel file has been generated!");
JOptionPane.showMessageDialog(null,excelFileName+" is generated successfully");
}
public static void main(String[] args) throws ScriptException, IOException, InvalidFormatException {
// TODO Auto-generated method stub
ExportToExcel e=new ExportToExcel();
JFileChooser chooser=new  JFileChooser();
        int returnVal = chooser.showOpenDialog(null);
        
        if(returnVal == JFileChooser.APPROVE_OPTION) 
        {
        //File f = chooser.getSelectedFile();
        String filename=chooser.getSelectedFile().getPath();
        if (filename.endsWith(".html")){
         Outputfile=filename.replace(".html", ".xlsx");
         e.Export(filename);
e.writetoExcel(Outputfile);
        }
        else
        {
         JOptionPane.showMessageDialog(null, "Pls select only Recorded HTML file");
         //System.exit(0);
        }
        
    }

}}

No comments:

Post a Comment