EasyPOI(三)将Excel打成压缩包批量导出

EasyPOI 是一款Excel导入/导出操作的工具包,特点在于减少Java代码。

业务场景

需求:

将模板生成的Excel文件打成压缩包导出。

要求:

  • 不能在服务器生成冗余临时文件;

  • 程序打成jar包执行时,可以准确找到Excel模板文件。

1. Service

import org.springframework.http.ResponseEntity;

/**
 * <p> @Title ExportService
 * <p> @Description 导出测试Service
 *
 * @author ACGkaka
 * @date 2020/9/15 15:10
 */
public interface ExportService {

    /**
     * 通过字节流导出文件
     *
     * @return ResponseEntity
     */
    ResponseEntity exportByByte();

}

2. ServiceImpl

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * <p> @Title ExportServiceImpl
 * <p> @Description 导出测试ServiceImpl
 *
 * @author ACGkaka
 * @date 2020/9/15 15:11
 */
@Service
@AllArgsConstructor
public class ExportServiceImpl {

    UserMapper userMapper;

    @Override
    public ResponseEntity exportByByte() {
        List<User> list = userMapper.queryAll();

        Map<String, Object> map = new HashMap<>();
        map.put("entityList", list);

        // 导出, 模板文件在 resources/file/demo 下
        // 这样存储的好处是, 如果打成jar包不影响模板文件的定位
        TemplateExportParams params = new TemplateExportParams("file/demo/" + "excel_template.xlsx");
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        // 字节缓冲区, 是内存读写流, 不同于指向硬盘的流, 字节数组是成员变量, 当数组不再使用的时候, GC会自动回收, 不用手动关闭流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            workbook.write(out);
            // 打压缩包
            out = zip(new ByteArrayInputStream(out.toByteArray()), "test.xlsx");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", URLEncoder.encode("导出文件.xlsx", "utf-8"));
            return new ResponseEntity<>(out.toByteArray(), headers, HttpStatus.CREATED);
        } catch (IOException e) {
            throw new RuntimeException("临时文件写入失败");
        }
    }

    /**
     * 打压缩包
     *
     * @param in                        要打包的文件流
     * @param fileName                  要打包的文件名
     */
    public static ByteArrayOutputStream zip(InputStream in, String fileName) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (ZipOutputStream zipOut = new ZipOutputStream(out)) {
            zipOut.putNextEntry(new ZipEntry(fileName));
            IOUtils.copy(in, zipOut);
        } catch (IOException e) {
            throw new RuntimeException("打包异常: " + e.getMessage());
        }
        return out;
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿放下技术的小赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值