马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
干系文章链接:
版本依靠
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>easyexcel</artifactId>
- <version>3.3.3</version>
- </dependency>
复制代码 @ExcelProperty
指定当前字段对应excel中的那一列,可以根据名字大概Index去匹配,固然也可以不写。
- value:指定写入的列头,如果不指定则利用成员变量的名字作为列头;如果要设置复杂的头,可以为value指定多个值
- order:优先级高于value,会根据order的序次来匹配实体和excel中数据的序次
- index:优先级高于value和order,指定写到第几列,如果不指定则根据成员变量位置排序;默认第一个字段就是index=0
- converter:指定当前字段用什么转换器,默认会自动选择。可以用来设置范例转换器,必要实现Converter接口
value
指定属性名
- @Data
- public class User {
-
-
- private Integer userId;
- private String name;
- private String phone;
- private String email;
- private Date createTime;
- }
复制代码- @RestController
- public class TestController {
-
-
- @GetMapping("/test1")
- public void test1(HttpServletResponse response) {
-
-
- try {
-
-
- response.setContentType("application/vnd.ms-excel");
- response.setCharacterEncoding("utf-8");
- String fileName = URLEncoder.encode("test1", "UTF-8").replaceAll("\\+", "%20");
- response.setHeader("Content-disposition", "attachment;filename" + fileName + ".xls");
- User user = new User();
- user.setUserId(123);
- user.setName("as");
- user.setPhone("15213");
- user.setEmail("5456");
- user.setCreateTime(13213L);
- EasyExcel.write(response.getOutputStream(), User.class)
- .sheet("test")
- .doWrite(Arrays.asList(user));
- } catch (Exception e) {
-
-
- e.printStackTrace();
- }
- }
- }
复制代码
默认情况下,利用类的属性名作为Excel的列表,固然也可以利用@ExcelProperty 注解来重新指定属性名称。
- @Data
- public class User {
-
-
- @ExcelProperty(value = "用户Id")
- private Integer userId;
-
- @ExcelProperty(value = "姓名")
- private String name;
-
- @ExcelProperty(value = "手机")
- private String phone;
-
- @ExcelProperty(value = "邮箱")
- private String email;
-
- @ExcelProperty(value = "创建时间")
- private Date createTime;
- }
复制代码
表头归并
value在写的时间,如果指定了多个值,会自动举行归并
- @Data
- public class User {
-
-
- @ExcelProperty(value = "用户Id")
- private Integer userId;
-
- @ExcelProperty(value = {
-
- "用户基本信息", "姓名"})
-
复制代码 |