import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel{
private String filePath;
private List list = new ArrayList();
public ReadExcel(String filePath){
this.filePath = filePath;
}
private void readExcel() throws IOException, BiffException{
//建立輸入流
InputStream stream = new FileInputStream(filePath);
//擷取Excel檔案對象
Workbook rwb = Workbook.getWorkbook(stream);
//擷取檔案的指定工作表 預設的第一個
Sheet sheet = rwb.getSheet(0);
//行數(表頭的目錄不需要,從1開始)
for(int i=0; i
//建立一個數組 用來存儲每一列的值
String[] str = new String[sheet.getColumns()];
Cell cell = null;
//列數
for(int j=0; j
//擷取第i行,第j列的值
cell = sheet.getCell(j,i);
str[j] = cell.getContents();
}
//把剛擷取的列存入list
list.add(str);
}
}
private void outData(){
for(int i=0;i
String[] str = (String[])list.get(i);
for(int j=0;j
System.out.print(str[j]+'\t');
}
System.out.println();
}
}
public static void main(String args[]) throws BiffException, IOException{
ReadExcel excel = new ReadExcel("E:\\student1.xls");
excel.readExcel();
excel.outData();
}
}
Excel檔案内容: