easyexcel 在 设置标题_使用easyexcel完成复杂表头及标题的导出功能(自定义样式)...

本文介绍了如何使用EasyExcel在Java中完成复杂表头和标题的导出,包括设置内容和头部的行高、列宽,以及使用自定义样式。通过实体类注解来指定单元格内容和样式,并提供了拦截器进行更精细化的设置。此外,还展示了在控制器中如何处理文件输出和内容生成。
摘要由CSDN通过智能技术生成

如需客户端指定excel版本,只需要判断后缀名然后在controller中的.excelType(ExcelTypeEnum.XLS)做指定输出内容格式即可

***(注意表格行高列宽统一设置是在实体类的类名注解上,如果需要对表格进行精细的宽高设置需要删除掉这两个注解,可以在拦截器使用row的方法进行设置)

1.引入依赖

com.alibaba

easyexcel

2.1.4

2.实体类(注解法)

package com.jpxx.admin.pilebody.service.api.dto;

import com.alibaba.excel.annotation.ExcelIgnore;

import com.alibaba.excel.annotation.ExcelProperty;

import com.alibaba.excel.annotation.write.style.ColumnWidth;

import com.alibaba.excel.annotation.write.style.ContentRowHeight;

import com.alibaba.excel.annotation.write.style.HeadRowHeight;

import com.alibaba.excel.util.StringUtils;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.experimental.Accessors;

@Data

@NoArgsConstructor

@Accessors(chain = true)

@ContentRowHeight(45)

@HeadRowHeight(50)

public class PilebodycheckMonthDto {

@ExcelIgnore

private String id;

@ExcelIgnore

private String cityid;

@ExcelIgnore

private String districtid;

@ExcelProperty(value = {"序号","序号"},index = 0)

@ColumnWidth(10)

private String orderNum;

@ExcelProperty(value = {"堆体名称","堆体名称"},index = 1)

@ColumnWidth(15)

private String name;

@ExcelProperty(value = {"具体位置","具体位置"},index = 3)

@ColumnWidth(30)

private String address;

@ExcelProperty(value = {"占地面积(平方)","占地面积(平方)"},index = 4)

@ColumnWidth(15)

private String areastr;

@ExcelProperty(value = {"堆体高度(米)","堆体高度(米)"},index = 5)

@ColumnWidth(10)</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 EasyExcel 导出复杂自定义表头,需要先定义一个类来描述表头的结构。这个类需要继承自 com.alibaba.excel.metadata.BaseRowModel 类,并且需要在类中定义一个 List<List<String>> 类型的变量来存储表头的内容。代码如下: ``` public class ComplexHeadData extends BaseRowModel { // 表头第一行内容 @ExcelProperty(value = {"表头1", "子头1", "子子头1"}, index = 0) private String head1; @ExcelProperty(value = {"表头1", "子头1", "子子头2"}, index = 1) private String head2; // 表头第二行内容 @ExcelProperty(value = {"表头1", "子头2", "子子头1"}, index = 2) private String head3; @ExcelProperty(value = {"表头1", "子头2", "子子头2"}, index = 3) private String head4; // 表头第三行内容 @ExcelProperty(value = {"表头2", "子头1", "子子头1"}, index = 4) private String head5; @ExcelProperty(value = {"表头2", "子头1", "子子头2"}, index = 5) private String head6; // 表头第四行内容 @ExcelProperty(value = {"表头2", "子头2", "子子头1"}, index = 6) private String head7; @ExcelProperty(value = {"表头2", "子头2", "子子头2"}, index = 7) private String head8; // 表头 private List<List<String>> head; public ComplexHeadData() { // 初始化表头结构 head = new ArrayList<>(); head.add(Arrays.asList("表头1", "表头1", "表头1", "表头1")); head.add(Arrays.asList("子头1", "子头1", "子头2", "子头2")); head.add(Arrays.asList("子子头1", "子子头2", "子子头1", "子子头2")); head.add(Arrays.asList("表头2", "表头2", "表头2", "表头2")); head.add(Arrays.asList("子头1", "子头1", "子头2", "子头2")); head.add(Arrays.asList("子子头1", "子子头2", "子子头1", "子子头2")); } public String getHead1() { return head1; } public void setHead1(String head1) { this.head1 = head1; } public String getHead2() { return head2; } public void setHead2(String head2) { this.head2 = head2; } public String getHead3() { return head3; } public void setHead3(String head3) { this.head3 = head3; } public String getHead4() { return head4; } public void setHead4(String head4) { this.head4 = head4; } public String getHead5() { return head5; } public void setHead5(String head5) { this.head5 = head5; } public String getHead6() { return head6; } public void setHead6(String head6) { this.head6 = head6; } public String getHead7() { return head7; } public void setHead7(String head7) { this.head7 = head7; } public String getHead8() { return head8; } public void setHead8(String head8) { this.head8 = head8; } public List<List<String>> getHead() { return head; } public void setHead(List<List<String>> head) { this.head = head; } } ``` 在定义好表头类之后,就可以使用 EasyExcel 提供的 ExcelWriter 类来进行数据导出。在 ExcelWriter 的构造函数中,需要传入一个 OutputStream 类型的参数,用于指定导出的文件输出位置。然后,通过调用 ExcelWriter 的 write 方法,将数据写入到 Excel 文件中。代码如下: ``` // 定义表头数据 ComplexHeadData complexHeadData = new ComplexHeadData(); // 定义导出数据 List<List<Object>> data = new ArrayList<>(); data.add(Arrays.asList("数据1", "数据2", "数据3", "数据4", "数据5", "数据6", "数据7", "数据8")); data.add(Arrays.asList("数据1", "数据2", "数据3", "数据4", "数据5", "数据6", "数据7", "数据8")); // 创建 ExcelWriter 对象 ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX); // 创建工作表对象 WriteSheet writeSheet = EasyExcel.writerSheet().build(); // 写入表头 excelWriter.write(complexHeadData.getHead(), writeSheet); // 写入数据 excelWriter.write(data, writeSheet); // 关闭 ExcelWriter 对象 excelWriter.finish(); ``` 在代码中,需要注意以下几点: 1. 在定义表头类时,需要使用 @ExcelProperty 注解来指定表头的内容和索引位置。 2. 在创建 ExcelWriter 对象时,需要指定输出的文件位置和文件类型。 3. 在创建工作表对象时,可以使用 EasyExcel 提供的 builder 模式来进行配置。 4. 在写入表头和数据时,需要使用 ExcelWriter 的 write 方法,并且需要将表头和数据分别写入两次。 5. 在写入完数据后,需要调用 ExcelWriter 的 finish 方法来关闭 ExcelWriter 对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值