springboot整合easyExcel实现报表导入导出
程序员文章站
2024-03-20 17:37:16
...
1.引入maven坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
2.编写导出实体类
package com.myservice.excel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @ClassName UserEntity
* @Description TODO
* @Author kejj
* @Date 2020/9/26 16:35
* @Version 1.0
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {
@ExcelProperty("用户名")
private String userName;
@ExcelProperty("年龄")
private int age;
@ExcelProperty("生日")
@ColumnWidth(30) //设置行宽
private Date birthday;
@ExcelProperty("备注")
private String desc;
}
3.实现监听器
package com.myservice.excel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* @ClassName UserListerner
* @Description TODO
* @Author kejj
* @Date 2020/9/26 16:39
* @Version 1.0
*/
@Component
@Scope("prototype")
public class UserListerner extends AnalysisEventListener<UserEntity> {
/**
逻辑处理
*/
@Override
public void invoke(UserEntity userEntity, AnalysisContext analysisContext) {
System.out.println(userEntity);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
4.对外暴露接口
package com.myservice.excel;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName ExportExcel
* @Description TODO
* @Author kejj
* @Date 2020/9/26 16:29
* @Version 1.0
*/
@Controller
@RequestMapping("/excel")
public class ExportExcel {
/**
* 文件上传
* @param file
* @return
* @throws IOException
*/
@RequestMapping("/upload")
@ResponseBody
public Map read(MultipartFile file) throws IOException {
ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), UserEntity.class, new UserListerner());
//此处调用监听器的invoke方法,实现代码逻辑
read.sheet().doRead();
HashMap<Object, Object> map = new HashMap<>();
map.put("respCode","200");
map.put("desc","");
return map;
}
/**
* 文件下载
* @param response
* @throws IOException
*/
@RequestMapping("download")
@ResponseBody
public void write(HttpServletResponse response) throws IOException {
String fileName = new String("用户模板.xlsx".getBytes(), "ISO-8859-1");
response.addHeader("Content-Disposition", "filename=" + fileName);
ServletOutputStream out = response.getOutputStream();
//准备数据
ArrayList<Object> objects = new ArrayList<>();
for(int i = 0;i<10;i++){
objects.add(new UserEntity("张三",13,new Date(),""));
}
//导出
ExcelWriterBuilder write = EasyExcel.write(out, UserEntity.class);
write.sheet().doWrite(objects);
}
}
``
上一篇: java操作cookie
推荐阅读
-
springboot整合easyExcel实现报表导入导出
-
用EasyExcel实现表格的导入,导出
-
掌财社:Java如何使用EasyExcel实现导入导出excel
-
THINKPHP3.2.3整合PHPexcel实现数据的导入导出
-
SpringBoot整合EasyExcel实现文件导入导出
-
SpringBoot集成EasyExcel实现Excel导入的方法
-
Java+EasyExcel实现文件的导入导出
-
Springboot2整合easypoi实现导入导出、OSS图片导出到Excel
-
excel表头_Springboot整合easyExcel导入导出Excel - Code2020
-
THINKPHP3.2.3整合PHPexcel实现数据的导入导出