SpringBoot集成easyExcel导入导出Excel文件

本文详细介绍使用EasyExcel进行Excel导出的全过程,包括引入依赖、前后端代码实现及各种实用注解配置方法,如标题设置、列宽调整、字体样式定制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入依赖

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

前台代码

<a href="${ctx}/itemCategory/daoExcel">
	导出Excel
</a>

sql语句

<select id="list" resultType="ItemCategory">
    select * from item_category
</select>

注解详解

定义标题名称

@ExcelProperty
后端实现

直接在实体类字段上添加注解,设置标题名称。value可以忽略不写。

//设置标题为类目名称
@ExcelProperty(value="类目名称")
private String name;
前端页面

在这里插入图片描述

设置大标题(合并)

@ExcelProperty
后端实现

直接在字段上添加注解

@ExcelProperty({"大标题","ID"})
private Integer id;

@ExcelProperty({"大标题","类目名称"})
private String name;

@ExcelProperty({"大标题","父类ID"})
private Integer pid;

@ExcelProperty({"大标题","是否已删除"})
private Integer isDelete;
前端页面

在这里插入图片描述

设置标题所在的列数

@ExcelProperty(index= )
后端实现

直接在实体类字段上添加注解,设置标题名称。value不可忽略,必须写。

//设置标题为第一列
@ExcelProperty(value="类目名称",index=0)
private String name;
前端页面

在这里插入图片描述

设置列的长度

@ColumnWith
后端实现

直接在实体类字段上添加注解,设置列长

//设置列长为100
@ColumnWidth(100)
private String name;
前端页面

在这里插入图片描述

设置标题高度

@HeadRowHeight
后端实现

直接在实体类上添加注解,设置标题的高度

//设置标题高度为50
@HeadRowHeight(50)
public class ItemCategory implements Serializable {}
前端页面

在这里插入图片描述

设置标题背景颜色

@HeadStyle
后端实现

直接在实体类上添加注解,设置标题的背景颜色

// 头背景设置成红色 IndexedColors.RED.getIndex()
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10)
public class ItemCategory implements Serializable {}
前端页面

在这里插入图片描述

设置文本行背景颜色

@ContentStyle
后端实现

直接在实体类上添加注解,设置内容的高度

// 内容的背景设置成绿色 IndexedColors.GREEN.getIndex() 
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 17)
public class ItemCategory implements Serializable { }
前端页面

在这里插入图片描述

设置某列标题下内容的背景颜色

后端实现

直接在字段上添加注解,设置内容的背景颜色

// 字符串的内容的背景设置成天蓝 IndexedColors.SKY_BLUE.getIndex()
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 40)
private String name;
前端页面

在这里插入图片描述

设置标题字体样式

设置标题字体颜色

@HeadFontStyle
后端实现

直接在实体类上添加注解,设置标题的字体颜色

//设置标题颜色为青色
@HeadFontStyle(color = 11)
public class ItemCategory implements Serializable { }
前端页面

在这里插入图片描述

设置单个标题字体颜色

@HeadFontStyle
后端实现

直接在字段上添加注解,设置单个标题的字体颜色

//设置标题颜色为蓝色
@HeadFontStyle(color = 12)
private String name;
前端页面

在这里插入图片描述

设置标题字体大小

@HeadFontStyle

直接在实体类上添加注解,设置标题的字体大小

//设置标题字体大小为30
@HeadFontStyle(fontHeightInPoints=30)
public class ItemCategory implements Serializable { }
前端页面

在这里插入图片描述

设置单个标题字体大小

@HeadFontStyle

直接在字段上添加注解,设置标题的字体大小

//设置标题字体大小为30
@HeadFontStyle(fontHeightInPoints=30)
private String name;
前端页面

在这里插入图片描述

文本内容样式

设置所有列标题下内容的字体大小

@ContentFontStyle
后端实现

直接在实体类上添加注解,设置内容的字体大小

// 字符串的内容字体设置成30
@ContentFontStyle(fontHeightInPoints = 30)
public class ItemCategory implements Serializable {}
前端页面

在这里插入图片描述

设置某列标题下内容的字体大小

