找不到类似的,自己琢磨出来,代码没优化。
import java.io.File;
import java.io.FileInputStream;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFileChooser;
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;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
*
* @author Chihua_Sun
*/
public class ImportTable {
public void readExcel(DefaultTableModel tableModel,JTable jTable){
JFileChooser fileChooser=new JFileChooser();//文件选择
MyFileFilter fileFilter=new MyFileFilter(); //创建过滤器对象,自定义的过滤器
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setAcceptAllFileFilterUsed(false); //去掉显示所有文件这个过滤器。
fileChooser.setFileFilter(fileFilter);
int result=fileChooser.showOpenDialog(null);//显示保存对话框,并返回操作选项
if(result==JFileChooser.APPROVE_OPTION){ //判断是否选择保存按钮
File excel=new File(fileChooser.getSelectedFile().getPath());
String name=excel.getName();
String suffix=name.substring(name.lastIndexOf('.')+1).toLowerCase();
if("xls".equals(suffix)){
readXls(excel, tableModel, jTable);
}else if("xlsx".equals(suffix)){
readXlsx(excel, tableModel, jTable);
}
}
}
/**
* Read the Excel 2010s
* @param path the path of the Excel
* @param tableModel
* @param jTable
*/
public void readXlsx(File path,DefaultTableModel tableModel,JTable jTable){
try{
InputStream is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
// 读取 Sheet
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
//读取表头
XSSFRow title = xssfSheet.getRow(0);
Object[] cName = new Object[title.getPhysicalNumberOfCells()];
for(int tnum=0;tnum<title.getPhysicalNumberOfCells();tnum++){
cName[tnum]=title.getCell(tnum).toString();
}
tableModel.setColumnIdentifiers(cName);//把表头放进tableModel
// 读取行
for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
Object[] row = new Object[title.getPhysicalNumberOfCells()];
if (xssfRow != null) {
for(int i=0;i<xssfRow.getPhysicalNumberOfCells();i++){
row[i]=((XSSFCell)xssfRow.getCell(i)).toString();
}
}
tableModel.addRow(row);
}
}catch(Exception io){
JOptionPane.showMessageDialog(null,"导入导入失败,请确认Excel文件格式正确!(不支持单元格合并等复杂Excel文件)","失败",JOptionPane.INFORMATION_MESSAGE);
}
JOptionPane.showMessageDialog(null,"导入成功","成功",JOptionPane.INFORMATION_MESSAGE);
jTable.setModel(tableModel);
}
/**
* Read the Excel 2003-2007
* @param path the path of the Excel
* @param tableModel
* @param jTable
*/
public void readXls(File path,DefaultTableModel tableModel,JTable jTable) {
try{
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
// 读取 Sheet
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
//读取表头
HSSFRow title = hssfSheet.getRow(0);
Object[] cName = new Object[title.getPhysicalNumberOfCells()];
for(int tnum=0;tnum<title.getPhysicalNumberOfCells();tnum++){
cName[tnum]=title.getCell(tnum).toString();
}
tableModel.setColumnIdentifiers(cName);//把表头放进tableModel
// 读取 行
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
Object[] row = new Object[title.getPhysicalNumberOfCells()];
if (hssfRow != null) {
for(int i=0;i<hssfRow.getPhysicalNumberOfCells();i++){
row[i]=((HSSFCell )hssfRow.getCell(i)).toString();
}
}
tableModel.addRow(row);
}
}catch(Exception io){
JOptionPane.showMessageDialog(null,"导入导入失败,请确认Excel文件格式正确!(不支持单元格合并等复杂Excel文件)","失败",JOptionPane.INFORMATION_MESSAGE);
}
JOptionPane.showMessageDialog(null,"导入成功","成功",JOptionPane.INFORMATION_MESSAGE);
jTable.setModel(tableModel);
}
public void readheader(){
}
//
// public boolean readExcelxls(File file,DefaultTableModel tableModel,JTable jTable){
// Workbook rwb = null;
// try{
//
// FileInputStream is = new FileInputStream(file);
// //创建一个workbook类读取excel文件
// rwb = Workbook.getWorkbook(file);
得到第i个工作薄
// Sheet st = rwb.getSheet(0);//这里有两种方法获取sheet表,1为名字,而为下标,从0开始
//得到st的列数
System.out.println("列"+ st.getColumns());
//得到st的行数
// System.out.println("行"+ st.getRows());
Vector<String> cName = new Vector<String>();
// Object[] cName = new Object[st.getColumns()];
// for(int j = 0;j<st.getColumns();j++){
Cell c00 =st.getCell(j,0);
LabelCell labelc00 = (LabelCell)c00;
cName[j]=labelc00.getString();
cName.add(((LabelCell)st.getCell(j,0)).getString());
// cName[j]=((LabelCell)st.getCell(j,0)).getString();
// }
// tableModel.setColumnIdentifiers(cName);
// Object[] object = new Object[st.getRows()];
// if(st.getRows()>2){
// for(int t=tableModel.getRowCount()-1;t>=0;t--){
// tableModel.removeRow(t);
// }
// //行循环
// for(int i = 1;i<st.getRows();i++){
// //列循环
// for(int j = 0;j<st.getColumns();j++){
// // 得到第j列第i行的数据
// Cell c00 = st.getCell(j,i);
// 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] = numd;
jTable.setValueAt(numi, i-1, j);
// }
//
// }
// tableModel.addRow(object);
// }
// }else{
// for(int i = 1;i<st.getRows();i++){
// //列循环
// for(int j = 0;j<st.getColumns();j++){
// // 得到第j列第i行的数据
// Cell c00 = st.getCell(j,i);
// 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,"导入"+file.getName()+"成功","成功",JOptionPane.INFORMATION_MESSAGE);
// jTable.setModel(tableModel);
// }catch(FileNotFoundException e){
// System.out.println("文件"+file.getName()+"没有找到");
// return false;
// } catch (BiffException e) {
// System.out.println("文件格式不正确");
// JOptionPane.showMessageDialog(null,"文件格式不正确","失败",JOptionPane.ERROR_MESSAGE);
// return false;
// } catch (IOException e) {
// System.out.println("文件读写失败");
// return false;
// }finally{
关闭
// try{
// rwb.close();
// }catch(Exception e){
// }
// }
// return true;
//}
//
}