java - How to update cell reference values using Apache POI -
i using apache poi create new xssfworkbook existing one, after updating values. suppose have 2 worksheets (lets say: worksheet & b) in existing workbook. worksheet b has cell reference worksheet a. if modify cell values of worksheet , save them new workbook, corresponding cell values of worksheet b should updated too. doesn't. how can update them programmatically? . thank you.
my code:
public void createexcel(clientdata cd) throws filenotfoundexception, ioexception, invalidformatexception{ // create new file double[] dataholder1= cd.getfinaldata1(), param1 = cd.getrecord1param(); double[] dataholder2 = cd.getfinaldata2(), param2 = cd.getrecord2param(); double[] ncv = cd.getncv(); string[] pname = cd.getname(); workbook workbook = new xssfworkbook(opcpackage.open(new fileinputstream("template/mncv.xlsx"))); // or sample.xls //creationhelper createhelper = workbook.getcreationhelper(); sheet s=workbook.getsheetat(0); int counter = dataholder1.length + param1.length +param2.length+dataholder2.length;//+ param1.length + param2.length; // r = s.getrow(0); // r.getcell(0).setcellvalue("param1"); // r.getcell(1).setcellvalue("record1"); // r.getcell(2).setcellvalue("param2"); // r.getcell(3).setcellvalue("record2"); int i; for(i=0;i<counter;i++){ if(i<param1.length){ for(int j=0;j<param1.length;j++){ r = s.getrow(i); r.getcell(0).setcellvalue(param1[j]); i++; } }else if(i<dataholder1.length+param1.length && i>=param1.length){ for(int j=0;j<dataholder1.length;j++){ r = s.getrow(i); r.getcell(0).setcellvalue(dataholder1[j]); i++; } }else if(i<dataholder1.length+param1.length+param2.length && i>=dataholder1.length+param1.length){ for(int j=0;j<param2.length;j++){ r = s.getrow(i); r.getcell(0).setcellvalue(param2[j]); i++; } }else{ for(int j=0;j<dataholder2.length;j++){ r = s.getrow(i); r.getcell(0).setcellvalue(dataholder2[j]); i++; } } // if(i<=param1.length){ // r.getcell(0).setcellvalue(param1[i-1]); // r.getcell(2).setcellvalue(param2[i-1]); // // } // r.getcell(0).setcellvalue(param1[i]); //r.getcell(3).setcellvalue(dataholder2[i-1]); i--; } for(int k=0;k<ncv.length;k++){ r = s.getrow(i); r.getcell(0).setcellvalue(ncv[k]); i++; } s = workbook.getsheetat(1); s.getrow(2).getcell(5).setcellvalue(pname[0]+" "+pname[1]+" "+pname[2]); s.getrow(3).getcell(5).setcellvalue(cd.getage()); s.getrow(4).getcell(5).setcellvalue(cd.getgender()); try (fileoutputstream out = new fileoutputstream("workbook.xlsx")) { //workbookevaluator = new workbookevaluator(workbook); workbook.write(out); out.close(); xssfformulaevaluator.evaluateallformulacells((xssfworkbook) workbook); }catch(exception e){ system.out.println(e); }
the excel file format caches result of formula evaluation, make opening file quicker. means when you're done making changes file, you'll need evaluate of formula cells updated cached value. (otherwise, when load file in excel, cases it'll still show old value until go cell)
luckily, apache poi provides code that, see formula evaluation documentation details. (you can choose recalculate formulas, if know cells have changed, or everything)
Comments
Post a Comment