
com.github.liaochong.myexcel.core.DefaultStreamExcelBuilder Maven / Gradle / Ivy
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.liaochong.myexcel.core;
import com.github.liaochong.myexcel.core.container.Pair;
import com.github.liaochong.myexcel.core.parser.ParseConfig;
import com.github.liaochong.myexcel.core.parser.Table;
import com.github.liaochong.myexcel.core.parser.Tr;
import com.github.liaochong.myexcel.core.reflect.ClassFieldContainer;
import com.github.liaochong.myexcel.core.strategy.AutoWidthStrategy;
import com.github.liaochong.myexcel.core.strategy.WidthStrategy;
import com.github.liaochong.myexcel.core.templatehandler.TemplateHandler;
import com.github.liaochong.myexcel.utils.ReflectUtil;
import com.github.liaochong.myexcel.utils.TempFileOperator;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* @author liaochong
* @version 1.0
*/
public class DefaultStreamExcelBuilder extends AbstractSimpleExcelBuilder implements SimpleStreamExcelBuilder {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(DefaultStreamExcelBuilder.class);
/**
* 设置需要渲染的数据的类类型
*/
private Class dataType;
/**
* 流工厂
*/
private HtmlToExcelStreamFactory htmlToExcelStreamFactory;
/**
* workbook
*/
private Workbook workbook;
/**
* 待追加excel
*/
private Path excel;
/**
* 任务取消
*/
private volatile boolean cancel;
/**
* 分组
*/
private Class>[] groups;
/**
* 模板处理器
*/
private TemplateHandler templateHandler;
private final List> asyncAppendFutures = new LinkedList<>();
private final HtmlToExcelStreamFactory.HtmlToExcelStreamFactoryContext context = new HtmlToExcelStreamFactory.HtmlToExcelStreamFactoryContext();
private DefaultStreamExcelBuilder(Class dataType) {
this(dataType, (Workbook) null);
}
private DefaultStreamExcelBuilder(Class dataType, Workbook workbook) {
super(false);
this.dataType = dataType;
this.workbook = workbook;
configuration.setWidthStrategy(WidthStrategy.NO_AUTO);
this.isMapBuild = dataType == Map.class;
}
private DefaultStreamExcelBuilder(Class dataType, Path excel) {
super(false);
this.dataType = dataType;
this.excel = excel;
configuration.setWidthStrategy(WidthStrategy.NO_AUTO);
this.isMapBuild = dataType == Map.class;
}
/**
* 获取实例,设定需要渲染的数据的类类型
*
* @param dataType 数据的类类型
* @param T
* @return DefaultStreamExcelBuilder
*/
public static DefaultStreamExcelBuilder of(Class dataType) {
return new DefaultStreamExcelBuilder<>(dataType);
}
/**
* 获取实例,设定需要渲染的数据的类类型
*
* @param dataType 数据的类类型
* @param workbook workbook
* @param T
* @return DefaultStreamExcelBuilder
*/
public static DefaultStreamExcelBuilder of(Class dataType, Workbook workbook) {
return new DefaultStreamExcelBuilder<>(dataType, workbook);
}
/**
* 获取实例,设定需要渲染的数据的类类型
*
* @param dataType 数据的类类型
* @param excel excel
* @param T
* @return DefaultStreamExcelBuilder
*/
public static DefaultStreamExcelBuilder of(Class dataType, Path excel) {
return new DefaultStreamExcelBuilder<>(dataType, excel);
}
/**
* 获取实例,设定需要渲染的数据的类类型
*
* @param dataType 数据的类类型
* @param excelInputStream excelInputStream
* @param T
* @return DefaultStreamExcelBuilder
*/
public static DefaultStreamExcelBuilder of(Class dataType, InputStream excelInputStream) {
return of(dataType, TempFileOperator.convertToFile(excelInputStream));
}
/**
* 已过时,请使用of方法代替
* 4.0版本移除
*
* @return DefaultStreamExcelBuilder
*/
@Deprecated
public static DefaultStreamExcelBuilder