All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xinjump.easyexcel.helper.AbstractEasyExcel Maven / Gradle / Ivy

The newest version!
package com.xinjump.easyexcel.helper;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.xinjump.base.util.collect.ListUtils;
import com.xinjump.base.util.function.CiFunction;
import com.xinjump.easyexcel.event.AnalysisEventAutoListener;
import com.xinjump.easyexcel.event.ExcelManager;
import com.xinjump.easyexcel.exceptions.ExcelException;
import com.xinjump.easyexcel.write.model.WriteSheetModel;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@SuppressWarnings({"rawtypes"})
public abstract class AbstractEasyExcel extends EasyExcelFactory {

    protected final List writeSheetModels = ListUtils.newArrayList();
    protected final List writeHandlers = ListUtils.newArrayList();
    protected final List converters = ListUtils.newArrayList();

    public void doWrite(HttpServletResponse response, String fileName) {
        this.doWrite(response, fileName, ExcelTypeEnum.XLSX);
    }

    public void doWrite(HttpServletResponse response, String fileName, ExcelTypeEnum excelTypeEnum) {
        ExcelWriter excelWriter = this.excelWriter(response, fileName, excelTypeEnum);
        for (WriteSheetModel writeSheetModel : writeSheetModels) {
            ExcelWriterSheetBuilder excelWriterSheetBuilder = writeSheetModel.getExcelWriterSheetBuilder();
            for (WriteHandler writeHandler : writeHandlers) {
                excelWriterSheetBuilder.registerWriteHandler(writeHandler);
            }
            for (Converter converter : converters) {
                excelWriterSheetBuilder.registerConverter(converter);
            }
            excelWriter.write(writeSheetModel.getData(), excelWriterSheetBuilder.build());
        }
        excelWriter.finish();
    }

    protected  void buildWrite(ExcelWriterSheetBuilder excelWriterSheetBuilder, List data) {
        writeSheetModels.add(WriteSheetModel.builder().excelWriterSheetBuilder(excelWriterSheetBuilder).data(data).build());
    }

    public  T getEventListener(String type, HttpServletResponse response) {
        return ExcelManager.getEventListener(type, response);
    }

    private ExcelWriter excelWriter(HttpServletResponse response, String fileName, ExcelTypeEnum excelTypeEnum) {
        return write(this.OUTPUT_STREAM.apply(response, fileName, excelTypeEnum)).build();
    }

    private final CiFunction OUTPUT_STREAM = (response, fileName, excelTypeEnum) -> {
        try {
            String finalFileName = URLEncoder.encode((fileName + excelTypeEnum.getValue()), "UTF-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Expires", "0");
            response.setHeader("Content-Disposition", "attachment;filename=" + finalFileName);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            return response.getOutputStream();
        } catch (IOException e) {
            throw new ExcelException("获取文件失败");
        }
    };

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy