java - POI: output blank if formula returns blank -


i have formula in excel:

=if(a1="foo";"";"0") 

if formula returns blank value want no value in resultiong csv file created poi. if formula returns 0 want 0 in csv file.

this part of code (it's question of how stripping code down):

iterator<row> rowiterator = sheet.rowiterator(); while (rowiterator.hasnext()) {      row row = rowiterator.next();     iterator<cell> celliterator = row.celliterator();     boolean isfirst = true;      (int cn = 0; cn < row.getlastcellnum(); cn++) {         cell cell = row.getcell(cn, row.create_null_as_blank);         if (!isfirst) {             buffer.write(delimiter.getbytes(charset));         } else {             isfirst = false;         }          // numeric cell type (0)         // string cell type (1)         // formula cell type (2)         // blank cell type (3)         // boolean cell type (4)         // error cell type (5)         if (cell.getcelltype() == 0 || cell.getcelltype() == 2) {             try {                 if (dateutil.iscelldateformatted(cell)) {                     cell.setcelltype(cell.cell_type_numeric);                     date value = cell.getdatecellvalue();                     simpledateformat sdf = new simpledateformat("dd.mm.yyyy");                     if (cell.getnumericcellvalue() < 1) {                         sdf.applypattern("hh:mm:ss");                     }                     buffer.write(sdf.format(value).getbytes(charset));                 } else {                     double valuedouble = cell.getnumericcellvalue();                     if (valuedouble == math.ceil(valuedouble)) {                         buffer.write(string.format("%d", (long) valuedouble).getbytes(charset));                     } else {                         valuedouble = round(valuedouble, roundingplaces);                         string value = string.valueof(valuedouble).replace(".", ",");                         buffer.write(value.getbytes(charset));                     }                 }             } catch (exception e) {                 // formula returns string                 cell.setcelltype(cell.cell_type_string);                 string value = cell.getstringcellvalue();                 buffer.write(value.getbytes(charset));             }         } else {             cell.setcelltype(cell.cell_type_string);             string value = cell.getstringcellvalue();             buffer.write(value.getbytes(charset));         }     }     buffer.write("\r\n".getbytes(charset)); } 

this code results in 0 in csv file in every case. results line

double valuedouble = cell.getnumericcellvalue(); 

the documentation pretty clear point:

double getnumericcellvalue()

get value of cell number.

for strings throw exception. blank cells return 0. formulas or error cells return precalculated value;

how can analyze cell if contains null value?

cell.getcelltype() == cell.cell_type_blank should true blank cell, false otherwise.

edit: forgot have formula cell. in case, augment to: cell.getcelltype() == cell.cell_type_formula ? cell.getcachedformularesulttype() == cell.cell_type_blank : cell.getcelltype() == cell.cell_type_blank

also should not filter blank cells, non-numeric cells.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -