Menggunakan library POI membaca file XLS

Library POI adalah library yang disediakan oleh apache foundation yang salah satunya dapat dipergunakan untuk membaca file format excel. library POI dapat di download di sini poi.apache.org

Berikut ini cara penggunaan library POI

try {

    FileInputStream file = new FileInputStream(new File("C:\\nama_file.xls"));

    //Buat objek workbook instan dari file XLS
    HSSFWorkbook workbook = new HSSFWorkbook(file);

    //Ambil data dari sheet pertama
    HSSFSheet sheet = workbook.getSheetAt(0);

    //Iterasi data baris per baris 

    Iterator<Row> rowIterator = sheet.iterator();
    while(rowIterator.hasNext()) {
        Row row = rowIterator.next();

        // Ambil data dari setiap cell pada baris dan kolom
        Iterator<Cell> cellIterator = row.cellIterator();
        while(cellIterator.hasNext()) {

            Cell cell = cellIterator.next();

            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    break;
            }
        }
        System.out.println("");
    }
    file.close();
    FileOutputStream out =
        new FileOutputStream(new File("C:\\nama_file.xls"));
    workbook.write(out);
    out.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Post a Response