2424import com .github .liaochong .myexcel .core .strategy .WidthStrategy ;
2525import com .github .liaochong .myexcel .core .templatehandler .TemplateHandler ;
2626import com .github .liaochong .myexcel .utils .ReflectUtil ;
27- import lombok . NonNull ;
27+ import com . github . liaochong . myexcel . utils . TempFileOperator ;
2828import lombok .extern .slf4j .Slf4j ;
2929import org .apache .poi .ss .usermodel .Workbook ;
3030
3131import java .io .IOException ;
32+ import java .io .InputStream ;
33+ import java .nio .file .Files ;
3234import java .nio .file .Path ;
3335import java .util .Arrays ;
3436import java .util .LinkedList ;
@@ -66,6 +68,10 @@ public class DefaultStreamExcelBuilder<T> extends AbstractSimpleExcelBuilder imp
6668 * workbook
6769 */
6870 private Workbook workbook ;
71+ /**
72+ * 待追加excel
73+ */
74+ private Path excel ;
6975 /**
7076 * 文件分割,excel容量
7177 */
@@ -91,10 +97,10 @@ public class DefaultStreamExcelBuilder<T> extends AbstractSimpleExcelBuilder imp
9197 */
9298 private TemplateHandler templateHandler ;
9399
94- private List <CompletableFuture <Void >> asyncAppendFutures = new LinkedList <>();
100+ private final List <CompletableFuture <Void >> asyncAppendFutures = new LinkedList <>();
95101
96102 private DefaultStreamExcelBuilder (Class <T > dataType ) {
97- this (dataType , null );
103+ this (dataType , ( Workbook ) null );
98104 }
99105
100106 private DefaultStreamExcelBuilder (Class <T > dataType , Workbook workbook ) {
@@ -105,14 +111,22 @@ private DefaultStreamExcelBuilder(Class<T> dataType, Workbook workbook) {
105111 this .isMapBuild = dataType == Map .class ;
106112 }
107113
114+ private DefaultStreamExcelBuilder (Class <T > dataType , Path excel ) {
115+ super (false );
116+ this .dataType = dataType ;
117+ this .excel = excel ;
118+ configuration .setWidthStrategy (WidthStrategy .NO_AUTO );
119+ this .isMapBuild = dataType == Map .class ;
120+ }
121+
108122 /**
109123 * 获取实例,设定需要渲染的数据的类类型
110124 *
111125 * @param dataType 数据的类类型
112126 * @param <T> T
113127 * @return DefaultStreamExcelBuilder
114128 */
115- public static <T > DefaultStreamExcelBuilder <T > of (@ NonNull Class <T > dataType ) {
129+ public static <T > DefaultStreamExcelBuilder <T > of (Class <T > dataType ) {
116130 return new DefaultStreamExcelBuilder <>(dataType );
117131 }
118132
@@ -124,10 +138,34 @@ public static <T> DefaultStreamExcelBuilder<T> of(@NonNull Class<T> dataType) {
124138 * @param <T> T
125139 * @return DefaultStreamExcelBuilder
126140 */
127- public static <T > DefaultStreamExcelBuilder <T > of (@ NonNull Class <T > dataType , @ NonNull Workbook workbook ) {
141+ public static <T > DefaultStreamExcelBuilder <T > of (Class <T > dataType , Workbook workbook ) {
128142 return new DefaultStreamExcelBuilder <>(dataType , workbook );
129143 }
130144
145+ /**
146+ * 获取实例,设定需要渲染的数据的类类型
147+ *
148+ * @param dataType 数据的类类型
149+ * @param excel excel
150+ * @param <T> T
151+ * @return DefaultStreamExcelBuilder
152+ */
153+ public static <T > DefaultStreamExcelBuilder <T > of (Class <T > dataType , Path excel ) {
154+ return new DefaultStreamExcelBuilder <>(dataType , excel );
155+ }
156+
157+ /**
158+ * 获取实例,设定需要渲染的数据的类类型
159+ *
160+ * @param dataType 数据的类类型
161+ * @param excelInputStream excelInputStream
162+ * @param <T> T
163+ * @return DefaultStreamExcelBuilder
164+ */
165+ public static <T > DefaultStreamExcelBuilder <T > of (Class <T > dataType , InputStream excelInputStream ) {
166+ return of (dataType , TempFileOperator .convertToFile (excelInputStream ));
167+ }
168+
131169 /**
132170 * 已过时,请使用of方法代替
133171 * 4.0版本移除
@@ -151,22 +189,22 @@ public static DefaultStreamExcelBuilder<Map> getInstance(Workbook workbook) {
151189 return new DefaultStreamExcelBuilder <>(Map .class , workbook );
152190 }
153191
154- public DefaultStreamExcelBuilder <T > titles (@ NonNull List <String > titles ) {
192+ public DefaultStreamExcelBuilder <T > titles (List <String > titles ) {
155193 this .titles = titles ;
156194 return this ;
157195 }
158196
159- public DefaultStreamExcelBuilder <T > sheetName (@ NonNull String sheetName ) {
197+ public DefaultStreamExcelBuilder <T > sheetName (String sheetName ) {
160198 configuration .setSheetName (sheetName );
161199 return this ;
162200 }
163201
164- public DefaultStreamExcelBuilder <T > fieldDisplayOrder (@ NonNull List <String > fieldDisplayOrder ) {
202+ public DefaultStreamExcelBuilder <T > fieldDisplayOrder (List <String > fieldDisplayOrder ) {
165203 this .fieldDisplayOrder = fieldDisplayOrder ;
166204 return this ;
167205 }
168206
169- public DefaultStreamExcelBuilder <T > workbookType (@ NonNull WorkbookType workbookType ) {
207+ public DefaultStreamExcelBuilder <T > workbookType (WorkbookType workbookType ) {
170208 if (workbook != null ) {
171209 throw new IllegalArgumentException ("Workbook type confirmed, not modifiable" );
172210 }
@@ -184,13 +222,13 @@ public DefaultStreamExcelBuilder<T> noStyle() {
184222 return this ;
185223 }
186224
187- public DefaultStreamExcelBuilder <T > widthStrategy (@ NonNull WidthStrategy widthStrategy ) {
225+ public DefaultStreamExcelBuilder <T > widthStrategy (WidthStrategy widthStrategy ) {
188226 configuration .setWidthStrategy (widthStrategy );
189227 return this ;
190228 }
191229
192230 @ Deprecated
193- public DefaultStreamExcelBuilder <T > autoWidthStrategy (@ NonNull AutoWidthStrategy autoWidthStrategy ) {
231+ public DefaultStreamExcelBuilder <T > autoWidthStrategy (AutoWidthStrategy autoWidthStrategy ) {
194232 configuration .setWidthStrategy (AutoWidthStrategy .map (autoWidthStrategy ));
195233 return this ;
196234 }
@@ -298,6 +336,15 @@ public DefaultStreamExcelBuilder<T> start() {
298336 if (head != null ) {
299337 htmlToExcelStreamFactory .appendTitles (head );
300338 }
339+ if (excel != null && Files .exists (excel )) {
340+ log .info ("start reading existing excel data." );
341+ SaxExcelReader <T > reader = SaxExcelReader .of (dataType )
342+ .readAllSheet ();
343+ if (titleLevel > 0 ) {
344+ reader .rowFilter (row -> row .getRowNum () > titleLevel - 1 );
345+ }
346+ reader .readThen (excel .toFile (), (Consumer <T >) this ::append );
347+ }
301348 return this ;
302349 }
303350
0 commit comments