欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot整合easyExcel实现报表导入导出

程序员文章站 2024-03-20 17:37:16
...

1.引入maven坐标

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

2.编写导出实体类

package com.myservice.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * @ClassName UserEntity
 * @Description TODO
 * @Author kejj
 * @Date 2020/9/26 16:35
 * @Version 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {
	
    @ExcelProperty("用户名") 
    private String userName;
    @ExcelProperty("年龄")
    private int age;
    
    @ExcelProperty("生日")
    @ColumnWidth(30)  //设置行宽
    private Date birthday;
    @ExcelProperty("备注")
    private String desc;


}

3.实现监听器

package com.myservice.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @ClassName UserListerner
 * @Description TODO
 * @Author kejj
 * @Date 2020/9/26 16:39
 * @Version 1.0
 */
@Component
@Scope("prototype")
public class UserListerner extends AnalysisEventListener<UserEntity> {

/**
逻辑处理
*/
    @Override
    public void invoke(UserEntity userEntity, AnalysisContext analysisContext) {
        System.out.println(userEntity);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}

4.对外暴露接口

package com.myservice.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ExportExcel
 * @Description TODO
 * @Author kejj
 * @Date 2020/9/26 16:29
 * @Version 1.0
 */
@Controller
@RequestMapping("/excel")
public class ExportExcel {

    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Map read(MultipartFile file) throws IOException {
        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), UserEntity.class, new UserListerner());
        //此处调用监听器的invoke方法,实现代码逻辑
        read.sheet().doRead();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("respCode","200");
        map.put("desc","");
        return map;
    }

    /**
     * 文件下载
     * @param response
     * @throws IOException
     */
    @RequestMapping("download")
    @ResponseBody
    public  void write(HttpServletResponse response) throws IOException {

        String fileName = new String("用户模板.xlsx".getBytes(), "ISO-8859-1");
        response.addHeader("Content-Disposition", "filename=" + fileName);
        ServletOutputStream out = response.getOutputStream();
        //准备数据
        ArrayList<Object> objects = new ArrayList<>();
        for(int i = 0;i<10;i++){
            objects.add(new UserEntity("张三",13,new Date(),""));
        }
        //导出
        ExcelWriterBuilder write = EasyExcel.write(out, UserEntity.class);
        write.sheet().doWrite(objects);
    }

}

``

相关标签: excel java spring