@ContentFontStyle
后端实现

直接在字段上添加注解,设置内容的字体大小

// 字符串的内容字体设置成30
@ContentFontStyle(fontHeightInPoints = 30)
private String name;
前端页面

在这里插入图片描述

文本行高度

@ContentRowHeight
后端实现

直接在实体类上添加注解,设置内容的高度

//设置文本行的高度为60
@ContentRowHeight(60)
public class ItemCategory implements Serializable { }
前端页面

在这里插入图片描述

设置标题字体样式

@HeadFontStyle
后端实现

直接在实体类或字段上添加注解

//设置标题字体为宋体,斜体
@HeadFontStyle(fontName = "宋体",italic = true)
public class ItemCategory implements Serializable {
前端页面

在这里插入图片描述

参数详解
参数含义
fontName设置字体名称(宋体,微软雅黑)
fontHeightInPoints设置字体高度
italic设置字体是否斜体
strikeout是否设置删除线
color设置字体颜色
typeOffset设置偏移量
underline设置下划线(可设置值 1或2,代表下划线条数)
charset设置字体编码
bold设置字体是否加粗

内容居中

@ContentRowHeight
后端实现

直接在实体类上添加注解,设置居中

//设置居中
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER)
public class ItemCategory implements Serializable { }
前端页面

在这里插入图片描述

单列居中
后端实现

直接在实体类上添加注解,设置居中

//设置居中
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER)
private String name;
前端页面

在这里插入图片描述

忽略某个字段

@ExcelIgnore
后端实现

直接在字段上添加注解

//导出Excel文件没有此字段,不显示
@ExcelIgnore
private String name;

设置边框

@ContentStyle
后端实现

直接在实体类上添加注解

/**
 * horizontalAlignment  设置居中
 * borderTop   设置上边框样式
 * borderBottom   设置下边框样式
 * borderLeft   设置左边框样式
 * borderRight   设置右边框样式
 */
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER,
        borderTop = BorderStyle.THIN,
        borderBottom = BorderStyle.THIN,
        borderLeft = BorderStyle.THIN,
        borderRight = BorderStyle.THIN)
public class ItemCategory implements Serializable {
前端页面

在这里插入图片描述

BorderStyle方法详解
参数含义
NONE无边框(默认)
THIN普通实线
MEDIUM中粗实线
DASHED虚线
DOTTED点虚线
THICK大粗实线
DOUBLE双实线
HAIR虚线
MEDIUM_DASHED粗虚线
MEDIUM_DASH_DOT点、线组合
DASH_DOT_DOT点、点、线组合
MEDIUM_DASH_DOT_DOT点、点、线(加粗组合)
SLANTED_DASH_DOT点、线(模糊组合)

设置字段输出格式

后端实现

直接在字段上添加注解

//excel 用年月日的格式
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
@ExcelProperty("日期标题")
private Date date;

//我想写到excel 用百分比表示
@NumberFormat("#.##%")
@ExcelProperty(value = "数字标题")
private Double doubleData;

自定义Converter转换器

转换前:

在这里插入图片描述

创建转换包 converter
后端实现
创建转换器
package com.javapandeng.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * 自定义转换器
 * 0 未删除
 * 1 已删除
 */
public class GenderConverter implements Converter<Integer> {

    @Override
    public Class supportJavaTypeKey() {
        //对象属性类型(java中数据类型)
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        //CellData属性类型(Excel中数据类型)
        return CellDataTypeEnum.STRING;
    }

    /**
     * Excel数据导入到数据库
     * @param cellData
     * @param excelContentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return "已删除".equals(cellData.getStringValue()) ? 1:0;
    }

    /**
     * 数据导出到Excel
     * @param integer 数据库中属性的值
     * @param excelContentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public CellData convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {

        if (integer.equals(0)){
            return new CellData("未删除");
        }else if(integer.equals(1)){
            return new CellData("已删除");
        }
        return new CellData("不详");

        //如果参数只有两个采用三目运算很方便
        //return new CellData(integer.equals(1) ? "已删除" : "未删除");
    }

}
修改实体类

注解 @ExcelProperty 后添加属性 converter = GenderConverter.class

public class ItemCategory implements Serializable {

    /**
    * 是否已删除
    */
    //设置列长
    @ColumnWidth(30)
    //设置标题名称
    @ExcelProperty(value = {"商品类目表","是否已删除"},converter = GenderConverter.class)
    private Integer isDelete;
    
}
转换后页面:

在这里插入图片描述

实体类

注解@ExcelProperty(“类目名称”) //括号中内容为生成Excel文件的表头信息

/**
 * 类目
 */
// 标题背景设置成蓝色 IndexedColors.RED.getIndex()
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 71)
//设置标题高度
@HeadRowHeight(40)

