1、需要的maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
2、后端代码配置
包含入参,file转Excel文件,及Excel文件内容基本解析(仅参考)
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSON;
@RestController
public class TestController {
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public void importExcel(MultipartFile file) throws Exception {
// file为上传的后台接收到的前端文件,文件名称可访问,内容为字节数据需要转成POI专属的Workbook对象进行内容数据获取
Assert.notNull(file, "上传文件不能为空");
// 将输入的file文件转成POI的Excel文件对象Workbook
String fileName = file.getOriginalFilename(); // 获取文件名
Workbook workbook = null;
if (StringUtils.endsWithIgnoreCase(fileName, "XLS")) {
try {
workbook = new HSSFWorkbook(file.getInputStream()); // XLS类型,2003Excel版本
} catch (IOException e) {
e.printStackTrace();
}
} else if (StringUtils.endsWithIgnoreCase(fileName, "XLSX")) {
try {
workbook = new XSSFWorkbook(file.getInputStream()); // XLSX类型,2007Excel版本
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件类型不符,不是Excel文件 ");
}
// 解析Workbook对象,获取上传Excel内容(实际中,上传Excel文件需要按照指定规范编写上传)
// 得到一个Sheet工作表
Sheet sheet = workbook.getSheetAt(0);
System.out.println("sheet:"+JSON.toJSONString(sheet));
// 获得Sheet工作表的数据总行数(第0行开始)
int totalRowNum = sheet.getLastRowNum();
System.out.println("总行数totalRowNum:"+totalRowNum);
// 获取当前sheet的第一行
Row row1 = sheet.getRow(0);
System.out.println("row1:"+JSON.toJSONString(row1));
// 获取当前sheet的第二行
Row row2 = sheet.getRow(1);
System.out.println("row2:"+JSON.toJSONString(row2));
// 获取当前sheet的第二行总列数(第1列开始)
int physicalNumberOfCells = row2.getPhysicalNumberOfCells();
System.out.println("获得第二行的总列数physicalNumberOfCells:"+physicalNumberOfCells);
// 获取第二行的第一个cell
Cell cell = row2.getCell(0);
System.out.println("第二行第一列cell:"+cell);
// 获取cell的数据类型
int cellType = cell.getCellType();
System.out.println("cellType:"+JSON.toJSONString(cellType));
// 获取cell的数据,需要根据类型选择调用以下的方法
// cell类型为String,获取内容
String stringCellValue = cell.getStringCellValue();
System.out.println("stringCellValue:"+JSON.toJSONString(stringCellValue));
// cell类型为数字,获取内容
// double numericCellValue = cell.getNumericCellValue();
cell类型为布尔型,获取内容
// boolean booleanCellValue = cell.getBooleanCellValue();
}
}
3、接口调用及测试
导入excel文件入下
后台获取响应