selenium - write to a excel file -
public class excellibrary{ /** * @param args */ //method accepts sheet name, row number , cell number // , returns string value present in cell public string getexceldata(string sheetname,int rownum, int cellnum){ string retval=null; try { fileinputstream fis = new fileinputstream("d:\\seleniumbsw9\\data.xlsx"); //get workbook object workbook wb = workbookfactory.create(fis); //get sheet [a particular sheet in workbook] sheet s = wb.getsheet(sheetname); //get row [a particular row in sheet] row r = s.getrow(rownum); //get cell [a particular cell of row obtained above] cell c = r.getcell(cellnum); //get string cell value cell retval = c.getstringcellvalue(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (invalidformatexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return retval; } public int getrowcount(string sheetname){ int lastrow=0; try { fileinputstream fis = new fileinputstream("d:\\seleniumbsw9\\data.xlsx"); //get workbook object workbook wb = workbookfactory.create(fis); //get sheet [a particular sheet in workbook] sheet s = wb.getsheet(sheetname); //return last row in data present counted form 0 lastrow = s.getlastrownum(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (invalidformatexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return lastrow; } public void writedatatoexcel(string sheetname, int rownum, int cellnum, string data){ try { fileinputstream fis = new fileinputstream("d:\\seleniumbsw9\\data.xlsx"); //get workbook object workbook wb = workbookfactory.create(fis); //get sheet [a particular sheet in workbook] sheet s = wb.getsheet(sheetname); //get row data needs written. step //assumes writing cell in existing row row r = s.getrow(rownum); //create cell in row. if trying write //a cell has not been created, throw error. first //we need create cell, set value of cell //this step not needed if writing existing cell cell c = r.createcell(cellnum); c.setcellvalue(data); fileoutputstream fos=new fileoutputstream("d:\\seleniumbsw9\\data.xlsx"); wb.write(fos); fos.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (invalidformatexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } }
}
to used rows in excel file using jxl.....
package testpackage; import java.io.fileinputstream; import java.io.ioexception; import jxl.workbook; import jxl.sheet; import jxl.read.biff.biffexception; public class rowcount { public static void main(string[] args) throws biffexception, ioexception { fileinputstream exo=new fileinputstream("e:\\test1.xls"); workbook wbo=workbook.getworkbook(exo); sheet wso=wbo.getsheet(0); int lastrownum; lastrownum= wso.getrows(); system.out.println(lastrownum); } }
Comments
Post a Comment