Extreme Component Documentation

How to import Excel file into JTable

May 9,2008

Here are three way to do this.

  1. Use "JTableReadTableModelTask " to do this. ( recommended )
  2. Use "ReadTableModelTask" to do this
  3. Use ModelIO to do this.

The first and second the way will run in the background thread, the third way will run in current thread.

1. Use "JTableReadTableModelTask "to do this. (recommended)

Here is the sample code to import excel file:

JTable jTable = new JTable();
String excelFileName = "excelFileName.xls";
File file = new File(excelFileName );   //‘file’ is the file you want to load.
JProgressBar progressBar = new JProgressBar(); //‘progressBar’ will show how much data it have loaded.
JTableReadTableModelTask task = new JTableReadTableModelTask(file, null, progressBar, jTable);

2.Use "ReadTableModelTask"to do this.

Use ReadTableModelTask, you must inherit it first, and override its method done(),in the done method, you can use following statement to get a TableModel,  this is not convenient, so we recommend the first way to do this:
       Object o = get();
       if(o instanceof TableModel) {
TableModel model = (TableModel)get(); 

3.Use ModelIO to do this.

Here is the sample code, also you could put the following code into a background thread, For example: use javax.swing.SwingWorker.
Map m = new HashMap();
// you could set progressBar in the map,for example: m.put(ModelIO.PROGRESS_BAR, progressBar);
WorkBook book = ModelIO.readWorkBook(openUrl, format, m);
TableModel tableModel;
if(book.getSelectedSheet() != null) {
    tableModel = book.getSelectedSheet().getModel();
} else if(book.getSheetCount() > 0) {
    tableModel = book.getSheet(0).getModel();

Subscribe RSS >>download

Send Us Feedback