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?
redds1Asked:
Who is Participating?
 
CEHJCommented:
Make sure you add poi-ooxml-3.7-20101029.jar to the classpath. Of course org.apache.poi.xssf is not a package in itself. You'd be importing stuff like


import org.apache.poi.xssf.model.*;
0
 
CEHJCommented:
>>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
0
 
redds1Author Commented:
I had 3.6 and I changed it to 3.7. All these versions supposedly support xssf but I can't access it.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
CEHJCommented:
You're saying you still can't compile when you import that package org.apache.poi.xssf ?
0
 
redds1Author Commented:
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();
    }
0
 
CEHJCommented:
0
 
redds1Author Commented:
I am getting: java.lang.NoClassDefFoundError
on this line: wb = new XSSFWorkbook(excelFile);
0
 
redds1Author Commented:
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
0
 
redds1Author Commented:
it works now, few pre-requisite jar files were missing
0
 
CEHJCommented:
You've probably got a few unnecessary ones now in the classpath - try removing them one by one
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.