Page 1 of 1

Parse xlsx file

PostPosted: Fri Feb 04, 2011 4:41 am
by davide_vanoni
Hi everybody! I'm a docx4j library newbie and I need to parse an xlsx file. I'm looking for some example in order to do it. I've already read these examples http://dev.plutext.org/trac/docx4j/brow ... 4j/samples but I haven't found what I need.
I've got an xlsx file (of course!). This file has 4 columns and some rows. What I have to do is
1) load the file
2) print the data found in each cell
This is my code:
Code: Select all
public class test {

   /**
    * @param args
    * @throws Docx4JException
    */
   public static void main(String[] args){
      // TODO Auto-generated method stub
      
      try {
         SpreadsheetMLPackage  spread = SpreadsheetMLPackage.load(new java.io.File("path\file.xlsx"));
         
      } catch (Docx4JException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
   }

}

I'm able to load the file but I don't know how to continue. Thanks for your help!
David

Re: Parse xlsx file

PostPosted: Sat Feb 05, 2011 7:35 pm
by jason
I've committed a new version of the PartsList sample http://dev.plutext.org/trac/docx4j/changeset/1418, which also prints the cell content of each worksheet. The guts of it:
Code: Select all
      for(WorksheetPart sheet: worksheets) {
         System.out.println(sheet.getPartName().getName() );
         Worksheet ws = sheet.getJaxbElement();
         SheetData data = ws.getSheetData();
         for (Row r : data.getRow() ) {
            System.out.println("row " + r.getR() );            
            for (Cell c : r.getC() ) {
               if (c.getT().equals(STCellType.S)) {
                  System.out.println( "  " + c.getR() + " contains " +
                        sharedStrings.getJaxbElement().getSi().get(Integer.parseInt(c.getV())).getT()
                              );
               } else {
                  // TODO: handle other cell types
                  System.out.println( "  " + c.getR() + " contains " + c.getV() );
               }
            }
         }
      }


If you flesh this out any further, it would be great if you could contribute your efforts.

cheers .. Jason