1.需要引入阿里巴巴的EasyExcel的依赖
com.alibaba
easyexcel
2.2.6
2.封装简单的导入导出的工具
packagecom.woniu.erp.util;importcom.alibaba.excel.EasyExcel;importcom.alibaba.excel.ExcelWriter;importcom.alibaba.excel.support.ExcelTypeEnum;importcom.woniu.erp.entity.Repository;importorg.junit.Test;importorg.springframework.stereotype.Component;importjavax.servlet.http.HttpServletResponse;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.List;
@Componentpublic class ExcelUtil{/*** 简单的表格导出工具
*@paramlist 导出到表格的数据
*@paramresp 响应对象
*@paramsheetName 工作表名称
*@return是否导出成功*/
public boolean excelExport(List list, HttpServletResponse resp, String sheetName) throwsIOException {//如果传入信息不正确,直接返回false
if (list == null || list.size() == 0 || resp == null){return false;
}//获取传入数据的class对象
Class> clazz = list.get(0).getClass();//如果工作表名称为空,默认修改为对象名称
if (sheetName == null || "".equals(sheetName)){
sheetName=clazz.getSimpleName();
}//使用EasyExcel导出数据到表格输出到前端
EasyExcel.write(resp.getOutputStream(),clazz).sheet(sheetName).doWrite(list);return true;
}/***
*@paraminputStream 前端传输文件的输入流
*@paramclazz 读取表格转换的对象class
*@return
*/
public List excelImport(InputStream inputStream,Class>clazz){returnEasyExcel.read(inputStream).head(clazz).sheet().doReadSync();
}
}
封装导出工具的原理:
1.导出的原理
方法的形参需要有 1.导出对象的list集合(从后端根据条件通过service查出来的) 2.前端的响应HttpServlrtResponse resp 3.需要导出的excel表名sheetName
<1>首先判断list是否为空,如果不为空则通过list中的对象的到对象的class对象(clazz=list.get(0).getClass())
<2>判断sheetname是否为空,如果sheetname为空的话则将class对象的名称作为表名 sheetname=clazz.getSimpleName()
<3>调用easyexcel中的方法将表格输出到前端,调用链式编程Easy.write(输出流,对象的class对象).sheet(表名).doWrite(要导出的对象集合)
response的作用是将查询出来的list通过输出流输出到浏览器
class对象的作用是保证输出的是这个对象的数据
sheet的作用是确保输出表格的名称
dowrite要输出的集合
前端向后端发送要导出的数据的条件==》后端接受到数据后查询出list集合,然后通过easyexcel的方法将list集合转换成表格并发送到浏览器。
2.导入的原理
方法的形参有 1.输入流对象 2.对象的 class对象
通过easyexcel的链式编程将前端上传的文件的内容转换成对象的list集合,然后再将list集合保存到数据库
3.文件导入和导出的具体实现
《1.导入》
@RequestMapping("/importGoods")public Map importGoods(@RequestParam("file") MultipartFile multipartFile){try{
List list = excelUtil.excelImport(multipartFile.getInputStream(), Good.class);//1.读取前端的excel文件并转换成list集合
Map map=new HashMap<>();if (list == null || list.size() == 0) {
map.put("msg", "error");
map.put("total",0);
map.put("available",0);returnmap;
}int row =goodsManageService.addGoodsList(list);//2.封装向前端返回的结果
map.put("total",list.size());
map.put("msg","success");
map.put("available",row);returnmap;
}catch(Exception e){
e.printStackTrace();
}return null;
}
《2.导出》
@RequestMapping("/exportGoods")public voidexportGoods(String searchType, String keyWord, HttpServletResponse resp){
Map goodsList =goodsManageService.getGoodsListBycons(searchType, keyWord);//1.根据前端传过来的数据查询相应的list集合
List list = (List) goodsList.get("rows");
resp.setHeader("Content-Disposition","attachment;fileName=goods.xlsx");//2.添加文件下载的响应头try{
excelUtil.excelExport(list,resp,null);//3.将list转换成excel表格并传送到浏览器
}catch(IOException e) {
System.out.println("IO异常");
}
}
《3.对象的属性需要加入的注解》
@ExcelProperty(value = "商品id",index = 0)private Integer goodId;//商品id
@ExcelProperty(value = "商品名称",index = 1)private String goodName;//商品名称
@ExcelProperty(value = "商品类型",index = 2)private String goodType;//商品类型
@ExcelProperty(value = "商品尺寸",index = 3)private String goodSize;//商品尺寸
@ExcelProperty(value = "商品价值",index = 4)private Integer goodValue;//商品价值