Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package read;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.util.Iterator;
  8.  
  9. import org.apache.poi.ss.usermodel.Cell;
  10. import org.apache.poi.ss.usermodel.Row;
  11. import org.apache.poi.xssf.usermodel.XSSFSheet;
  12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  13.  
  14. public class BacaExcel {
  15.  
  16.     public static void main(String[] args) {
  17.         System.out.println("=== Baca File Excel ===\n");
  18.        
  19.         try {          
  20.             FileInputStream fiStream = new FileInputStream(new File("D:/mahasiswa.xlsx"));
  21.             XSSFWorkbook xw = new XSSFWorkbook(fiStream);
  22.             XSSFSheet sheet = xw.getSheetAt(0);
  23.             Iterator<Row> rowIter = sheet.iterator();
  24.            
  25.             while(rowIter.hasNext()){
  26.                 Row r = rowIter.next();
  27.                 Iterator<Cell> cellIter = r.cellIterator();            
  28.                 while(cellIter.hasNext()){
  29.                     Cell c = cellIter.next();                  
  30.                     switch(c.getCellType()){
  31.                         case Cell.CELL_TYPE_STRING:
  32.                             System.out.print(c.getStringCellValue()+" ");
  33.                         break;
  34.                         case Cell.CELL_TYPE_NUMERIC:
  35.                         c.setCellType(Cell.CELL_TYPE_STRING);
  36.                             System.out.print(c.getStringCellValue()+" ");
  37.                         break;
  38.                     }                  
  39.                 }
  40.                 System.out.println("");
  41.             }          
  42.             fiStream.close();          
  43.         } catch (FileNotFoundException e) {
  44.             e.printStackTrace();
  45.         } catch (IOException e) {
  46.             e.printStackTrace();
  47.         }
  48.  
  49.     }
  50.  
  51. }