1、引入依赖
<poi.version > 3.9</poi.version >
<poi-ooxml.version > 3.9</poi-ooxml.version >
<dependency >
<groupId > org.apache.poi</groupId >
<artifactId > poi</artifactId >
<version > ${poi.version}</version >
</dependency >
<dependency >
<groupId > org.apache.poi</groupId >
<artifactId > poi-ooxml</artifactId >
<version > ${poi-ooxml.version}</version >
</dependency >
2、编码
下面是读取Excel数据的代码内容,忽略业务逻辑,自己手动删除
@Resource
private UwoSpecialUserService uwoSpecialUserService;
private final static String xls = "xls" ;
private final static String xlsx = "xlsx" ;
@RequestMapping(value = "1425" , method = {RequestMethod.GET, RequestMethod.POST})
public Response<Object> importFile (@RequestParam ("file") MultipartFile file){
List<UwoSpecialUser> uwoSpecialUserList=new ArrayList<>();
Response<Object> result = new Response<Object>();
try {
int count = 0 ;
String msg = "" ;
Workbook workbook = getWorkBook(file);
Sheet sheet = workbook.getSheetAt(0 );
for (Row row : sheet) {
if (row.getRowNum()==0 ||row.getRowNum()==1 ){
continue ;
}
UwoSpecialUser uwoSpecialUser=new UwoSpecialUser();
if (row.getCell(0 )!=null ){
row.getCell(0 ).setCellType(Cell.CELL_TYPE_STRING);
String specialUserName = row.getCell(0 ).getStringCellValue();
uwoSpecialUser.setSpecialUserName(specialUserName);
}
if (row.getCell(1 )!=null ){
row.getCell(1 ).setCellType(Cell.CELL_TYPE_STRING);
String phonenum = row.getCell(1 ).getStringCellValue();
uwoSpecialUser.setPhonenum(phonenum);
}
if (row.getCell(2 )!=null ){
row.getCell(2 ).setCellType(Cell.CELL_TYPE_STRING);
String money = row.getCell(