/**
 * horizontalAlignment  设置居中
 * borderTop   设置上边框样式
 * borderBottom   设置下边框样式
 * borderLeft   设置左边框样式
 * borderRight   设置右边框样式
 */
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER,
        borderTop = BorderStyle.THIN,
        borderBottom = BorderStyle.THIN,
        borderLeft = BorderStyle.THIN,
        borderRight = BorderStyle.THIN)

//设置标题字体和斜体
@HeadFontStyle(fontName = "宋体",italic = true)
//设置内容字体和下划线
//@ContentFontStyle(fontName = "微软雅黑")
//设置文本内容高度
@ContentRowHeight(20)

public class ItemCategory implements Serializable {

    /**
     * 主键id
     */
    //设置列长
    @ColumnWidth(10)
    //设置标题名称
    @ExcelProperty({"商品类目表","ID"})
    private Integer id;

    /**
     * 类目名称
     */
    //设置列长
    @ColumnWidth(20)
    //设置标题名称
    @ExcelProperty({"商品类目表","类目名称"})
    @HeadFontStyle(fontName = "微软雅黑",italic = true,underline = 3)
    //设置内容字体和下划线
    //@ContentFontStyle(fontName = "微软雅黑",underline = 2)
    private String name;

    /**
     * 父id
     */
    //设置列长
    @ColumnWidth(10)
    //设置标题名称
    @ExcelProperty({"商品类目表","父类ID"})
    private Integer pid;

    /**
     * 是否已删除
     */
    //设置列长
    @ColumnWidth(30)
    //设置标题名称
    @ExcelProperty(value = {"商品类目表","是否已删除"},converter = GenderConverter.class)
    private Integer isDelete;

Controller层

//文件生成地址
String PATH = "D://";
//导出Excel
//大文件使用生成Excel (SXSSFWorkbook)
@RequestMapping("/daoExcel")
public String simpleWrite1() {

    //查询所有类目
    List<ItemCategory> list = itemCategoryService.list();

    String fileName = PATH + "成绩表.xlsx";
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    // 如果这里想使用03 则 传入excelType参数即可
    EasyExcel.write(fileName, ItemCategory.class).sheet("模板sheet").doWrite(list);

    System.out.println("数据导出成功");

    return "redirect:/itemCategory/findAll";

}

@ContentStyle中的所有参数

@HeadStyle 也可用
参数含义
dataFormat日期格式
hidden设置单元格使用此样式隐藏
locked设置单元格使用此样式锁定
quotePrefix在单元格前面增加`符号,数字或公式将以字符串形式展示
horizontalAlignment设置是否水平居中
wrapped设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见
verticalAlignment设置是否垂直居中
rotation设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180°
indent设置单元格中缩进文本的空格数
borderLeft设置左边框的样式
borderRight设置右边框样式
borderTop设置上边框样式
borderBottom设置下边框样式
leftBorderColor设置左边框颜色
rightBorderColor设置右边框颜色
topBorderColor设置上边框颜色
bottomBorderColor设置下边框颜色
fillPatternType设置填充类型
fillBackgroundColor设置背景色
fillForegroundColor设置前景色
shrinkToFit设置自动单元格自动大小

注解 fillForegroundColor 对应的颜色值

在这里插入图片描述



       每个人的内心深处都会保留着一份悲观色彩,只是因为现实的残酷,人心的难测,所以才会用层层面具遮盖起自己真实的一面,没有人可以从容的面对一切经历,也没有人可以淡定的去处理一切遭遇。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值