java - Unable to update the required cell values in the excel using apache poi? -
below program trying update birds column value(ex:duck) not updated after "parrot" value.i want output below.can 1 me resolve this. program: package config;
import java.io.fileinputstream; import java.io.fileoutputstream; 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; import org.testng.annotations.test; public class writing { @test public void test() throws exception { fileinputstream f1=new fileinputstream("d://xl4.xlsx"); xssfworkbook w1=new xssfworkbook(f1); xssfsheet s1=w1.getsheetat(0); xssfcell c = null; xssfrow r = null; int rows=s1.getphysicalnumberofrows(); for(int i=0;i<=rows;i++) { r = s1.getrow(i); if(r == null) { s1.createrow((short) (i)).createcell(1).setcellvalue("duck"); } } fileoutputstream f2=new fileoutputstream("d://xl4.xlsx"); w1.write(f2); f2.close(); } } **actual result:** animals birds elephant parrot lion tiger deer cow duck **expected result:** animals birds elephant parrot lion duck tiger deer cow [output][1] [1]: http://i.stack.imgur.com/xzttz.png
if(r == null)
is culprit here. when in 3rd row r = s1.getrow(i) not null. so, call not come inside condition block.
change condition row not null , second cell null - create new cell , assign value "duck" here.
Comments
Post a Comment