介紹幾種方法將Excel格式的文件導入到JTable中

How to import Excel file into JTable
html

Here are three way to do this.java

  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:api

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);
task.execute();

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.ide

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();
}

 

奇新Java控件——Java控件提供商和Java RIA, Web, J2ME解決方案開發商測試

JComponentPack是基於Swing框架的Java GUI控件集合,它包括JDataGrid電子表格版本JDataGrid數據庫版本JComponentSet--Java swing控件集網站


瞭解更多產品。。。
 
this



--------------------------
新聞: 微軟稱已與國內主流網站合做測試IE8兼容性
導航: 博客園首頁   知識庫   新聞   招聘   社區   小組   博問   網摘   找找看
相關文章
相關標籤/搜索