一、读取xlsx文件
1、pom.xml
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2、测试类
public class PoiDemo {
@Test
public void exportExcel()throws Exception{
InputStream is = Class.forName("com.mytest.PoiDemo").getClassLoader().getResourceAsStream("poi.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(is);
XSSFSheet sheet = workbook.getSheetAt(0);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType()== XSSFCell.CELL_TYPE_NUMERIC){
if (HSSFDateUtil.isCellDateFormatted(cell)){
double dateDouble = cell.getNumericCellValue();
Date javaDate = HSSFDateUtil.getJavaDate(dateDouble);
String formatStr = dateFormat.format(javaDate);
System.out.print(formatStr+" ");
}else {
double numericCellValue = cell.getNumericCellValue();
System.out.print(numericCellValue+" ");
}
} else if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){
System.out.print(cell.getBooleanCellValue()+" ");
}else if (cell.getCellType()==Cell.CELL_TYPE_FORMULA){
System.out.println(cell.getCellFormula().toString());
}else{
String stringCellValue = cell.getStringCellValue();
System.out.print(stringCellValue+" ");
}
}
System.out.println();
}
workbook.close();
is.close();
}
}