其他分享
首页 > 其他分享> > easyPOI导出

easyPOI导出

作者:互联网

文档:http://doc.wupaas.com/docs/easypoi/easypoi-1c0u6ksp2r091

1.依赖

        <!-- easypoi的支持 -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>

2.实体类

注解: @Excel(name = "字典id")

name值即为excel标题行

3.controller

@GetMapping("/export/test/{id}")
public ResponseEntity<byte[]> exportProtocol(@PathVariable("id") Integer id, HttpServletRequest request, HttpServletResponse response){
    return testService.export(id,request,response);
}

4.service

public ResponseEntity<byte[]> exportProtocol(Integer protocolId, HttpServletRequest request, HttpServletResponse response) {
    //准备参数集合
    List<A> datas1 = new ArrayList<>();
    List<B> datas3 = new ArrayList<>();
    List<Map<String,Object>> sheetsList = new ArrayList<>();
    //sheet1
    sheetsList.add(makeSheet("sheet页名称1", A.class, datas1));
    //sheet2
    sheetsList.add(makeSheet("sheet页名称2", B.class, datas2));
    //工具类获取Workbook对象
    Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        workbook.write(out);
        String fileName = DownloadUtil.processFileName("测试文件导出-文件名.xls",request,response);
        return DownloadUtil.downloadFile(out,fileName);
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

    /**
     * 封装sheet
     * @param sheetName sheet名称
     * @param pojoClass 实体类
     * @param datas 数据
     * @param <T>
     * @return
     */
    public static <T> Map<String, Object> makeSheet(String sheetName, Class<T> pojoClass, List<T> datas) {
        ExportParams sheet = new ExportParams();
        sheet.setSheetName(sheetName);
        Map<String, Object> map = new HashMap<>();
        map.put("title", sheet);
        map.put("entity", pojoClass);
        map.put("data", datas);
        return map;
    }

5.工具类

package com.test.utils;

import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * Mvc 导出文件
 */
public class DownloadUtil {
    private DownloadUtil() {}

    @SneakyThrows
    public static ResponseEntity<byte[]> downloadFile(File file, String fileName) {
        HttpHeaders headers = new HttpHeaders();
        //通知浏览器以attachment(下载方式)打开
        headers.setContentDispositionFormData("attachment",
                new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        //application/octet-stream : 二进制流数据(最常见的文件下载)。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<>(FileCopyUtils.copyToByteArray(file),
                headers, HttpStatus.CREATED);
    }

    @SneakyThrows
    public static ResponseEntity<byte[]> downloadFile(ByteArrayOutputStream out, String fileName) {
        HttpHeaders headers = new HttpHeaders();
        //通知浏览器以attachment(下载方式)打开
        headers.setContentDispositionFormData("attachment",
                new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        //application/octet-stream : 二进制流数据(最常见的文件下载)。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<>(out.toByteArray(),
                headers, HttpStatus.CREATED);
    }


    public static String processFileName(String fileName, HttpServletRequest request, HttpServletResponse response) {
        String userAgent = request.getHeader("USER-AGENT");
        try {
            if (StringUtils.contains(userAgent, "MSIE")) {
                //IE浏览器
                fileName = URLEncoder.encode(fileName, "UTF8");
            } else if (StringUtils.contains(userAgent, "Mozilla")) {
                //google,火狐浏览器
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } else if(StringUtils.contains(fileName,"Postman")){
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } else {
                fileName = URLEncoder.encode(fileName, "UTF8");
                //其他浏览器
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return fileName;
    }
}

标签:return,String,导出,new,fileName,headers,import,easyPOI
来源: https://www.cnblogs.com/bgtong/p/16502239.html