apache poi cellIterator skips blank cells but not in first row -
i creating java programme read excel sheet , create comma separated file. when run sample excel file, blank columns, first row works perfectly, rest of rows skip blank cells. have read code changes required insert blank cells rows, question why first row work ????
public arraylist openandreadexcel(){ fileinputstream file = null; hssfworkbook workbook = null; arraylist <string> rows = new arraylist(); //open file seek { file = new fileinputstream(new file("fruity.xls")); } grab (filenotfoundexception e) { // todo auto-generated grab block system.out.println("could not open input file"); e.printstacktrace(); } // open input stream workbook seek { workbook = new hssfworkbook(file); } grab (ioexception e) { // todo auto-generated grab block system.out.println("can't open hssf workbook"); e.printstacktrace(); } // sheet hssfsheet sheet = workbook.getsheetat(0); // add together iterator every row , column iterator<row> rowiter = sheet.rowiterator(); while (rowiter.hasnext()) { string rowholder = ""; hssfrow row = (hssfrow) rowiter.next(); iterator<cell> celliter = row.celliterator(); boolean first =true; while ( celliter.hasnext()) { if (!first) rowholder = rowholder + ","; hssfcell cell = (hssfcell) celliter.next(); rowholder = rowholder + cell.tostring() ; first = false; } rows.add(rowholder); } homecoming rows; } public void writeoutput(arraylist<string> rows) { // todo auto-generated method stub printstream outfile ; seek { outfile = new printstream("fruity.txt"); for(string row : rows) { outfile.println(row); } outfile.close(); } grab (filenotfoundexception e) { // todo auto-generated grab block e.printstacktrace(); } }
} ----- input in .xls file (sorry don't know how insert excel table here ) name >>>>>>>>>> country of origin >>>>>>>>> state of origin >>>>>>> grade>>>>>> no of months apple >>>>>>>> usa >>>>>>>>>>>>>>>>>>>>>> washington >>>>>>>>>>>>>> >>>>>>>>> 6 orange >>>>>> usa >>>>>>>>>>>>>>>>>>>>>> florida >>>>>>>>>>>>>>>>> >>>>>>>>> 9 pineapple>>>>> usa >>>>>>>>>>>>>>>>>>>>>> hawaii >>>>>>>>>>>>>>>>>> b >>>>>>>>> 10 strawberry>>>> usa >>>>>>>>>>>>>>>>>>>>>> new jersey>>>>>>>>>>>>>> c >>>>>>>>>> 3 my output text file name ,country of origin,state of origin,,,grade,no of months apple,usa,washington,a,6.0 orange,usa,florida,a,9.0 pineapple,usa,hawaii,b,10.0 strawberry,usa,new jersey,c,3.0
notice 2 commas before grade column... because have 2 blank columns there.<br/>
these commas missing in rest of output.
i using apache poi-3.9-20121203.jar
you should have read through iterating on rows , cells documentation on apache poi website.
the celliterator homecoming cells have been defined in file, largely means ones either values or formatting. excel file format sparse, , doesn't bother storing cells have neither values nor formatting.
for case, must have formatting applied first row, causes them show up.
you need read through documentation , switch lookups index. allow total command on how blank vs never used cells handled in code.
apache-poi