引言:在实际的web项目中经常会遇到什么报表导出、批量导入数据啊什么的。操作excel的工具包有很多,这里介绍下poi是比较火的之一
一、maven依赖,按需引入后面三那个是必要的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
2、读取excel
excel的2003版本和2007版本有所不同,注意一下就行了
AjaxResult是一个简单的返回结果类就不贴了,能来看这篇博文的都不是小白了。
@PostMapping("/upload")
public AjaxResult importCar(@RequestParam("file") MultipartFile file) {
int count = 0;
InputStream inputStream = null;
// 文件名
String fileName = file.getOriginalFilename().toLowerCase();
// 文件格式
final String type = fileName.substring(fileName.lastIndexOf(".") + 1);
try {
inputStream = file.getInputStream();
switch (type) {
case "xls":// 2003
count = carExcel(new HSSFWorkbook(inputStream));
break;
case "xlsx":// 2007
count = carExcel(new XSSFWorkbook(inputStream));
break;
default:
log.info("导入失败:文件格式{}不支持!", type);
return AjaxResult.fail("导入的文件格式错误,允许的格式:xls、xlsx");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return AjaxResult.Success("成功导入" + count + "条数据!");
}
private int carExcel(Workbook wb) {
int count = 0;
log.info("excel导入车辆开始...");
List<Car> cars = new ArrayList<>(100);
Car car = null;
// 获取sheet数量
int numberOfSheets = wb.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheet = wb.getSheetAt(i);
// 行数
int RowNum = sheet.getPhysicalNumberOfRows();
// 跳过标题行
for (int j = 1; j < RowNum; j++) {
car = new Car();
Row row = sheet.getRow(j);
// 得到单元格数
// 车牌
String plate = row.getCell(0).getStringCellValue();
car.setPlate(StringUtils.trim(plate));
// 型号
String cartype = row.getCell(1).getStringCellValue();
car.setCartype(StringUtils.trim(cartype));
// 类型
String type = row.getCell(2).getStringCellValue();
TypeEnum enm = CarEnum.TypeEnum.getEnum(StringUtils.trim(type));
car.setType(enm.getCode());
// 油耗
double oilwea = row.getCell(3).getNumericCellValue();
car.setOilwea(oilwea);
// 载重
double appload = row.getCell(4).getNumericCellValue();
car.setAppload(appload);
// 状态
String status = row.getCell(5).getStringCellValue();
StatusEnum enm1 = CarEnum.StatusEnum.getEnum(StringUtils.trim(status));
car.setStatus(enm1.getCode());
log.info("读取第{}张sheet,第{}行数据:车牌号-{}", i, j, car.getPlate());
cars.add(car);
if (cars.size() % 100 == 0) {
// 100条插入一次
count += carService.insertBatch(cars);
cars.clear();
}
}
}
if (!CollectionUtils.isEmpty(cars)) {
count += carService.insertBatch(cars);
}
log.info("新增{}条车辆数据!", count);
return count;
}
有个坑需要注意:
如果你的excel文件是通过修改后缀名来的话(xls改为xlsx或者xlsx改为xls也算),解析的时候会有异常。(别问我怎么知道的?)
希望对您有所微薄帮助,求大佬轻喷,欢迎大家相互交流。
向上的路并不拥挤,而大多数人选择了安逸——it疯子也