Java中excel的导入和导出By项目

导入:(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;
  }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值