資源簡介
本人 網(wǎng)上找的,修改后,很好用,跟大家分享一下

代碼片段和文件信息
package?com.exam.write;
import?java.io.*;
import?javax.swing.JOptionPane;
import?javax.swing.JTable;
import?javax.swing.table.DefaultTableModel;
import?jxl.Cell;
import?jxl.CellType;
import?jxl.LabelCell;
import?jxl.NumberCell;
import?jxl.Sheet;
import?jxl.Workbook;
import?jxl.read.biff.BiffException;
public?class?readExceltoJtable?{
public?boolean?readExceltoJtable(File?fileDefaultTableModel?tableModelJTable?jTable){
Workbook?rwb?=?null;
object[]?object?=?new?object[jTable.getColumnCount()];
try{
FileInputStream?is?=?new?FileInputStream(file);
//創(chuàng)建一個(gè)workbook類讀取excel文件
rwb?=?Workbook.getWorkbook(is);
// 得到第i個(gè)工作薄
Sheet?st?=?rwb.getSheet(0);//這里有兩種方法獲取sheet表1為名字,而為下標(biāo),從0開始
if(st.getRows()>2){
for(int?t=tableModel.getRowCount()-1;t>=0;t--){
tableModel.removeRow(t);
}
//行循環(huán)
for(int?i?=?1;i //列循環(huán)
for(int?j?=?0;j // 得到第j列第i行的數(shù)據(jù)
Cell?c00?=?st.getCell(ji);
if(c00.getType()?==?CellType.LABEL)
{
????LabelCell?labelc00?=?(LabelCell)c00;
????object[j]?=?labelc00.getString();
????//jTable.setValueAt(labelc00.getString()?i-1?j);
}else?if(c00.getType()?==?CellType.NUMBER)
{
Double?numd;
int?numi;
NumberCell?numc10?=?(NumberCell)c00;
numd?=?new?Double(numc10.getValue());
numi?=?numd.intValue();
object[j]?=?numi;
// ????jTable.setValueAt(numi?i-1?j);
}
}
tableModel.addRow(object);
}
}else{
for(int?i?=?1;i //列循環(huán)
for(int?j?=?0;j // 得到第j列第i行的數(shù)據(jù)
Cell?c00?=?st.getCell(ji);
if(c00.getType()?==?CellType.LABEL)
{
????LabelCell?labelc00?=?(LabelCell)c00;
????object[j]?=?labelc00.getString();
????//jTable.setValueAt(labelc00.getString()?i-1?j);
}else?if(c00.getType()?==?CellType.NUMBER)
{
Double?numd;
int?numi;
NumberCell?numc10?=?(NumberCell)c00;
numd?=?new?Double(numc10.getValue());
numi?=?numd.intValue();
object[j]?=?numi;
// ????jTable.setValueAt(numi?i-1?j);
}
}
tableModel.addRow(object);
}
}
JOptionPane.showMessageDialog(null“導(dǎo)入“+file.getName()+“成功““成功“JOptionPane.INFORMATION_MESSAGE);
}catch(FileNotFoundException?e){
System.out.println(“文件“+file.getName()+“沒有找到“);
return?false;
}?catch?(BiffException?e)?{
System.out.println(“文件格式不正確“);
return?false;
}?catch?(IOException?e)?{
System.out.println(“文件讀寫失敗“);
return?false;
}finally{
// 關(guān)閉
rwb.close();
}
return?true;
}
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????2909??2011-08-23?11:57??readExceltoJtable.java
評(píng)論
共有 條評(píng)論