JAVA实现easyExcel模版导出

easyExcel文档

模板注意:

  • 用 {} 来表示你要用的变量 ,如果本来就有"{“,”}" ,特殊字符用"{“,”}"代替
  • {} 代表普通变量
  • {.}代表是list的变量

添加pom依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.6</version>
</dependency>

<!--工具类-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.21</version>
</dependency>

第一种:简单模版导出

实体类

package com.example.mybatismysql8demo.excel;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;

@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Data
public class TemplateGoodsExcel implements Serializable {
   

    private String goodsName;

    private Integer num;

    private BigDecimal price;

    public TemplateGoodsExcel(String goodsName, BigDecimal price, Integer num) {
   
        this.goodsName = goodsName;
        this.price = price;
        this.num = num;
    }

}

导出模版
在这里插入图片描述

执行方法

package com.example.mybatismysql8demo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.example.mybatismysql8demo.excel.TemplateGoodsExcel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax
文件时,如何设置单元格样式? A:使用easyexcel模板导出Excel文件时,可以通过设置注解@ExcelProperty的style属性来指定对应单元格的样式。具体操作如下: 1. 定义样式类,继承自com.alibaba.excel.write.style.AbstractCellStyleStrategy,重写setCellStyle方法,对单元格样式进行设置。示例代码如下: ``` public class CustomCellStyleStrategy extends AbstractCellStyleStrategy { private CellStyle cellStyle; public CustomCellStyleStrategy(Workbook workbook) { this.cellStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontName("微软雅黑"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); } @Override protected CellStyle setCellStyle(Cell cell, Head head, Integer integer, Integer integer1) { return cellStyle; } } ``` 2. 在需要导出的实体类中,为需要定制样式的属性添加@ExcelProperty注解,设置style属性为对应的样式类。示例代码如下: ``` public class User { @ExcelProperty(value = "姓名", index = 0, style = CustomCellStyleStrategy.class) private String name; @ExcelProperty(value = "年龄", index = 1) private Integer age; // ... } ``` 在注解中,value属性指定了单元格的标题,index属性指定了导出的顺序,style属性指定了样式类。 通过以上步骤,即可设置单元格样式。需要注意的是,样式类需要在导出Excel时传入,如下示例代码: ``` List<User> userList = new ArrayList<>(); // ... 添加用户数据 // 构造导出参数对象 WriteWorkbook writeWorkbook = EasyExcel.write(fileName).build(); WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").head(User.class).registerWriteHandler(new CustomCellStyleStrategy(writeWorkbook.getWorkbook())).build(); // 执行导出 EasyExcel.write(fileName, User.class).sheet("Sheet1").build().write(userList); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值