java office2003_JAVA解析EXCEL(2003和2007)

importjava.util.ArrayList;importjava.io.FileInputStream;importjava.io.IOException;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;public classReadExcel {public ArrayList>readExcel(String fileName,String path) {

ArrayList> Row =new ArrayList>();try{

Workbook workBook= null;try{

workBook= new XSSFWorkbook(path+"\\"+fileName);

}catch(Exception ex) {

workBook= new HSSFWorkbook(new FileInputStream(path+"\\"+fileName));

}for (int numSheet = 0; numSheet < workBook.getNumberOfSheets(); numSheet++) {

Sheet sheet=workBook.getSheetAt(numSheet);if (sheet == null) {continue;

}//循环行Row

for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {

Row row=sheet.getRow(rowNum);if (row == null) {continue;

}//循环列Cell

ArrayList arrCell =new ArrayList();for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {

Cell cell=row.getCell(cellNum);if (cell == null) {continue;

}

arrCell.add(getValue(cell));

}

Row.add(arrCell);

}

}

}catch(IOException e) {

System.out.println("e:"+e);

}returnRow;

}privateString getValue(Cell cell) {if (cell.getCellType() ==cell.CELL_TYPE_BOOLEAN) {returnString.valueOf(cell.getBooleanCellValue());

}else if (cell.getCellType() ==cell.CELL_TYPE_NUMERIC) {returnString.valueOf(cell.getNumericCellValue());

}else{returnString.valueOf(cell.getStringCellValue());

}

}public static voidmain(String[] args) {

ReadExcel s= newReadExcel();//ArrayList> row=s.readExcel("TEST.xlsx","D:\\Program Files\\Java");

ArrayList> row=s.readExcel("TEST1.xls","D:\\Program Files\\Java");

System.out.println("size:"+row.size());for (ArrayListcell : row) {for(String str : cell) {

System.out.println(str);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值