第1个回答 2015-04-01
public void test() throws InvalidFormatException, FileNotFoundException, IOException{
Double[] doubleArr1 = new Double[100];
Double[] doubleArr2 = new Double[100];
Workbook workbook = WorkbookFactory.create(new FileInputStream(
new File("Excel路径")));
Sheet sheet = workbook.getSheetAt(0);
//开始行
int startRowNum = sheet.getFirstRowNum();
//最后行
int endRowNum = sheet.getLastRowNum();
for (int rowNum = startRowNum; rowNum <= endRowNum; rowNum++) {
Row row = sheet.getRow(rowNum);
if (row == null)
continue;
int startCellNum = row.getFirstCellNum();
int endCellNum = row.getLastCellNum();
for (int cellNum = startCellNum; cellNum < endCellNum; cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell == null)
continue;
//取得数据类型
int type = cell.getCellType();
String cellNumValue = null;
switch (type) {
case Cell.CELL_TYPE_NUMERIC:// 数值、日期类型
double d = cell.getNumericCellValue();
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 日期类型
Date date = HSSFDateUtil.getJavaDate(d);
//TODO
} else {
// 数值类型
if(cellNum == 1){
doubleArr1[rowNum] = Double.valueOf(cellNumValue);
}
else if(cellNum == 2){
doubleArr2[rowNum] = Double.valueOf(cellNumValue);
}
}
break;
case Cell.CELL_TYPE_BLANK:// 空白单元格
cellNumValue = null;
//TODO
break;
case Cell.CELL_TYPE_STRING:// 字符类型
cellNumValue = String.valueOf(cellNumValue);
//TODO
break;
case Cell.CELL_TYPE_BOOLEAN:// 布尔类型
cellNumValue = Boolean.toString(cell.getBooleanCellValue());
//TODO
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障or非法字符
cellNumValue = "=#VALUE!";
//TODO
break;
default:
cellNumValue = "=#NAME?";// 未知类型
//TODO
break;
}
}
}
}