1.HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());//后面一般跟一个inputstream
2.Sheet sheet = wb.getSheetAt(i);//可以得到 sheet
3.Row row = sheet.getRow(i);// 可以得到row
4.Cell cell = row.getCell(i);//可以得到cell
5.得到cell里面的value ,以string返回
private String getCellStringValue(Cell cell) {
String cellContent = null;
if (cell == null)
return "";
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cellContent = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
try {
Double tempValue = cell.getNumericCellValue();
cellContent = tempValue.toString();
} catch (Exception e) {
log.error("", e);
}
break;
case Cell.CELL_TYPE_BOOLEAN:
cellContent = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
break;
case Cell.CELL_TYPE_BLANK:
case Cell.CELL_TYPE_ERROR:
cellContent = null;
default:
cellContent = "";
}
return cellContent;
}
最后 maven配置
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>