基于PHP和AJAX创建RSS聚合器 移动IP与DHCP、VPN等技术的比较 五招让Vista电脑更具个性化 Oracle 如何搞垮他的数据库——谈Oracle安全

介绍几种方法将Excel格式的文件导入到JTable中

[ 1450 查看 / 1 回复 ]

How to import Excel file into JTable

Here are three way to do this.
  • Use "JTableReadTableModelTask " to do this. ( recommended )
  • Use "ReadTableModelTask" to do this
  • Use ModelIOto 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.
JProgressBarprogressBar = 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 itsmethod done(),in the done method, you can use following statement toget a TableModel,  this is not convenient, so we recommend the firstway to do this:
        Object o = get();
      if(o instanceof TableModel) {
TableModel model = (TableModel)get(); 
}

3.Use ModelIOto do this.
Here is the sample code, also you could put the following code into abackground 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控件集

TOP

谢谢提供
TOP