Page 1 of 1

get WorksheetPart from WorkbookPart.Sheets.sheet

PostPosted: Sat Oct 25, 2014 3:22 am
by fachhoch
I am trying to parse xlsx file , this file has lot of sheets , using SpreadsheetMLPackage.load.getWorkbookPart () I can get WorkbookPart, this has List of sheets, I want to get WorksheetPart for each Sheet to get the sheetdata.Please advice me how.


Code: Select all
SpreadsheetMLPackage xlsxPkg = SpreadsheetMLPackage.load(new java.io.File(inputfilepath));      
      
      
      WorkbookPart workbookPart= xlsxPkg.getWorkbookPart();
      workbookPart.getContents().getSheets().getSheet().forEach((Sheet  sheet)->{
         System.out.println(sheet.getName()+" "+sheet.getId());
      });

Re: get WorksheetPart from WorkbookPart.Sheets.sheet

PostPosted: Sat Oct 25, 2014 7:31 am
by jason
WorkbookPart contains:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public WorksheetPart getWorksheet(int index) throws Xlsx4jException {
               
                List<Sheet> sheets;
                try {
                        sheets = this.getContents().getSheets().getSheet();
                } catch (Docx4JException e1) {
                        throw new Xlsx4jException(e1.getMessage(), e1);
                }

                int zeroBasedCount = sheets.size() -1;

                if (index< 0 || index>zeroBasedCount) {
                        throw new Xlsx4jException("No sheet at index " + index + ".  (There are " + sheets.size() + " sheets) ");                      
                }

                try {
                        Sheet s = sheets.get(index);
                        return (WorksheetPart)this.getRelationshipsPart().getPart(s.getId());
                } catch (Exception e) {
                        throw new Xlsx4jException("Sheet " + index + " not found", e);
                }
               
        }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4