基于EasyExcel和boostrapTable的文件导入和导出

1.需要引入阿里巴巴的EasyExcel的依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>

2.封装简单的导入导出的工具

package com.woniu.erp.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.woniu.erp.entity.Repository;
import org.junit.Test;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class ExcelUtil<E> {

    /**
     *  简单的表格导出工具
     * @param list 导出到表格的数据
     * @param resp 响应对象
     * @param sheetName 工作表名称
     * @return 是否导出成功
     */
    public boolean excelExport(List<E> list, HttpServletResponse resp, String sheetName) throws IOException {
        //如果传入信息不正确,直接返回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;
    }


    /**
     *
     * @param inputStream 前端传输文件的输入流
     * @param clazz 读取表格转换的对象class
     * @return
     */
    public List<E> excelImport(InputStream inputStream,Class<?> clazz){
        return EasyExcel.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<String,Object> importGoods(@RequestParam("file") MultipartFile multipartFile){
        try {
            List<Good> list = excelUtil.excelImport(multipartFile.getInputStream(), Good.class);//1.读取前端的excel文件并转换成list集合
            Map<String,Object> map=new HashMap<>();
            if (list == null || list.size() == 0) {
                map.put("msg", "error");
                map.put("total",0);
                map.put("available",0);
                return map;
            }
            int row = goodsManageService.addGoodsList(list);//2.封装向前端返回的结果
            map.put("total",list.size());
            map.put("msg","success");
            map.put("available",row);
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

《2.导出》

   @RequestMapping("/exportGoods")
    public void exportGoods(String searchType, String keyWord, HttpServletResponse resp){
        Map<String, Object> goodsList = goodsManageService.getGoodsListBycons(searchType, keyWord);//1.根据前端传过来的数据查询相应的list集合
        List<Good> list = (List<Good>) 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;//商品价值

猜你喜欢

转载自www.cnblogs.com/yyk520/p/13380428.html