Java : Membaca Data dari File Excel

Last Updated on 13 years by Mas Herdi

Ini adalah kode program untuk membaca data yang ada di dalam file Excel, sayangnya sampai saat ini library yang saya pakai belum mendukung file Excel tahun 2007 ke atas.

Oke, dalam test ini, kira-kira begini layout data yang ada pada file Excel.

File Excel Data Pelanggan

Let’s see, nanti sifat program ini adalah statis, karena itu output yang dikeluarkan akan berubah2 tergantung layout data yang ada pada file Excel. Library JExcelAPI sudah memiliki fungsi untuk membedakan apakah itu String/Label, ataukah Integer/Number. Namun dia tidak bisa membedakan antara Table Header, dan Tabel Content. Maka dari itu kita sendirilah yang harus mengantisipasi dan merubahnya di codingan.

File ReadExcel.java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package readexcel;

/**
 *
 *@authorHerdi Naufal
 */

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcel {

    private String fileInput;

    public void setInputFile(String fileInputX) {
        fileInput = fileInputX;
    }

    public void ngeBaca() throws IOException  {
        File fileExcel = new File(fileInput);
        Workbook w;
        try {
            w = Workbook.getWorkbook(fileExcel);

            // Ambil sheet pertama, nomer 0 menandakan sheet ke 1
            Sheet sheet = w.getSheet(0);

            // Looping sebanyak kolom dan baris yang ada
            for (int j = 0; j < sheet.getColumns(); j++) {
                for (int i = 0; i < sheet.getRows(); i++) {
                    Cell cell = sheet.getCell(j, i);

                                        //Jika baris adalah TABEL HEADER
                                        if((i==0 && j==0) ||(i==0 && j==1))
                                        {
                                            System.out.println("Isi tabel "
                                + cell.getContents());
                                        }
                                        else
                                        {
                    if (cell.getType() == CellType.LABEL) {
                        System.out.println("Ini adalah Label "
                                + cell.getContents());
                    }

                    if (cell.getType() == CellType.NUMBER) {
                        System.out.println("Ini adalah Nomor "
                                + cell.getContents());
                    }
                                        }

                }
            }
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        ReadExcel test = new ReadExcel();
        test.setInputFile("D:/HafizhUIDver2/NetBeansProject/ReadExcel/src/readexcel/pelanggan.xls");
        test.ngeBaca();
    }

}

Dan inilah output dari program di atas :

Useful post :





Download aplikasi kami di Google Play Store


Tutorial Menarik Lainnya :

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

TWOH&Co.