java - Reading Column values from Excel as string return as decimal? -
i have requirement read excel file particular column values.
i unable read cell values are.
my code
import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.openxml4j.exceptions.invalidformatexception; import org.apache.poi.ss.usermodel.cell; import org.apache.poi.ss.usermodel.row; import org.apache.poi.ss.usermodel.sheet; import org.apache.poi.ss.usermodel.workbookfactory; import org.apache.poi.ss.usermodel.workbook; public class excelsheetcolumn { public static void main(string[] args) throws invalidformatexception, filenotfoundexception, ioexception { workbook wb = workbookfactory.create(new fileinputstream("c:/users/pradeep.halcyontekdc/desktop/my data/excelfiles/xls1.xls")); sheet sheet = wb.getsheet("retail - all"); row row=null; cell cell ; (int j=0; j< sheet.getlastrownum()+1; j++) { row = sheet.getrow(j); cell = row.getcell(0); if(cell!=null) { int type = cell.getcelltype(); if (type == hssfcell.cell_type_string) system.out.println(cell.getrichstringcellvalue().tostring()); else if (type == hssfcell.cell_type_numeric) system.out.println(cell.getnumericcellvalue()); else if (type == hssfcell.cell_type_boolean) system.out.println( cell.getbooleancellvalue()); else if (type == hssfcell.cell_type_blank) system.out.println(cell.getcolumnindex() + "] = blank cell"); } } } }
excel column values are
79 999861 999861 https://staging.adpcreditsolutions.com /index/contract_decision 9200278 2011/01/17 79 5032944200 il4-pcc@test.com 1979/12/31 0.00 0.00
but output getting is
79.0 999861.0 999861.0 https://staging.adpcreditsolutions.com /index/contract_decision 9200278.0 17-jan-2011 79.0 5.0329442e9 il4-pcc@test.com 31-dec-1979 0.0 0.0
how read code above program read excel column values are? please me!!
if (type == hssfcell.cell_type_numeric) { string stringvalue=""+cell.getnumericcellvalue(); string[] splitvalue=stringvalue.split(".0"); system.out.println(splitvalue[0]); }
system.out.println(cell.tostring());//here problem
tostring() method returns string representation of object
. in general, tostring method returns string "textually represents" object
.
use cell.getstringcellvalue()
instead
cell.tostring()
and propor usage needed.
for numeric values have use
getnumericcellvalue() , put condition there
if(cell!=null) { int type = cell.getcelltype(); if (type == hssfcell.cell_type_string) system.out.println(cell.getrichstringcellvalue().tostring()); else if (type == hssfcell.cell_type_numeric) string[] splits = string.valueof(cell.getnumericcellvalue()).split("."); system.out.println(splits[0]); else if (type == hssfcell.cell_type_boolean) system.out.println( cell.getbooleancellvalue()); else if (type == hssfcell.cell_type_blank) system.out.println(cell.getcolumnindex() + "] = blank cell"); }
Comments
Post a Comment