需要导入的maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
代码
@Test
public void importBHKF() throws IOException {
InputStream inputStream = new FileInputStream("D:\\HG\\北环用户.xls");
POIFSFileSystem fs = new POIFSFileSystem(inputStream);
HSSFWorkbook workbook = new HSSFWorkbook(fs);
HSSFSheet sheet = workbook.getSheetAt(0);
if (sheet == null){
return;
}
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
HSSFRow row = sheet.getRow(rowNum);
if (row==null){
continue;
}
WtUser wtUser = new WtUser();
for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
HSSFCell cell = row.getCell(cellNum);
if (cell==null){
continue;
}
System.out.print(" "+getValue(cell));
}
users.add(wtUser);
System.out.println();
}
wtUserDao.saveAll(users);
}
private static String getValue(HSSFCell cell){
if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){
return String.valueOf(cell.getBooleanCellValue());
}else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
double cellValue = cell.getNumericCellValue();
return new DecimalFormat("#").format(cellValue);
}else {
return String.valueOf(cell.getRichStringCellValue());
}
}