本文已参与「新人创作礼」活动.一起开启掘金创作之路。
EasyExcel注解
【本文系外部转帖 原文地址 】
阿里出品导入导出表格挺实用的工具,个人感觉比POI更易上手,记录下别人整理的EasyExcel相关注解使用方法
\
11个注解
- @ExcelProperty
- @ColumnWith 列宽
- @ContentFontStyle 文本字体样式
- @ContentLoopMerge 文本合并
- @ContentRowHeight 文本行高度
- @ContentStyle 文本样式
- @HeadFontStyle 标题字体样式
- @HeadRowHeight 标题高度
- @HeadStyle 标题样式
- @ExcelIgnore 忽略项
- @ExcelIgnoreUnannotated 忽略未注解
| 字段注解 | 类注解 |
|---|---|
| @ColumnWith(列宽) | @ColumnWidth(全局列宽) |
| @ExcelProperty(字段配置) | @HeadFontStyle(头样式) |
| @HeadRowHeight(标题高度) | |
| @ContentFontStyle(内容字体样式) | |
| @ContentRowHeight(内容高度) |
@ExcelProperty
必要的一个注解,注解中有三个参数value,index,converter分别代表列明,列序号,数据转换方式
value和index只能二选一,通常不用设置converter
1.value 通过标题文本对应
2.index 通过文本行号对应
3.converter 转换器,通常入库和出库转换使用,如性别入库0和1,出库男和女
public class ImeiEncrypt {
@ExcelProperty(value = "值")
private String valueField;
@ExcelProperty(value = 1,converter =IndustryIdConverter.class)
private String indexField;
@ExcelProperty(value = "值对应和转换器",converter =IndustryIdConverter.class)
private String valueAndConverterField;
}
@ColumnWith
设置列宽度,只有一个参数value,value的单位是字符长度,最大可以设置255个字符,因为一个excel单元格最大可以写入的字符个数就是255个字符。
public class ImeiEncrypt {
@ColumnWidth(value = 18)
private String imei;
}
@ContentFontStyle
用于设置单元格内容字体格式的注解
参数:
| 参数 | 含义 |
|---|---|
| fontName | 字体名称 |
| fontHeightInPoints | 字体高度 |
| italic | 是否斜体 |
| strikeout | 是否设置删除水平线 |
| color | 字体颜色 |
| typeOffset | 偏移量 |
| underline | 下划线 |
| bold | 是否加粗 |
| charset | 编码格式 |
@ContentLoopMerge
用于设置合并单元格的注解
参数:
| 参数 | 含义 |
|---|---|
| eachRow | |
| columnExtend |
@ContentRowHeight
用于设置行高
参数:
| 参数 | 含义 |
|---|---|
| value | 行高,-1代表自动行高 |
@ContentStyle
设置内容格式注解
参数:
| 参数 | 含义 |
|---|---|
| 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 | 设置自动单元格自动大小 |
@HeadFontStyle
用于定制标题字体格式
| 参数 | 含义 |
|---|---|
| fontName | 设置字体名称 |
| fontHeightInPoints | 设置字体高度 |
| italic | 设置字体是否斜体 |
| strikeout | 是否设置删除线 |
| color | 设置字体颜色 |
| typeOffset | 设置偏移量 |
| underline | 设置下划线 |
| charset | 设置字体编码 |
| bold | 设置字体是否加粗 |
@HeadRowHeight
设置标题行行高
| 参数 | 含义 |
|---|---|
| value | 设置行高,-1代表自动行高 |
@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 | 设置自动单元格自动大小 |
@ExcelIgnore
不将该字段转换成Excel
@ExcelIgnoreUnannotated
没有注解的字段都不转换
补充:EasyExcel颜色对照表
NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillPattern 为单元格背景色的填充样式。
NPOI Excel 单元格背景颜色设置方法以及颜色对照表:
ICellStyle style = workbook.CreateCellStyle();``style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;``style.FillPattern = FillPattern.SolidForeground;``ICell cell = workbook.CreateSheet().CreateRow(0).CreateCell(0);``cell.CellStyle = style; |
|---|
颜色对照表
| 颜色 | Class名称 | short |
|---|---|---|
| Black | 8 | |
| Brown | 60 | |
| Olive_Green | 59 | |
| Dark_Green | 58 | |
| Dark_Teal | 56 | |
| Dark_Blue | 18 | |
| Indigo | 62 | |
| Grey_80_PERCENT | 63 | |
| Dark_Red | 16 | |
| Orange | 53 | |
| DARK_YELLOW | 19 | |
| Green | 17 | |
| Teal | 21 | |
| Blue | 12 | |
| Blue_Grey | 54 | |
| Grey_50_PERCENT | 23 | |
| Red | 10 | |
| LIGHT_ORANGE | 52 | |
| LIME | 50 | |
| SEA_GREEN | 57 | |
| AQUA | 49 | |
| LIGHT_BLUE | 48 | |
| VIOLET | 20 | |
| GREY_40_PERCENT | 55 | |
| Pink | 14 | |
| Gold | 51 | |
| Yellow | 13 | |
| BRIGHT_GREEN | 11 | |
| TURQUOISE | 15 | |
| SKY_BLUE | 40 | |
| Plum | 61 | |
| GREY_25_PERCENT | 22 | |
| Rose | 45 | |
| Tan | 47 | |
| LIGHT_YELLOW | 43 | |
| LIGHT_GREEN | 42 | |
| LIGHT_TURQUOISE | 41 | |
| PALE_BLUE | 44 | |
| LAVENDER | 46 | |
| White | 9 | |
| CORNFLOWER_BLUE | 24 | |
| LEMON_CHIFFON | 26 | |
| MAROON | 25 | |
| ORCHID | 28 | |
| CORAL | 29 | |
| ROYAL_BLUE | 30 | |
| LIGHT_CORNFLOWER_BLUE | 31 | |
| AUTOMATIC | 64EasyExcel注解 |
【本文系外部转帖 原文地址 】
阿里出品导入导出表格挺实用的工具,个人感觉比POI更易上手,记录下别人整理的EasyExcel相关注解使用方法
\
11个注解
- @ExcelProperty
- @ColumnWith 列宽
- @ContentFontStyle 文本字体样式
- @ContentLoopMerge 文本合并
- @ContentRowHeight 文本行高度
- @ContentStyle 文本样式
- @HeadFontStyle 标题字体样式
- @HeadRowHeight 标题高度
- @HeadStyle 标题样式
- @ExcelIgnore 忽略项
- @ExcelIgnoreUnannotated 忽略未注解
| 字段注解 | 类注解 |
|---|---|
| @ColumnWith(列宽) | @ColumnWidth(全局列宽) |
| @ExcelProperty(字段配置) | @HeadFontStyle(头样式) |
| @HeadRowHeight(标题高度) | |
| @ContentFontStyle(内容字体样式) | |
| @ContentRowHeight(内容高度) |
@ExcelProperty
必要的一个注解,注解中有三个参数value,index,converter分别代表列明,列序号,数据转换方式
value和index只能二选一,通常不用设置converter
1.value 通过标题文本对应
2.index 通过文本行号对应
3.converter 转换器,通常入库和出库转换使用,如性别入库0和1,出库男和女
public class ImeiEncrypt {
@ExcelProperty(value = "值")
private String valueField;
@ExcelProperty(value = 1,converter =IndustryIdConverter.class)
private String indexField;
@ExcelProperty(value = "值对应和转换器",converter =IndustryIdConverter.class)
private String valueAndConverterField;
}
@ColumnWith
设置列宽度,只有一个参数value,value的单位是字符长度,最大可以设置255个字符,因为一个excel单元格最大可以写入的字符个数就是255个字符。
public class ImeiEncrypt {
@ColumnWidth(value = 18)
private String imei;
}
@ContentFontStyle
用于设置单元格内容字体格式的注解
参数:
| 参数 | 含义 |
|---|---|
| fontName | 字体名称 |
| fontHeightInPoints | 字体高度 |
| italic | 是否斜体 |
| strikeout | 是否设置删除水平线 |
| color | 字体颜色 |
| typeOffset | 偏移量 |
| underline | 下划线 |
| bold | 是否加粗 |
| charset | 编码格式 |
@ContentLoopMerge
用于设置合并单元格的注解
参数:
| 参数 | 含义 |
|---|---|
| eachRow | |
| columnExtend |
@ContentRowHeight
用于设置行高
参数:
| 参数 | 含义 |
|---|---|
| value | 行高,-1代表自动行高 |
@ContentStyle
设置内容格式注解
参数:
| 参数 | 含义 |
|---|---|
| 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 | 设置自动单元格自动大小 |
@HeadFontStyle
用于定制标题字体格式
| 参数 | 含义 |
|---|---|
| fontName | 设置字体名称 |
| fontHeightInPoints | 设置字体高度 |
| italic | 设置字体是否斜体 |
| strikeout | 是否设置删除线 |
| color | 设置字体颜色 |
| typeOffset | 设置偏移量 |
| underline | 设置下划线 |
| charset | 设置字体编码 |
| bold | 设置字体是否加粗 |
@HeadRowHeight
设置标题行行高
| 参数 | 含义 |
|---|---|
| value | 设置行高,-1代表自动行高 |
@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 | 设置自动单元格自动大小 |
@ExcelIgnore
不将该字段转换成Excel
@ExcelIgnoreUnannotated
没有注解的字段都不转换
补充:EasyExcel颜色对照表
NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillPattern 为单元格背景色的填充样式。
NPOI Excel 单元格背景颜色设置方法以及颜色对照表:
ICellStyle style = workbook.CreateCellStyle();``style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;``style.FillPattern = FillPattern.SolidForeground;``ICell cell = workbook.CreateSheet().CreateRow(0).CreateCell(0);``cell.CellStyle = style; |
|---|
颜色对照表
| 颜色 | Class名称 | short |
|---|---|---|
| Black | 8 | |
| Brown | 60 | |
| Olive_Green | 59 | |
| Dark_Green | 58 | |
| Dark_Teal | 56 | |
| Dark_Blue | 18 | |
| Indigo | 62 | |
| Grey_80_PERCENT | 63 | |
| Dark_Red | 16 | |
| Orange | 53 | |
| DARK_YELLOW | 19 | |
| Green | 17 | |
| Teal | 21 | |
| Blue | 12 | |
| Blue_Grey | 54 | |
| Grey_50_PERCENT | 23 | |
| Red | 10 | |
| LIGHT_ORANGE | 52 | |
| LIME | 50 | |
| SEA_GREEN | 57 | |
| AQUA | 49 | |
| LIGHT_BLUE | 48 | |
| VIOLET | 20 | |
| GREY_40_PERCENT | 55 | |
| Pink | 14 | |
| Gold | 51 | |
| Yellow | 13 | |
| BRIGHT_GREEN | 11 | |
| TURQUOISE | 15 | |
| SKY_BLUE | 40 | |
| Plum | 61 | |
| GREY_25_PERCENT | 22 | |
| Rose | 45 | |
| Tan | 47 | |
| LIGHT_YELLOW | 43 | |
| LIGHT_GREEN | 42 | |
| LIGHT_TURQUOISE | 41 | |
| PALE_BLUE | 44 | |
| LAVENDER | 46 | |
| White | 9 | |
| CORNFLOWER_BLUE | 24 | |
| LEMON_CHIFFON | 26 | |
| MAROON | 25 | |
| ORCHID | 28 | |
| CORAL | 29 | |
| ROYAL_BLUE | 30 | |
| LIGHT_CORNFLOWER_BLUE | 31 | |
| AUTOMATIC | 64 |