selah

一个从xls中读取数据写入ArrayList的例子

public ArrayList<ProjInfo> getProjInfo(String filename){

ArrayList<ProjInfo> projs = new ArrayList<ProjInfo>(); 

File proj_xls = new File(this.filename);

Workbook wb = null;

try {

wb = Workbook.getWorkbook(proj_xls);

} catch (BiffException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

Sheet ws = wb.getSheet(0);

int row_num = ws.getRows();

int col_num = ws.getColumns();

int key_col = -1;

int pname_col = -1;

int status_col = -1;

int updated_col = -1;

for (int i=0; i< row_num; i++){

if ((-1 == key_col)||(-1 == pname_col)||(-1 == status_col)||(-1 == updated_col)){

for (int j=0; j<col_num; j++){

String cell_content = ws.getCell(j,i).getContents();

if(cell_content.contentEquals("Key")){

key_col = j;

}else if (cell_content.contentEquals("Summary")){

pname_col = j;

}else if (cell_content.contentEquals("Status")){

status_col = j;

}else if (cell_content.contentEquals("Updated")){

updated_col = j;

}

}

}else{

ProjInfo pi = new ProjInfo();

pi.setKey(ws.getCell(key_col, i).getContents());

pi.setProjname(ws.getCell(pname_col, i).getContents());

pi.setStatus(ws.getCell(status_col,i).getContents());

pi.setUpdated(ws.getCell(updated_col, i).getContents().split("\\s")[0]);

if (! pi.getKey().isEmpty()){

    projs.add(pi);

}

}

}

return projs;

}


评论
热度(1)

© selah | Powered by LOFTER