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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
CEHJCommented:
You're saying you still can't compile when you import that package org.apache.poi.xssf ?
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.