怎样读取EXCEL文件里的数据?
如题。如果有个成型的方法就更好了。
------解决方案--------------------List list = new ArrayList();
try {
File exlFile = new File(路径);
Workbook workbook = Workbook.getWorkbook(exlFile);
Sheet sheet = workbook.getSheet(0);//取得EXCEL第一TABLE页的内容
Cell[] colvalue = sheet.getColumn(1);//取得第一列
for (int j = 1; j < colvalue.length; j++) {//根据行数循环
Cell[] colrow = sheet.getRow(j);//取得对应行的列
for (int i = 0; i < colrow.length; i++) {//根据列数循环
list.add(colrow[i].getContents());//把本行的各列内容存放到LIST中
}
}
// 释放资源
workbook.close();
} catch (Exception e) {
// log.error(e.getMessage());
}
------解决方案--------------------读取含有多个sheet的EXCEL文件
import java.io.File;
import
java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class JXL {
public void readFile(String path){
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File(path));
}catch(BiffException e){
e.printStackTrace();
}catch(
IOException e){
e.printStackTrace();
}
Sheet[] sheet = workbook.getSheets();
Cell cell = null;
for(int k=0;k <sheet.length;k++){
for(int i = 0; i <sheet[k].getRows(); i++) {
for(int j = 0; j <sheet[k].getColumns(); j++) {
cell=sheet[k].getCell(j, i);
if(cell.getType()==CellType.NUMBER){
System.out.print(((NumberCell)cell).getValue());
}else if(cell.getType()==CellType.DATE){
System.out.print(((DateCell)cell).getDate());
}else{
System.out.print(cell.getContents());
}
System.out.print( "\t ");
}
System.out.print( "\n ");
}
}
workbook.close();
}
public static void main(String[] args){
String path= "c:/fundvalue-1.xls "; //这里写你要读的文件名 注意是“/”不是“\”
JXL OB=new JXL();
OB.readFile(path);
}
}