阿里巴巴EasyExcel官网:链接 导入步骤:
1.导入Excel依赖
2.编写对应的Excel实体类(使用EasyExcel提供的注解)
3.编写controller
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.0-beta2</version>
</dependency>
实体类
假设我的excel表格长这样,对应实体类写法如下
@ExcelProperty("药物名称") 核心注解就是这个,这个注解代表着Excel中的标题,
有几个标题,这里就得写几个注解对应在属性上
package com.cz.pojo;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@TableName("medices")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Medicnes {
@TableId(type = IdType.AUTO)
Integer id;
@ExcelProperty("药物名称")
String medice;
@ExcelProperty("药物来源")
String remark;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
@TableField("ctime")
Date creatTime;
}
controller实现文件导入功能
一行代码即可把Excel中的数据转换成List集合
EasyExcel.read(file.getInputStream()).head(Medicnes.class).sheet().
doReadSync();
@RequestMapping(value = "/importExcel",method = RequestMethod.POST)
public JSONObject importExcel(@RequestParam("file") MultipartFile file) {
try {
List<Medicnes> list = EasyExcel.read(file.getInputStream()).head(Medicnes.class).sheet().doReadSync();
mediceService.insertBatch(list);
return CommonResult.Sucess();
} catch (IOException e) {
log.info("文件导入异常{}",e);
return CommonResult.Failure();
}
}
导出功能
先上工具类ExcelUtils
package com.cz.untils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Slf4j
public class ExcelUtils {
public static void Export(String fileName, Class excelHead, List<T> data, HttpServletResponse response){
ServletOutputStream outputStream = setResponseDownLoad(fileName, response);
ExcelWriterBuilder writeBook = EasyExcel.write(outputStream, excelHead);
ExcelWriterSheetBuilder sheet = writeBook.sheet(fileName);
sheet.doWrite(data);
}
public static ServletOutputStream setResponseDownLoad(String fileName, HttpServletResponse response){
try {
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "max-age=0");
return response.getOutputStream();
} catch (Exception e) {
log.info("设置响应头异常{}",e);
}
return null;
}
}
Controller
对应的参数说明在工具类当中,在浏览器访问即可下载文件,建议使用谷歌浏览器
@RequestMapping(value = "/testDown",method = RequestMethod.GET)
public void testDown(HttpServletResponse response){
ExcelUtils.Export("药物记录表",Medicnes.class,null,response);
}
注意:
请使用浏览器进行下载测试,postman测试会有个小问题,名称显示乱码;
|