1.创建一个Spring Boot项目(略)
下面直接上手代码展示
2.导入依赖
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.20</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.69</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
Controller层
/**
* 导出
* @param response
* @throws Exception
*/
@ApiOperation(value = "导出接口")
@GetMapping("/export")
public void export(HttpServletResponse response) throws Exception{
warehouseStockService.export(response);
}
/**
* excel 导入
* @param file
* @throws Exception
*/
@ApiOperation(value = "导入接口")
@PostMapping("/import")
public Boolean importFile(MultipartFile file) throws Exception {
return warehouseStockService.importFile(file);
}
Service层
/**
* 导出
* @param response
* @return
*/
@Override
public void export(HttpServletResponse response) throws Exception {
// 从数据库查询出所有的数据
List<WarehouseStockModel> list = warehouseStockModelMapper.listStock();
// 通过工具类创建writer 写出到磁盘路径
// ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/库存信息.xlsx");
// 在内存操作,写出到浏览器
ExcelWriter writer = ExcelUtil.getWriter(true);
//自定义标题别名
writer.addHeaderAlias("stockTime", "更新时间");
writer.setColumnWidth(9, 20);
writer.setDefaultRowHeight(18);
// 合并单元格后的标题行,使用默认标题样式
//writer.merge(8, "库存信息");
// 一次性写出list内的对象到excel,使用默认样式,强制输出标题
writer.write(list, true);
// 设置浏览器响应的格式
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String fileName = URLEncoder.encode("库存信息", "UTF-8");
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + dateString + ".xlsx");
ServletOutputStream out = response.getOutputStream();
writer.flush(out, true);
out.close();
writer.close();
}
/**
* 导入
* @param file
* @return
*/
@Override
public Boolean importFile(MultipartFile file) throws Exception{
InputStream inputStream = file.getInputStream();
ExcelReader reader = ExcelUtil.getReader(inputStream);
// 方式1:(推荐) 通过 javabean的方式读取Excel内的对象,但是要求表头必须是英文,跟javabean的属性要对应起来
// List<WarehouseStockModel> list = reader.readAll(WarehouseStockModel.class);
// 方式2:忽略表头的中文,直接读取表的内容
List<List<Object>> list = reader.read(1);
List<WarehouseStockModel> warehouseStockModels = CollUtil.newArrayList();
for (List<Object> row : list) {
WarehouseStockModel warehouseStockModel = new WarehouseStockModel();
warehouseStockModel.setStockDetailSeq(row.get(0).toString());
Integer nums = Integer.valueOf(row.get(7).toString());
warehouseStockModel.setStockCount(nums);
warehouseStockModel.setStockDescr(row.get(8).toString());
warehouseStockModel.setStockTime(new Date());
warehouseStockModels.add(warehouseStockModel);
}
warehouseStockModelService.saveBatch(warehouseStockModels);
for (WarehouseStockModel warehouseStockModel : warehouseStockModels) {
System.out.println(warehouseStockModel);
}
return true;
}