java - Excel to String xls/xlsx different result -
want compare excel files each other see if same or not. can choose excel files , read them. have 2 excel sheets same content 1 in .xls , on in .xlsx format. use following code read files (for xls hssfworkbook , on)
private string xlsx(file inputfile) { string outputstring = ""; // storing data string stringbuffer data = new stringbuffer(); try { // workbook object xlsx file xssfworkbook wbook = new xssfworkbook(new fileinputstream(inputfile)); // first sheet workbook xssfsheet sheet = wbook.getsheetat(0); row row; cell cell; // iterate through each rows first sheet iterator<row> rowiterator = sheet.iterator(); while (rowiterator.hasnext()) { row = rowiterator.next(); // each row, iterate through each columns iterator<cell> celliterator = row.celliterator(); while (celliterator.hasnext()) { cell = celliterator.next(); data.append(cell + ";"); } data.append("\n"); } system.out.println(data.tostring()); outputstring = data.tostring(); wbook.close(); } catch (exception ioe) { ioe.printstacktrace(); } return outputstring; }
in excel have blank cells - when read them xls data;;;;;data
correct when same in xlsx data;data
somehow code skips empty cells?! how can fix problem? in advance
after more google research , trying different things found solution problem. iterator skips empty cells because have no value - null - in xls file seems not null - whatever
my code:
private string xlsx(file inputfile) { string outputstring = ""; system.out.println("start"); // storing data string stringbuffer data = new stringbuffer(); try { // workbook object xlsx file xssfworkbook wbook = new xssfworkbook(new fileinputstream(inputfile)); // first sheet workbook xssfsheet sheet = wbook.getsheetat(0); // decide rows process int rowstart = 0; int rowend = sheet.getlastrownum()+1; (int rownum = rowstart; rownum < rowend; rownum++) { row r = sheet.getrow(rownum); int lastcolumn = r.getlastcellnum(); (int cn = 0; cn < lastcolumn; cn++) { cell c = r.getcell(cn); if (c == null) { data.append("" + ";"); } else { data.append(c + ";"); } } data.append("\n"); } system.out.println(data.tostring()); outputstring = data.tostring(); wbook.close(); } catch (exception ioe) { ioe.printstacktrace(); } system.out.println("end"); return outputstring; }
Comments
Post a Comment