引包
<!-- excel导入导出 依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>RELEASE</version>
</dependency>
代码示例
@Test
public void import01(){
System.out.println("--------------------");
Workbook workbook=null;
try {
String filePath="C:\\Users\\Desktop\\小蜜蜂系统录入必填项.xlsx";
InputStream inputStream = new FileInputStream(filePath);
workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);
int rows = sheet.getLastRowNum()+1;
System.out.println("------------rows:"+rows);
List<Object[]> list = new ArrayList<Object[]>();
for (int i = 0; i < rows; i++) {
Row row = sheet.getRow(i);
Object[] objects = new Object[row.getLastCellNum()];
for (Cell cell : row) {
String cellType = cell.getCellType().toString();
int index=cell.getColumnIndex();
if("NUMERIC".equals(cellType)){
objects[index] = (int) cell.getNumericCellValue();
}else if("STRING".equals(cellType)){
objects[index] = cell.getStringCellValue();
}else if("BOOLEAN".equals(cellType)){
objects[index] = cell.getBooleanCellValue();
}else if("ERROR".equals(cellType)){
objects[index] = cell.getErrorCellValue();
}
System.out.println("------index:"+index+"---"+objects[index]);
}
list.add(objects);
}
System.out.println("导入文件解析成功!");
System.out.println("------list长度:"+list.size());
} catch (Exception e) {
e.printStackTrace();
}
}