标记点:
1、引入所需要的jar包。下载路径:http://download.csdn.net/detail/china_guanq/9575760
2、确定.xls文件路径,或者导入到工程中。
3、可读取excel每一列(纵向)的数据,读取没一个工作表(页码)。
代码实例:
/**
* 实现方法
*/
public static void main(String[] args) throws FileNotFoundException, IOException{
String path1 = "../creditManager/src/main/webapp/vote/file/国际创新大赛8个赛区名单07.05.xls"; //工程中绝对文件路径
File file = new File(path1);
ImportExcelServer impor = new ImportExcelServer(file);
List<String> listXiBu = new ArrayList<String>();
List<List<Object>> list = impor.getSheetData(0); //获取工作表页码,索引从0开始
for (List<Object> list2 : list) {
try {
String segNoXiBu = (String)list2.get(2);
listXiBu.add(segNoXiBu);
System.out.println(segNoXiBu);
} catch (IndexOutOfBoundsException e) {
}
}
// System.out.println(listXiBu + "\t长度:" + listXiBu.size());
}
=================util 类
/**
* 返回list集合对象
* @param sheetNum
* @return
*/
public List<List<Object>> getSheetData(int sheetNum){
List<List<Object>> resultList = new ArrayList<List<Object>>();
if(workBook==null){
return resultList;
}
//获取sheet页
HSSFSheet sheet = workBook.getSheetAt(sheetNum);
if(sheet==null){
return resultList;
}
//获取总行号
int rowCount = sheet.getLastRowNum();
if(rowCount<1){
return resultList;
}
//System.out.println("rowCount="+rowCount);
//遍历行
for(int rowIndex=0; rowIndex<rowCount+1; rowIndex++){
HSSFRow row = sheet.getRow(rowIndex);
if(row!=null){
int columnCount = row.getLastCellNum();
//工号为空的行忽略
HSSFCell workNumberCell = row.getCell(0);
String workNumberCellStr = (String) this.getCellData(workNumberCell);
// if(HelpUtil.isEmpty(workNumberCellStr)){
// break;
// }
List<Object> rowData = new ArrayList<Object>();
for(int j=0;j<columnCount;j++){
HSSFCell cell = row.getCell(j);
Object cellStr = this.getCellData(cell);
rowData.add(cellStr);
}
resultList.add(rowData);
}
}
return resultList;
}