Link to home
Start Free TrialLog in
Avatar of redds1
redds1

asked on

How to use POI with Excel 2007 in core java?

My code works fine with Excel 2003.

fs =  new POIFSFileSystem(excelFile);        
 wb = new HSSFWorkbook(fs);

        String cleanSheetName;
        int numsheets = wb.getNumberOfSheets();
        boolean proceed;        
             sheet = wb.getSheetAt("testr");
                      Iterator iter = sheet.rowIterator();
            while(iter.hasNext()){
                proceed = true;
                HSSFRow row = (HSSFRow)iter.next();
                try{
                    boolean cell1,cell2;
                    cell1 = cell2 = true;
                    proceed = cell1 || cell2;
                }catch(Exception ex){
                    proceed = false;
                }
                if(proceed){
                    StringBuffer sb = new StringBuffer();
                    for(int c=0;c< 1;c++){
                        HSSFCell cell = row.getCell((short)c);
                        try{
                            String tp = str.cleanString(cell.getStringCellValue());

                            sb.append(tp);
                                                          sb.append(",");
                                                   }catch(NullPointerException ex){
                                     }catch(Exception ex){
                            sb.append(myFormatter.format(cell.getNumericCellValue()));
                            sb.append(",");
                            //System.out.println("Numeric cell "+cleanSheetName+" row: "+(row.getRowNum()+1)+" col: "+c+" value: "+cell.getNumericCellValue());
                        }
                    }
                    writer.write(sb.append("\n").toString());
                }
         }
But I want to read an xlsx file. My POI version is 3.7, it seems to have support for xssf but I can't get access to the xssf api:  import org.apache.poi.xssf. Can you help please?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>but I can't get access to the xssf api:  import org.apache.poi.xssf.

In that case you have the wrong version in your classpath. Make sure you get rid of all versions that don't support 2007
Avatar of redds1
redds1

ASKER

I had 3.6 and I changed it to 3.7. All these versions supposedly support xssf but I can't access it.
You're saying you still can't compile when you import that package org.apache.poi.xssf ?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of redds1

ASKER

thanks, I can compile now after adding this jar to my classpath.Still, it does not work, can you take a look at the code, thanks

    public TsExcelImportPtbi(InputStream excelFile){
        this.excelFile = excelFile;
    }

    public void write(OutputStream out) throws IOException{
        OutputStreamWriter writer = new OutputStreamWriter(out);
        POIFSFileSystem fs = null;
        XSSFWorkbook wb = null;
        XSSFSheet sheet = null;

        DecimalFormat myFormatter = new DecimalFormat("0.#########");

        fs =  new POIFSFileSystem(excelFile);

        wb = new XSSFWorkbook(excelFile);

        String cleanSheetName;
        int numsheets = wb.getNumberOfSheets();
        boolean proceed;
                    sheet = wb.getSheetAt(0);
                       Iterator iter = sheet.rowIterator();
            while(iter.hasNext()){
                proceed = true;
                XSSFRow row = (XSSFRow)iter.next();
                try{
                    boolean cell1,cell2;
                    cell1 = cell2 = true;
                    proceed = cell1 || cell2;
                }catch(Exception ex){
                    proceed = false;
                }
                if(proceed){
                    StringBuffer sb = new StringBuffer();
                    for(int c=0;c< 3;c++){
                        XSSFCell cell = row.getCell((short)c);
                        try{
                            String tp = str.cleanString(cell.getStringCellValue());

                            sb.append(tp);
                                                       sb.append(",");
                           
                        }catch(NullPointerException ex){
                                                  }catch(Exception ex){
                            sb.append(myFormatter.format(cell.getNumericCellValue()));
                            sb.append(",");
                           
                        }
                    }
                    writer.write(sb.append("\n").toString());
                }
           // }
        }

        writer.flush();
        writer.close();
    }
Avatar of redds1

ASKER

I am getting: java.lang.NoClassDefFoundError
on this line: wb = new XSSFWorkbook(excelFile);
Avatar of redds1

ASKER

I have added following jars 1)poi-3.6-20091214.jar 2)poi-contrib-3.6-20091214.jar 3)poi-examples-3.6-20091214.jar 4)poi-ooxml-3.6-20091214.jar 5)poi-ooxml-schemas-3.6-20091214.jar 6)poi-scratchpad-3.6-20091214.jar, still it's giving java.lang.NoClassDefFoundError when I try to import the file
Avatar of redds1

ASKER

it works now, few pre-requisite jar files were missing
You've probably got a few unnecessary ones now in the classpath - try removing them one by one