0

In the following code i want to pass two data in two different cells. (Cell 1 and Cell 2).But its only showing data for cell 1.Someone please help.

import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class xl {
private WebDriver driver;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void test() throws Exception {
driver.get("http://www.google.com/");
String s = driver.getTitle();
writereport(0,0,s);
writereport(1,1,"Valid");
}

@After
public void tearDown() throws Exception {
driver.quit();
}



public void writereport(int a,int b,String text) 
   { 
    try
    {
   FileOutputStream f = new FileOutputStream("C:\\DEMO.xls",true);
   WritableWorkbook book = Workbook.createWorkbook(f); 
   WritableSheet sheet = book.createSheet("TESTRESULTS",0);
   Label i = new Label(a, b, text);
   sheet.addCell(i);
   book.write(); 
   book.close(); 
    }
    catch (Exception e)
    {
     e.printStackTrace();
    }
   }}

I have tried Label i = new Label(a, b, text); sheet.addCell(i); Label P = new Label(a, b, text); sheet.addCell(p); But No luck

2 Answers 2

1

The second time you call writereport, you are trying to create a workbook that already exists. This fails.

To write to an existing workbook with jxl, you need to make a copy of that workbook first.

    public void writereport(int a, int b, String text) {
    try {
        File excelFile = new File("D:\\DEMO.xls");

        WritableWorkbook book;
        WritableSheet sheet;
        Workbook existingBook = null;

        if (!excelFile.exists()) {
            book = Workbook.createWorkbook(excelFile);
            sheet = book.createSheet("TESTRESULTS", 0);
        } else {
            existingBook = Workbook.getWorkbook(excelFile);
            book = Workbook.createWorkbook(excelFile, existingBook);
            sheet = book.getSheet("TESTRESULTS");
        }

        Label i = new Label(a, b, text);
        sheet.addCell(i);
        book.write();
        book.close();

        if (existingBook != null)
            existingBook.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

(This is not a selenium issue by the way).

Sign up to request clarification or add additional context in comments.

Comments

0

Write result in Existing excel sheet using selenium webdriver. User this method in your code method. like

writeresultinexcel(3, i, "Pass"); 

or

writeresultinexcel(3, i, "Fail");



public void writeresultinexcel(int row, int col, String result) throws Exception{

try {

File fl = new File("excelpath");

WritableWorkbook wwb;

WritableSheet wsht;

Workbook existingbook=null;

if(!fl.exists()){

wwb=Workbook.createWorkbook(fl);

wsht=wwb.getSheet("excelpath");

}

else {

existingbook=Workbook.getWorkbook(fl);

wwb=Workbook.createWorkbook(fl,existingbook);

wsht=wwb.getSheet(0);

}

Label lbl=new Label(row, col, result);

wsht.addCell(lbl);

wwb.write();

wwb.close();

if(existingbook!=null){

existingbook.close();

}

} 

catch (Exception e) {

        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.