Low Orbit Flux Logo 2 F

Java How To Read Xlsx File

One great way to read an xlsx file in Java is to use the Apache POI library.

We haven’t exactly done a ton of testing with this yet but here is an example that is hopefully in working order.


import java.io.File;  
import java.io.FileInputStream;  
import java.util.Iterator;  
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.Row;  
import org.apache.poi.xssf.usermodel.XSSFSheet;  
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  

public class XLSXReaderExample {  
    public static void main(String[] args) {  
        try {  
            FileInputStream f1 = new FileInputStream(new File("Z:\\projects\\inventory.xlsx")); 
            XSSFSheet sheet = new XSSFWorkbook(f1).getSheetAt(0);    

            Iterator i = sheet.iterator();     
            while (i.hasNext()) {  
                Row row = i.next();  
                Iterator c = row.cellIterator();   
                while (c.hasNext()) {  
                     Cell cell = c.next();  
                     ct = cell.getCellType();
                      if( ct == case Cell.CELL_TYPE_STRING ) {
                                 System.out.print(cell.getStringCellValue() + "    " );  
                      } else if( ct == Cell.CELL_TYPE_NUMERIC ) {
                                 System.out.print(cell.getNumericCellValue() + "    " );  
                      }
                 }  
                 System.out.println("");  
             }  
         }  
         catch(Exception e) {  
             System.out.println(e);  
          }  
      }  
} 
</code></pre>