目录
目录
1.导出实现
2.Converter说明
3.easyexcel的注解说明
ExcelProperty
ExcelIgnore
ExcelIgnoreUnannotated
DateTimeFormat
NumberFormat
4.response头中的content-type
1.导出实现
(1).实体类
package com.qst.security.wms.vo.order;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.qst.security.wms.easyExcel.StatusConvert;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.Date;
/**
* @author
* @create 2018-10-22 14:59
**/
@Getter
@Setter
public class ShelfOnExcelVo implements Serializable {
//客户名称
@ExcelProperty(value = "客户名称")
private String customerName;
//商品货号
@ExcelProperty(value = "商品货号")
private String goodsNo;
//商品名称
@ExcelProperty(value = "商品名称")
private String goodsName;
//库位代码
@ExcelProperty(value = "库位代码")
private String locationCode;
//上架任务单号
@ExcelProperty(value = "上架任务单号")
private String taskCode;
//预录单号
@ExcelProperty(value = "预录单号")
private String prerecordCode;
//任务数量
@ExcelProperty(value = "任务数量")
private Integer taskQuantity;
//实际上架数量
@ExcelProperty(value = "实际上架数量")
private Integer submitQuantity;
//状态
@ExcelProperty(value = "状态", converter = StatusConvert.class)
private String status;
//收货到期日期
@DateTimeFormat(value = "yyyy-MM-dd")
@ExcelProperty(value = "收货到期日期")
private Date dueDate;
//上架到期日期
@DateTimeFormat(value = "yyyy-MM-dd")
@ExcelProperty(value = "上架到期日期")
private Date dueDateShelf;
}
使用ExcelProperty注解,value定义导出文件的标题,converter用来进行数据转换,比如说,查询出来的数据是“1”,导出文件中要显示成“是”,就需要自定义数据转换的类进行数据转换。
使用DateTimeFormat注解进行日期的格式转换。
(2).数据转换类
package com.qst.security.wms.easyExcel;
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;
/**
* @author linaibo
* @version 1.0
* Create by 2022/12/22 9:30
*/
public class StatusConvert implements Converter<String> {
@Override
public Class supportJavaTypeKey() {
return String.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return null;
}
@Override
public CellData convertToExcelData(String s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
switch (s) {
case "1":
return new CellData("正常");
case "2":
return new CellData("上架数量大于实际数量");
case "3":
return new CellData("此库位在良品区不存在");
case "4":
return new CellData("料号冲突");
case "5":
return new CellData("效期冲突");
case "6":
return new CellData("客户冲突");
default:
return new CellData("");
}
}
}
(3).导出逻辑
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName= "良品上架明细"+ DateFormatUtils.format(new Date(),"yyyyMMddHHmmSSS")+ ".xlsx";;
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
EasyExcel.write(response.getOutputStream(), ShelfOnExcelVo.class)
.autoCloseStream(Boolean.FALSE)
.sheet("良品上架明细")
.doWrite(listv);
设置好response及文件名称,然后使用write方法将集合中的数据输出到文件中。
2.Converter说明
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.alibaba.excel.converters;
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;
public interface Converter<T> {
// 在java中的类型是什么
Class supportJavaTypeKey();
// 在excel中的类型是什么
CellDataTypeEnum supportExcelTypeKey();
// 将excel的数据类型转为java数据类型
T convertToJavaData(CellData var1, ExcelContentProperty var2, GlobalConfiguration var3) throws Exception;
// 将java的数据类型转为excel数据类型
CellData convertToExcelData(T var1, ExcelContentProperty var2, GlobalConfiguration var3) throws Exception;
}
convert接口中的泛型T指的是实体类中要转换项目的属性,比如说,有个String类型的状态,有“0”,“1”两种情况,要转换成“正常”,“异常”,这时这个泛型就是String。
还需要设置一下要转换项目转换前的类型及转换后的类型,以及应该如何转换。比如说导入的时候“正常”,“异常”应该转换成“0”,“1”,导出的时候“0”,“1”,要转换成“正常”,“异常”。
使用时直接在ExcelProperty注解中指定要使用的转换器就可以了
使用此类型的转换器,如果要转换的项目很多,则需要定义很多转换器。
如果需要对所有项目都进行相同的转化,可以使用全局转换器。
3.easyexcel的注解说明
ExcelProperty
用于匹配excel和实体类的匹配,参数如下:
名称 默认值 描述
value 空 用于匹配excel中的头,必须全匹配,如果有多行头,会匹配最后一行头
order Integer.MAX_VALUE 优先级高于value,会根据order的顺序来匹配实体和excel中数据的顺序
index -1 优先级高于value和order,会根据index直接指定到excel中具体的哪一列
converter 自动选择 指定当前字段用什么转换器,默认会自动选择。读的情况下只要实现com.alibaba.excel.converters.Converter#convertToJavaData(com.alibaba.excel.converters.ReadConverterContext<?>) 方法即可
ExcelIgnore
默认所有字段都会和excel去匹配,加了这个注解会忽略该字段
ExcelIgnoreUnannotated
默认不加ExcelProperty 的注解的都会参与读写,加了不会参与
在类的最上方加上@ExcelIgnoreUnannotated 注解即可;
这个注解的意思是:忽略不加@ExcelProperty的字段进行导出
DateTimeFormat
日期转换,用String去接收excel日期格式的数据会调用这个注解,参数如下:
名称 默认值 描述
value 空 参照java.text.SimpleDateFormat书写即可
use1904windowing 自动选择 excel中时间是存储1900年起的一个双精度浮点数,但是有时候默认开始日期是1904,所以设置这个值改成默认1904年开始
NumberFormat
数字转换,用String去接收excel数字格式的数据会调用这个注解。
名称 默认值 描述
value 空 参照java.text.DecimalFormat书写即可
roundingMode RoundingMode.HALF_UP 格式化的时候设置舍入模式
详细的参照官网及博客
EasyExcel格式化映射注解和样式注解详解_流水武qin的博客-CSDN博客_easyexcel样式注解
关于Easyexcel | Easy Excel
4.response头中的content-type
.doc
application/msword
.dot
application/msword
.docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx
application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm
application/vnd.ms-word.document.macroEnabled.12
.dotm
application/vnd.ms-word.template.macroEnabled.12
.xls
application/vnd.ms-excel
.xlt
application/vnd.ms-excel
.xla
application/vnd.ms-excel
.xlsx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx
application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm
application/vnd.ms-excel.sheet.macroEnabled.12
.xltm
application/vnd.ms-excel.template.macroEnabled.12
.xlam
application/vnd.ms-excel.addin.macroEnabled.12
.xlsb
application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt
application/vnd.ms-powerpoint
.pot
application/vnd.ms-powerpoint
.pps
application/vnd.ms-powerpoint
.ppa
application/vnd.ms-powerpoint
.pptx
application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx
application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx
application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam
application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm
application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm
application/vnd.ms-powerpoint.presentation.macroEnabled.12
.ppsm
application/vnd.ms-powerpoint.slideshow.macroEnabled.12
要根据输出文件的格式,选择正确的类型
有关导入导出,这个博客可以参照一下
JAVA ( EasyExcel 通过模板 导入、导出、下载模板)——亲测有用_七月吃橘子的博客-CSDN博客_easyexcel 模板
文章知识点与官方知识档案匹配,可进一步学习相关知识
————————————————
版权声明:本文为CSDN博主「linab112」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_72167535/article/details/128406798