.:: Fajar_ridikc identity | | about me Fajar_ridikc ::.

JAVA :: Import Excel Data into JTable

0 :: comments


بِسْــــــــــــــــمِ اﷲِالرَّحْمَنِ اارَّحِيم
bismillaahirrahmaanirrahiim

السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
Assalamu'alaikum warahmatullahi wabarakatuh
{ Silahkan Cari sesuatu terkait Blog Fajar Dibawah ini }

Import Excel Data into JTable





Here in above window, user will see nothing but one button for asking Excel file. Just click on that button and you will see fpllowing:



Here is the jFilechooser, from this you have to choose excel file having extension ".xls".  Otherwise following Error message will be displayed:

Note: If you want to go with MS Office 2007 Excel, you have to customise this code little bit.
Now  after select excel file the data in that Excel file will be shown in JTable as below:

OK For this you have to create one class called  excelTojTable.java 
and paste the below code in it.

And run it....Simple right..?

Note: In this code All the Dynamic Part logic is done in 
              fillData()  method.  So focus on that method...

Note:    To run this example successfully you have to download and add "jxl.jar" file.


import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import jxl.Cell;

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

public class excelTojTable extends JFrame {

 static JTable table;
 static JScrollPane scroll;
 // header is Vector contains table Column
 static Vector headers = new Vector();
 // Model is used to construct JTable
 static DefaultTableModel model = null;
 // data is Vector contains Data from Excel File
 static Vector data = new 
   Vector();
 static JButton jbClick;
 static JFileChooser jChooser;
 static int tableWidth = 0; // set the tableWidth
 static int tableHeight = 0; // set the tableHeight

 public excelTojTable() {
  super("Import Excel To JTable");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JPanel buttonPanel = new JPanel();
  buttonPanel.setBackground(Color.white);
  jChooser = new JFileChooser();
  jbClick = new JButton("Select Excel File");
  buttonPanel.add(jbClick, BorderLayout.CENTER);
  // Show Button Click Event
  jbClick.addActionListener(new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent arg0) {
    jChooser.showOpenDialog(null);
    
    File file = jChooser.getSelectedFile();
    if(!file.getName().endsWith("xls")){
     JOptionPane.showMessageDialog(null, 
       "Please select only Excel file.",
       "Error",JOptionPane.ERROR_MESSAGE);
    }
    else
    {
     fillData(file);
     model = new DefaultTableModel(data, 
       headers);
     tableWidth = model.getColumnCount() 
       * 150;
     tableHeight = model.getRowCount() 
       * 25;
     table.setPreferredSize(new Dimension(
       tableWidth, tableHeight));
 
     table.setModel(model);
    }
   }
  });

  table = new JTable();
  table.setAutoCreateRowSorter(true);
  model = new DefaultTableModel(data, headers);

  table.setModel(model);
  table.setBackground(Color.pink);

  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  table.setEnabled(false);
  table.setRowHeight(25);
  table.setRowMargin(4);

  tableWidth = model.getColumnCount() * 150;
  tableHeight = model.getRowCount() * 25;
  table.setPreferredSize(new Dimension(
    tableWidth, tableHeight));

  scroll = new JScrollPane(table);
  scroll.setBackground(Color.pink);
  scroll.setPreferredSize(new Dimension(300, 300));
  scroll.setHorizontalScrollBarPolicy(
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  scroll.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  getContentPane().add(buttonPanel, 
    BorderLayout.NORTH);
  getContentPane().add(scroll, 
    BorderLayout.CENTER);
  setSize(600, 600);
  setResizable(true);
  setVisible(true);
 }

 /**
  * Fill JTable with Excel file data.
  * 
  * @param file
  * file :contains xls file to display in jTable
  */
 void fillData(File file) {

  Workbook workbook = null;
  try {
   try {
    workbook = Workbook.getWorkbook(file);
   } catch (IOException ex) {
    Logger.getLogger(
      excelTojTable.class.
      getName()).log(Level.SEVERE, 
        null, ex);
   }
   Sheet sheet = workbook.getSheet(0);
   
   headers.clear();
   for (int i = 0; i < sheet.getColumns(); i++) {
    Cell cell1 = sheet.getCell(i, 0);
    headers.add(cell1.getContents());
   }

   data.clear();
   for (int j = 1; j < sheet.getRows(); j++) {
    Vector d = new Vector();
    for (int i = 0; i < sheet.getColumns(); i++) {

     Cell cell = sheet.getCell(i, j);
     
     d.add(cell.getContents());

    }
    d.add("\n");
    data.add(d);
   }
  } catch (BiffException e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {

  new excelTojTable();
 }
}

--------------------------------------------------------------------



Ingin Berlangganan artikel Fajar..? Masukkan Email Dibawah ini kemudian Klik tombol [klik disini] dibawah ini (gratis):

jangan lupa konfirmasi, karna akan ada form verifikasi di email anda.

NB :: Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code, membeli hardware elektronika untuk kepentingan Perusahaan maupun Tugas Akhir (TA/SKRIPSI), Saya siap membantu, Dengan Cara menghubungi saya { fajar_ridikc }, melalui :

Email: Fajarudinsidik@gmail.com
No Hp: 085369237896
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :

ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِين
Alhamdulilah hirobil alamin

وَ السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ
wassalamualaikum warahmatullahi wabarakatuh




{ Silahkan Cari sesuatu Disini }
{ Dibawah ini hanya sebagian artikel, untuk lebih lengkap, cari melalui form diatas}

Fajar_ridikc
Ditulis Oleh : Fajar_ridikc

Artikel JAVA :: Import Excel Data into JTable, Diterbitkan oleh Fajarudin Sidik pada Tuesday, 27 December 2016. Semoga artikel ini dapat menambah wawasan Anda.Sobat diperbolehkan mengcopy paste / menyebar luaskan artikel ini, namun anda harus menyertakan sumbernya. Bila Sobat tertarik Ingin membuat software, membeli software, membeli source code ,membeli hardware elektronika atau ingin kursus program, Saya siap membantu ,Dengan Cara menghubungi saya {fajar_ridikc} Ke Email: Fajarudinsidik@gmail.com, atau No Hp :085369237896