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

com.kangaroohy.plugin.excel.handler.ManySheetWriteHandler Maven / Gradle / Ivy

There is a newer version: 3.1.3
Show newest version
package com.kangaroohy.plugin.excel.handler;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.kangaroohy.plugin.excel.annotation.ResponseExcel;
import com.kangaroohy.plugin.excel.annotation.Sheet;
import com.kangaroohy.plugin.excel.config.ExcelConfigProperties;
import com.kangaroohy.plugin.excel.enhance.WriterBuilderEnhancer;
import com.kangaroohy.plugin.excel.kit.ExcelException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author hy
 * @date 2020/3/29
 */
public class ManySheetWriteHandler extends AbstractSheetWriteHandler {

    public ManySheetWriteHandler(ExcelConfigProperties configProperties,
                                 ObjectProvider>> converterProvider, WriterBuilderEnhancer excelWriterBuilderEnhance) {
        super(configProperties, converterProvider, excelWriterBuilderEnhance);
    }

    /**
     * 当且仅当List不为空且List中的元素也是List 才返回true
     *
     * @param obj 返回对象
     * @return boolean
     */
    @Override
    public boolean support(Object obj) {
        if (obj instanceof List) {
            List objList = (List) obj;
            return !objList.isEmpty() && objList.get(0) instanceof List;
        } else {
            throw new ExcelException("@ResponseExcel 返回值必须为List类型");
        }
    }

    @Override
    public void write(Object obj, HttpServletResponse response, ResponseExcel responseExcel) {
        List objList = (List) obj;
        ExcelWriter excelWriter = getExcelWriter(response, responseExcel);

        Sheet[] sheets = responseExcel.sheets();
        WriteSheet sheet;
        for (int i = 0; i < sheets.length; i++) {
            List eleList = (List) objList.get(i);

            if (CollectionUtils.isEmpty(eleList)) {
                sheet = EasyExcelFactory.writerSheet(responseExcel.sheets()[i].sheetName()).build();
            } else {
                // 有模板则不指定sheet名
                Class dataClass = eleList.get(0).getClass();
                sheet = this.sheet(responseExcel.sheets()[i], dataClass, responseExcel.template(), responseExcel.headGenerator());
            }

            // 填充 sheet
            if (responseExcel.fill()) {
                excelWriter.fill(eleList, sheet);
            } else {
                // 写入sheet
                excelWriter.write(eleList, sheet);
            }
        }

        excelWriter.finish();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy