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

如需客户端指定excel版本,只需要判断后缀名然后在controller中的.excelType(ExcelTypeEnum.XLS)做指定输出内容格式即可
***(注意表格行高列宽统一设置是在实体类的类名注解上,如果需要对表格进行精细的宽高设置需要删除掉这两个注解,可以在拦截器使用row的方法进行设置)

1. ## 引入依赖

  1.     <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>2.1.4</version>
    </dependency>
    

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 = {"堆体高度(
  • 4
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
对于复杂自定义表头导出,可以使用EasyExcel的注解`@ExcelProperty`来实现。具体步骤如下: 1. 创建表头实体类,使用`@ExcelProperty`注解来定义表头名称和对应的字段。 ```java public class ComplexHeadData { @ExcelProperty({"主标题", "名称"}) private String name; @ExcelProperty({"主标题", "数量"}) private Integer number; @ExcelProperty({"主标题", "价格"}) private Double price; @ExcelProperty({"次标题", "子名称"}) private String subName; @ExcelProperty({"次标题", "子数量"}) private Integer subNumber; @ExcelProperty({"次标题", "子价格"}) private Double subPrice; // 省略getter和setter方法 } ``` 2. 创建导出数据集合,并将数据集合和表头实体类传入`EasyExcel.write()`方法中,使用`Sheet`对象来设置表头。 ```java List<ComplexHeadData> data = ... Sheet sheet = new Sheet(1, 0, ComplexHeadData.class); // 设置主标题行 List<List<String>> head = new ArrayList<>(); head.add(Arrays.asList("主标题", "名称", "数量", "价格")); // 设置次标题行 head.add(Arrays.asList("次标题", "子名称", "子数量", "子价格")); // 设置表头样式 WriteCellStyle headStyle = new WriteCellStyle(); // ... 设置样式 // 设置主标题行的样式 WriteCellStyle mainHeadStyle = new WriteCellStyle(headStyle); mainHeadStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex()); // 设置次标题行的样式 WriteCellStyle subHeadStyle = new WriteCellStyle(headStyle); subHeadStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); // 设置主标题行和次标题行的样式 WriteCellStyle headCellStyle = new WriteCellStyle(); headCellStyle.setWrapped(true); headCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); headCellStyle.setMainHeadStyle(mainHeadStyle); headCellStyle.setSubHeadStyle(subHeadStyle); // 设置表头 sheet.setHead(head); sheet.setHeadStyle(headCellStyle); // 写入数据到Excel EasyExcel.write(outputStream, ComplexHeadData.class).sheet().doWrite(data); ``` 通过以上步骤,即可实现复杂自定义表头导出。需要注意的是,`@ExcelProperty`注解中的数组参数表示表头的层级关系,数组的第一个元素为主标题,第二个元素为次标题,以此类推。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值