java - Can't read Excel 2010 file with Apache POI. First Row number is -1 -


i trying testfile apache poi api (current version 3-10-final). following test code

import java.io.fileinputstream; import org.apache.poi.xssf.usermodel.xssfsheet; import org.apache.poi.xssf.usermodel.xssfworkbook;  public class exceltest {      public static void main(string[] args) throws exception {         string filename = "testfile.xlsx";         xssfworkbook wb = new xssfworkbook(new fileinputstream(filename));         xssfsheet sheet = wb.getsheetat(0);         system.out.println(sheet.getfirstrownum());     } } 

results in first row number -1 (and existing rows come null). test file created excel 2010 (i have no control on part) , can read excel without warnings or problems. if open , save file version of excel (2013) can read expected.

any hints why can't read original file or how can highly appreciated.

the testfile.xlsx created "spreadsheetgear 7.1.1.120". open xlsx file software can deal zip archives , /xl/workbook.xml see that. in worksheets/sheet?.xml files notice row elements without row numbers. if put row number in first row-tag <row r="1"> apache poi can read row.

if comes question, blame this, answer both apache poi , spreadsheetgear ;-). apache poi because attribute r in row element optional. spreadsheetgear because there no reason not use r attribute if excel ever.

if cannot testfile.xlsx in format can apache poi read directly, must work underlying objects. following works testfile.xlsx:

import org.apache.poi.xssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.*; import org.apache.poi.openxml4j.exceptions.invalidformatexception;  import java.io.filenotfoundexception; import java.io.ioexception; import java.io.fileinputstream; import java.io.inputstream;  import org.openxmlformats.schemas.spreadsheetml.x2006.main.ctworksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.ctsheetdata; import org.openxmlformats.schemas.spreadsheetml.x2006.main.ctrow;  import java.util.list;  class testfile {   public static void main(string[] args) {   try {     inputstream inp = new fileinputstream("testfile.xlsx");    workbook wb = workbookfactory.create(inp);     sheet sheet = wb.getsheetat(0);     system.out.println(sheet.getfirstrownum());     ctworksheet ctworksheet = ((xssfsheet)sheet).getctworksheet();     ctsheetdata ctsheetdata = ctworksheet.getsheetdata();     list<ctrow> ctrowlist = ctsheetdata.getrowlist();     row row = null;    cell[] cell = new cell[2];     (ctrow ctrow : ctrowlist) {     row = new myrow(ctrow, (xssfsheet)sheet);     cell[0] = row.getcell(0);     cell[1] = row.getcell(1);     if (cell[0] != null && cell[1] != null && cell[0].tostring() != "" && cell[1].tostring() != "")         system.out.println(cell[0].tostring()+"\t"+cell[1].tostring());    }    } catch (invalidformatexception ifex) {   } catch (filenotfoundexception fnfex) {   } catch (ioexception ioex) {   }  } }  class myrow extends xssfrow {  myrow(org.openxmlformats.schemas.spreadsheetml.x2006.main.ctrow row, xssfsheet sheet) {   super(row, sheet);  } } 

i have used org.openxmlformats.schemas.spreadsheetml.x2006.main.ctworksheet, org.openxmlformats.schemas.spreadsheetml.x2006.main.ctsheetdata, org.openxmlformats.schemas.spreadsheetml.x2006.main.ctrow. part of apache poi binary distribution poi-bin-3.10.1-20140818 , there within poi-ooxml-schemas-3.10.1-20140818.jar

for documentation see http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/

and have extend xssfrow, because can't use xssfrow constructor directly since has protected access.


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -