导入:(read)
Maven:easypoi
<!-- easy poi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
用于导入的实体类:
注意:给每个字段的注解(@excel)中的name需要和表中的字段一一对应,如果传就来的表中没有和这个name对应上,那便会没有数据,所以一般是我们提供给需要导入的用户模板,严格按照我们的模板的字段来填写导入内容。
@Data
@Api("用于导入excle表格的实体类")
public class ShopSignExcel {
@Excel(name = "店铺id")
private String shopId;
@Excel(name = "城市")
private String cname;
@Excel(name = "店铺名称")
private String name;
}
实现类:
实现类中使用下面的短短两行代码就可以获取到excel文件中的数据,文件装在ShopSignExcel对象的list中,后续要对这些数据进行各种业务处理,然后再存到你想存入的对象中·····
public ApiResult<?> importData(MultipartFile file) {//前端传来的需要导入的excel的文件
ImportParams importParams = new ImportParams();//easypoi中自带的对象
List<ShopSignExcel> list =
ExcelImportUtil.importExcel(file.getInputStream(), ShopSignExcel.class, importParams);//ShopSignExcel是用来装导入的数据对象
}
导出:(write)
我好傻逼···明明一个easyexcel能解决的事情,我还引入了两个maven····
package com.orangeonline.common.workorder.excel;
import com.alibaba.excel.EasyExcel;
import com.orangeonline.common.shop.vo.ShopListVO;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
public class ShopExcelWrite {
public static void simpleWriteXlsx(List<ShopListVO> shopList, HttpServletResponse response) {
try {
// 设置文件名和文件类型
String fileName = "门店表.xlsx";
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
// 将文件内容写入响应流
EasyExcel.write(response.getOutputStream(), ShopListVO.class)
.sheet("门店表")
.doWrite(data(shopList));
} catch (Exception e) {
throw new RuntimeException("下载文件失败", e);
}
}
private static List<ShopListVO> data(List<ShopListVO> shopList) {
List<ShopListVO> list = new ArrayList<>();
for (int i = 0; i < shopList.size(); i++) {
ShopListVO shopFor = shopList.get(i);
ShopListVO shop = new ShopListVO();
shop.setExcelId(i);
shop.setCityDesc(shopFor.getCityDesc());
shop.setSalePullWechatDate(shopFor.getSalePullWechatDate());
shop.setModel(shopFor.getModel());
shop.setMeituanShopid(shopFor.getMeituanShopid());
shop.setName(shopFor.getName());
shop.setServiceDay(shopFor.getServiceDay());
shop.setCooperateBeginDate(shopFor.getCooperateBeginDate());
shop.setCooperateEndDate(shopFor.getCooperateEndDate());
shop.setOperateTime(shopFor.getOperateTime());
shop.setOperateManagerName(shopFor.getOperateManagerName());
shop.setSaleManagerName(shopFor.getSaleManagerName());
list.add(shop);
}
return list;
}
}
839






