1、引入依赖
<dependency><groupId>com.pig4cloud.excel</groupId><artifactId>excel-spring-boot-starter</artifactId><version>1.2.7</version>
</dependency>
2、导出
@ResponseExcel(name = "测试列表")
@PostMapping("/export")
public List<TestVO> export() {List<TestVO> list = new ArrayList<>();TestVO vo = new TestVO();vo.setId("1");vo.setName("杰克马");list.add(vo);TestVO vo1 = new TestVO();vo1.setId("2");vo1.setName("乔峰");list.add(vo1);return list;
}
@Data
public class TestVO {@ExcelProperty(index = 0, value = "编号")@ColumnWidth(15)private String id;@ExcelProperty(index = 1, value = "名称")@ColumnWidth(20)private String name;
}
导出效果
3、导入
@PostMapping("/import")
public void importTest(@RequestExcel List<ImportVO> list) {log.info("导入数据 = {}", JSONObject.toJSONString(list));
}
@Data
public class ImportVO {@ExcelProperty(index = 0, value = "编号")private String id;@ExcelProperty(index = 1, value = "名称")private String name;
}
创建模板文件
用postman测试调用,其中key必须为file
导入效果