一、导出至Excel

1.导入依赖

导出方法需要使用到fastJson的依赖,这里也直接导入

点击查看代码
<!--阿里的easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.0-beta1</version>
<scope>compile</scope>
</dependency>
<!-- 阿里fastjson包JSON转换-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>

2.准备导出数据

这里简单创建一个Student的实体类

在导出的数据中,要是不想有某个属性,可以在该属性上面添加@ExcelIgnore注解,

同样,若在导出的数据中想要某个属性,可以在该属性上面添加@ExcelProperty("姓名")注解

若在属性上面啥注解也没有,默认也是会导出该属性的数据,但是表头就是这个属性名。

点击查看实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
private static final long serialVersionUID = 1L; @ExcelProperty("Id")
private String id; @ExcelProperty("姓名")
private String name; @ExcelProperty("学号")
private String studentNo; @ExcelProperty("电话")
private String phone; @ExcelProperty("性别")
private Integer sex; @ExcelIgnore
private Integer age; private String h;
}

这里再简单编写一个service方法,模拟查询到的数据;

点击查看Service方法
import java.util.ArrayList;
import java.util.List; @Service
public class ExportExcelService { public List<Student> getData(){
Student student = new Student("001","张三","191026","13299998888",1,20,"1");
Student student1 = new Student("002","李四","191027","13299993333",0,20,"1");
Student student2 = new Student("003","王五","191028","13293338888",1,20,"1");
return new ArrayList<Student>(){{
add(student);
add(student1);
add(student2);
}};
}
}

3.编写controller方法

这里导出的方法就直接写在controller里了,从service查数据。

这里其实需要改动的就只有两个地方,一个就是导出数据集合的泛型,二是导出的数据集合。

EasyExcel.write(response.getOutputStream(), Student.class) .autoCloseStream(Boolean.FALSE) .sheet("导出详情") .doWrite(studentList);

点击查看代码
@RestController
@RequestMapping("/export")
public class ExportExcelController {
@Autowired
private ExportExcelService exportExcelService; @GetMapping("/")
public void export(HttpServletResponse response) throws IOException {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
String fileName = URLEncoder.encode("导出", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
List<Student> studentList = exportExcelService.getData();
// 这里需要设置不关闭流
EasyExcel.write(response.getOutputStream(), Student.class)
.autoCloseStream(Boolean.FALSE)
.sheet("导出详情")
.doWrite(studentList);
} catch (Exception e) {
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
Map<String, String> map = new HashMap();
map.put("status", "failure");
map.put("message", "下载文件失败" + e.getMessage());
response.getWriter().println(JSON.toJSONString(map));
}
}
}

4.最后附上效果图

当然,默认的文件名,还有表头的宽高,数据一行的宽高都是可以设置的。都有相应的注解,加在实体类上就可以了,这里就简单逻辑几个:

设置表头的高度:@HeadRowHeight(20)

设置数据的高度:@ContentRowHeight(20)

设置数据的宽度:@ColumnWidth(25)

二、从Excel导入数据

1.依赖

导入和导出使用的依赖都是一样的,只是调用的方法不同,使用一中导入的依赖即可。

2.掉方法

    @SneakyThrows
@PostMapping("/")
@ApiOperation(value="导入")
public List<Student> importData(@RequestPart("file") MultipartFile file) {
List<Student> studentList = EasyExcel.read(file.getInputStream())
.head(Student.class)
.sheet()
.doReadSync();
return studentList;
}

3.测试

最新文章

  1. sql对于between和时间
  2. mysql 几个命令
  3. 为Elasticsearch添加中文分词
  4. 【转】更新SDK后,打包APK时报 the zipalign tool was not found in the sdk
  5. linux 上查找pid,筛选出来
  6. 【实用技术】DreamWeaver常用快捷键
  7. 基于位运算符的IP和数值转换
  8. JavaScript闭包小窥
  9. 复习之webview(观看张荣超视频)
  10. redis中与key相关的命令
  11. [Swift]LeetCode901. 股票价格跨度 | Online Stock Span
  12. JavaWeb-----实现第一个Servlet程序
  13. iis默认文档有什么用?
  14. [Fiddler] 开启Fiddler抓包的时候产品报“证书错误”
  15. MiniUI合并单元格
  16. Nginx配置location及rewrite规则
  17. goland激活
  18. LeetCode 360. Sort Transformed Array
  19. Android 开发手记二 C可执行程序编译实例(转帖)
  20. POJ1226 Substrings(二分+后缀数组)

热门文章

  1. Vue2基础知识学习
  2. 这是不是你想要了解SQL的艺术,基础语法等等
  3. 用map来统计数组中各个字符串的数量
  4. 图学习【参考资料2】-知识补充与node2vec代码注解
  5. 修复 docker build 错误 &quot;ERROR: No build stage in current context&quot;
  6. pytest文档82 - 用例收集钩子 pytest_collect_file 的使用
  7. 基于python的数学建模---高阶样条插值
  8. i春秋Fuzz
  9. PEP8语法规范解释说明
  10. Linux 基础-查看 cpu、内存和环境等信息