依赖: <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.6</version> </dependency>
导出:
public Class Test{
public void importExcel(HttpServletResponse response){
List<ProjectInfoVo> list = new ArrayList<>();
//ExcelWriter excelWriter = null;
for(int i=0;i<8;i++){
ProjectInfoVo infoVo = new ProjectInfoVo();
infoVo.setId(i+1);
infoVo.setProjectTypeName("香港建材类项目"+i+1);
infoVo.setPrjName("九龙房地产开发项目"+i+1);
infoVo.setDetailAddr("香港九龙");
infoVo.setPrjIntro("社区居住类房屋");
infoVo.setRealStartDate(new Date());
infoVo.setResultVal("开发中");
list.add(infoVo);
}
try{
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyExcel没有关系
String fileNames = "mergeWrite" + System.currentTimeMillis() + ".xlsx";
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileNames + ".xlsx");
EasyExcel.write(response.getOutputStream(), ProjectInfoVo.class).sheet("sheet名称")
.doWrite(list);
} catch (Exception e) {
e.printStackTrace();
}
}
public void importWriter(@RequestPart("file") MultipartFile file){
List<Member> list = EasyExcel.read(file.getInputStream())
.head(Member.class)
.sheet()
.doReadSync();
for (Member member : list) {
System.out.println(member);
}
}
}