4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus;
17 |
18 | import io.github.biezhi.excel.plus.exception.ReaderException;
19 | import io.github.biezhi.excel.plus.reader.ReaderFactory;
20 | import io.github.biezhi.excel.plus.types.Result;
21 | import io.github.biezhi.excel.plus.util.StringUtil;
22 |
23 | import java.io.File;
24 | import java.io.InputStream;
25 | import java.nio.charset.Charset;
26 | import java.nio.charset.StandardCharsets;
27 | import java.util.List;
28 | import java.util.stream.Stream;
29 |
30 | import static java.util.stream.Collectors.toList;
31 |
32 | /**
33 | * ExcelPlus Reader
34 | *
35 | * Used to read Excel documents
36 | *
37 | * @author biezhi
38 | * @date 2018-12-11
39 | */
40 | public class Reader {
41 |
42 | /**
43 | * Java entity type to which the Excel row is mapped
44 | */
45 | private Class modelType;
46 |
47 | /**
48 | * The index of the sheet to be read, default is 0
49 | */
50 | private int sheetIndex;
51 |
52 | /**
53 | * Sheet name to be read, sheet does not take effect if sheet is configured
54 | */
55 | private String sheetName;
56 |
57 | /**
58 | * Reading data from the first few lines should start with an index that really has data,
59 | * otherwise an error will occur. please confirm that the parameter is correct when reading.
60 | *
61 | * Index starts at 0 instead of 1.
62 | */
63 | private int startRow = 2;
64 |
65 | /**
66 | * Read a excel row from a file
67 | */
68 | private File fromFile;
69 |
70 | /**
71 | * Read a excel row from a InputStream
72 | */
73 | private InputStream fromStream;
74 |
75 | private Charset charset = StandardCharsets.UTF_8;
76 |
77 | public Reader(Class modelType) {
78 | this.modelType = modelType;
79 | }
80 |
81 | public static Reader create(Class modelType) {
82 | return new Reader<>(modelType);
83 | }
84 |
85 | public static Reader create(Class modelType, File fromFile) {
86 | return new Reader<>(modelType).from(fromFile);
87 | }
88 |
89 | public static Reader create(Class modelType, InputStream fromStream) {
90 | return new Reader<>(modelType).from(fromStream);
91 | }
92 |
93 | /**
94 | * Read row data from an Excel file
95 | *
96 | * @param fromFile excel file object
97 | * @return Reader
98 | */
99 | public Reader from(File fromFile) {
100 | if (null == fromFile || !fromFile.exists()) {
101 | throw new IllegalArgumentException("excel file must be exist");
102 | }
103 | this.fromFile = fromFile;
104 | return this;
105 | }
106 |
107 | /**
108 | * Read row data from an InputStream
109 | *
110 | * @param fromStream excel InputStream
111 | * @return Reader
112 | */
113 | public Reader from(InputStream fromStream) {
114 | this.fromStream = fromStream;
115 | return this;
116 | }
117 |
118 | /**
119 | * Set the reading from the first few lines, the index starts from 0
120 | *
121 | * @param startRow start row index
122 | * @return Reader
123 | */
124 | public Reader start(int startRow) {
125 | if (startRow < 0) {
126 | throw new IllegalArgumentException("start cannot be less than 0");
127 | }
128 | this.startRow = startRow;
129 | return this;
130 | }
131 |
132 | /**
133 | * The setting is read from the first sheet, the default is 0
134 | *
135 | * @param sheetIndex sheet index
136 | * @return Reader
137 | */
138 | public Reader sheet(int sheetIndex) {
139 | if (sheetIndex < 0) {
140 | throw new IllegalArgumentException("sheet cannot be less than 0");
141 | }
142 | this.sheetIndex = sheetIndex;
143 | return this;
144 | }
145 |
146 | /**
147 | * Set the name of the sheet to be read. If you set the name, sheet will be invalid.
148 | *
149 | * @param sheetName sheet name
150 | * @return
151 | */
152 | public Reader sheet(String sheetName) {
153 | if (StringUtil.isEmpty(sheetName)) {
154 | throw new IllegalArgumentException("sheet cannot be empty");
155 | }
156 | this.sheetName = sheetName;
157 | return this;
158 | }
159 |
160 | public Reader charset(Charset charset){
161 | this.charset = charset;
162 | return this;
163 | }
164 |
165 | /**
166 | * Return the read result as a Stream
167 | *
168 | * @return Stream
169 | * @throws ReaderException Thrown when an exception occurs during reading
170 | */
171 | public Stream asStream() {
172 | if (modelType == null) {
173 | throw new IllegalArgumentException("modelType can be not null");
174 | }
175 |
176 | if (fromFile == null && fromStream == null) {
177 | throw new IllegalArgumentException("Excel source not is null");
178 | }
179 |
180 | if (fromFile != null) {
181 | return ReaderFactory.readByFile(this);
182 | } else {
183 | return ReaderFactory.readByStream(this);
184 | }
185 | }
186 |
187 | /**
188 | * Convert the read result to a List
189 | *
190 | * @return List
191 | * @throws ReaderException Thrown when an exception occurs during reading
192 | */
193 | public List asList() throws ReaderException {
194 | Stream stream = this.asStream();
195 | return stream.collect(toList());
196 | }
197 |
198 | public Result asResult() throws ReaderException {
199 | return new Result<>(asList());
200 | }
201 |
202 | public InputStream fromStream() {
203 | return this.fromStream;
204 | }
205 |
206 | public File fromFile() {
207 | return fromFile;
208 | }
209 |
210 | public Class modelType() {
211 | return modelType;
212 | }
213 |
214 | public int sheetIndex() {
215 | return this.sheetIndex;
216 | }
217 |
218 | public String sheetName() {
219 | return this.sheetName;
220 | }
221 |
222 | public int startRow() {
223 | return this.startRow;
224 | }
225 |
226 | public Charset charset(){
227 | return this.charset;
228 | }
229 |
230 | }
231 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/annotation/ExcelColumn.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.annotation;
17 |
18 | import io.github.biezhi.excel.plus.conveter.Converter;
19 | import io.github.biezhi.excel.plus.conveter.NullConverter;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | /**
27 | * @author biezhi
28 | * @date 2018-12-11
29 | */
30 | @Retention(RetentionPolicy.RUNTIME)
31 | @Target(ElementType.FIELD)
32 | public @interface ExcelColumn {
33 |
34 | String title() default "";
35 |
36 | int index() default -1;
37 |
38 | String datePattern() default "";
39 |
40 | Class extends Converter> converter() default NullConverter.class;
41 |
42 | int width() default -1;
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/BigIntConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 | import io.github.biezhi.excel.plus.util.StringUtil;
20 |
21 | import java.math.BigInteger;
22 |
23 | /**
24 | * BigInteger to string converter
25 | *
26 | * @author biezhi
27 | * @date 2018-12-12
28 | */
29 | public class BigIntConverter extends NumberConverter implements Converter {
30 |
31 | @Override
32 | public BigInteger stringToR(String value) throws ConverterException {
33 | try {
34 | value = replaceComma(value);
35 | if (StringUtil.isEmpty(value)) {
36 | return null;
37 | }
38 | return new BigInteger(value);
39 | } catch (Exception e){
40 | throw new ConverterException("convert [" + value + "] to BigInteger error", e);
41 | }
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/BooleanConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | /**
19 | * Boolean to string converter
20 | *
21 | * @author biezhi
22 | * @date 2018-12-12
23 | */
24 | public class BooleanConverter implements Converter {
25 |
26 | @Override
27 | public Boolean stringToR(String value) {
28 | return Boolean.parseBoolean(value);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/ByteConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 |
20 | /**
21 | * Byte to string converter
22 | *
23 | * @author biezhi
24 | * @date 2018-12-12
25 | */
26 | public class ByteConverter implements Converter {
27 |
28 | @Override
29 | public Byte stringToR(String value) throws ConverterException {
30 | try {
31 | return Byte.parseByte(value);
32 | } catch (Exception e){
33 | throw new ConverterException("convert [" + value + "] to Byte error", e);
34 | }
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/Converter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 |
20 | /**
21 | * @author biezhi
22 | * @date 2018-12-12
23 | */
24 | public interface Converter {
25 |
26 | R stringToR(String value) throws ConverterException;
27 |
28 | default java.lang.String toString(R fieldValue) throws ConverterException {
29 | if (null == fieldValue) {
30 | return null;
31 | }
32 | return fieldValue.toString();
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/ConverterCache.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.annotation.ExcelColumn;
19 | import lombok.experimental.UtilityClass;
20 |
21 | import java.lang.reflect.Field;
22 | import java.math.BigDecimal;
23 | import java.math.BigInteger;
24 | import java.time.LocalDate;
25 | import java.time.LocalDateTime;
26 | import java.util.Date;
27 | import java.util.HashMap;
28 | import java.util.Map;
29 |
30 | /**
31 | * @author biezhi
32 | * @date 2018-12-12
33 | */
34 | @UtilityClass
35 | public class ConverterCache {
36 |
37 | private static final Map, Converter> CONVERTER_MAP = new HashMap<>(64);
38 |
39 | static {
40 | CONVERTER_MAP.put(StringConverter.class, new StringConverter());
41 | CONVERTER_MAP.put(IntConverter.class, new IntConverter());
42 | CONVERTER_MAP.put(LongConverter.class, new LongConverter());
43 | CONVERTER_MAP.put(ShortConverter.class, new ShortConverter());
44 | CONVERTER_MAP.put(ByteConverter.class, new ByteConverter());
45 | CONVERTER_MAP.put(BooleanConverter.class, new BooleanConverter());
46 | CONVERTER_MAP.put(DoubleConverter.class, new DoubleConverter());
47 | CONVERTER_MAP.put(FloatConverter.class, new FloatConverter());
48 | CONVERTER_MAP.put(DecimalConverter.class, new DecimalConverter());
49 | CONVERTER_MAP.put(BigIntConverter.class, new BigIntConverter());
50 | }
51 |
52 | public static void addConvert(Converter converter) {
53 | if (null != converter) {
54 | CONVERTER_MAP.put(converter.getClass(), converter);
55 | }
56 | }
57 |
58 | public static Converter getConvert(
59 | Class extends Converter> type) {
60 | return CONVERTER_MAP.get(type);
61 | }
62 |
63 | public static Converter computeConvert(Field field) throws Exception {
64 | if (null == field) {
65 | return null;
66 | }
67 |
68 | Class fieldType = field.getType();
69 |
70 | ExcelColumn column = field.getAnnotation(ExcelColumn.class);
71 | if (null != column && !NullConverter.class.equals(column.converter())) {
72 | return column.converter().newInstance();
73 | }
74 |
75 | if (fieldType.equals(String.class)) {
76 | return ConverterCache.getConvert(StringConverter.class);
77 | } else if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) {
78 | return ConverterCache.getConvert(IntConverter.class);
79 | } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) {
80 | return ConverterCache.getConvert(LongConverter.class);
81 | } else if (fieldType.equals(double.class) || fieldType.equals(Double.class)) {
82 | return ConverterCache.getConvert(DoubleConverter.class);
83 | } else if (fieldType.equals(float.class) || fieldType.equals(Float.class)) {
84 | return ConverterCache.getConvert(FloatConverter.class);
85 | } else if (fieldType.equals(short.class) || fieldType.equals(Short.class)) {
86 | return ConverterCache.getConvert(ShortConverter.class);
87 | } else if (fieldType.equals(byte.class) || fieldType.equals(Byte.class)) {
88 | return ConverterCache.getConvert(ByteConverter.class);
89 | } else if (fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) {
90 | return ConverterCache.getConvert(BooleanConverter.class);
91 | } else if (fieldType.equals(BigInteger.class)) {
92 | return ConverterCache.getConvert(BigIntConverter.class);
93 | } else if (fieldType.equals(BigDecimal.class)) {
94 | return ConverterCache.getConvert(DecimalConverter.class);
95 | } else if (fieldType.equals(Date.class)) {
96 | String pattern = field.getAnnotation(ExcelColumn.class).datePattern();
97 | return new DateConverter(pattern);
98 | } else if (fieldType.equals(LocalDate.class)) {
99 | String pattern = field.getAnnotation(ExcelColumn.class).datePattern();
100 | return new LocalDateConverter(pattern);
101 | } else if (fieldType.equals(LocalDateTime.class)) {
102 | String pattern = field.getAnnotation(ExcelColumn.class).datePattern();
103 | return new LocalDateTimeConverter(pattern);
104 | }
105 | return null;
106 | }
107 |
108 | }
109 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/DateConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 |
20 | import java.text.DateFormat;
21 | import java.text.SimpleDateFormat;
22 | import java.util.Date;
23 |
24 | /**
25 | * Date to string converter
26 | *
27 | * @author biezhi
28 | * @date 2018-12-12
29 | */
30 | public class DateConverter implements Converter {
31 |
32 | private ThreadLocal df;
33 |
34 | public DateConverter(String pattern) {
35 | this.df = ThreadLocal.withInitial(() -> new SimpleDateFormat(pattern));
36 | }
37 |
38 | @Override
39 | public Date stringToR(String value) throws ConverterException {
40 | try {
41 | if(null == value){
42 | return null;
43 | }
44 | return df.get().parse(value);
45 | } catch (Exception e) {
46 | throw new ConverterException("convert [" + value + "] to Date error", e);
47 | }
48 | }
49 |
50 | @Override
51 | public String toString(Date date) throws ConverterException {
52 | try {
53 | if(null == date){
54 | return null;
55 | }
56 | return df.get().format(date);
57 | } catch (Exception e) {
58 | throw new ConverterException("convert [" + date + "] to String error", e);
59 | }
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/DecimalConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 | import io.github.biezhi.excel.plus.util.StringUtil;
20 |
21 | import java.math.BigDecimal;
22 |
23 | /**
24 | * BigDecimal to string converter
25 | *
26 | * @author biezhi
27 | * @date 2018-12-12
28 | */
29 | public class DecimalConverter extends NumberConverter implements Converter {
30 |
31 | @Override
32 | public BigDecimal stringToR(String value) throws ConverterException {
33 | try {
34 | value = super.replaceComma(value);
35 | if (StringUtil.isEmpty(value)) {
36 | return null;
37 | }
38 | return new BigDecimal(value);
39 | } catch (Exception e){
40 | throw new ConverterException("convert [" + value + "] to BigDecimal error", e);
41 | }
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/DoubleConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 | import io.github.biezhi.excel.plus.util.StringUtil;
20 |
21 | /**
22 | * Double to string converter
23 | *
24 | * @author biezhi
25 | * @date 2018-12-12
26 | */
27 | public class DoubleConverter extends NumberConverter implements Converter {
28 |
29 | @Override
30 | public Double stringToR(String value) throws ConverterException {
31 | try {
32 | value = super.replaceComma(value);
33 | if (StringUtil.isEmpty(value)) {
34 | return null;
35 | }
36 | return Double.parseDouble(value);
37 | } catch (Exception e){
38 | throw new ConverterException("convert [" + value + "] to Double error", e);
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/FloatConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 | import io.github.biezhi.excel.plus.util.StringUtil;
20 |
21 | /**
22 | * Float to string converter
23 | *
24 | * @author biezhi
25 | * @date 2018-12-12
26 | */
27 | public class FloatConverter extends NumberConverter implements Converter {
28 |
29 | @Override
30 | public Float stringToR(String value) throws ConverterException {
31 | try {
32 | value = super.replaceComma(value);
33 | if (StringUtil.isEmpty(value)) {
34 | return null;
35 | }
36 | return Float.parseFloat(value);
37 | } catch (Exception e){
38 | throw new ConverterException("convert [" + value + "] to Float error", e);
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/IntConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 | import io.github.biezhi.excel.plus.util.StringUtil;
20 |
21 | /**
22 | * Integer to string converter
23 | *
24 | * @author biezhi
25 | * @date 2018-12-12
26 | */
27 | public class IntConverter extends NumberConverter implements Converter {
28 |
29 | @Override
30 | public Integer stringToR(String value) throws ConverterException {
31 | try {
32 | value = super.replaceComma(value);
33 | if (StringUtil.isEmpty(value)) {
34 | return null;
35 | }
36 |
37 | return Integer.parseInt(value);
38 | } catch (Exception e) {
39 | throw new ConverterException("convert [" + value + "] to Integer error", e);
40 | }
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/LocalDateConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 |
20 | import java.time.LocalDate;
21 | import java.time.LocalDateTime;
22 | import java.time.YearMonth;
23 | import java.time.format.DateTimeFormatter;
24 | import java.time.format.DateTimeParseException;
25 |
26 | /**
27 | * LocalDate to string converter
28 | *
29 | * @author biezhi
30 | * @date 2018-12-12
31 | */
32 | public class LocalDateConverter implements Converter {
33 |
34 | private DateTimeFormatter formatter;
35 |
36 | public LocalDateConverter(String pattern) {
37 | this.formatter = DateTimeFormatter.ofPattern(pattern);
38 | }
39 |
40 | @Override
41 | public LocalDate stringToR(String value) throws ConverterException {
42 | try {
43 | if(null == value){
44 | return null;
45 | }
46 | return LocalDate.parse(value, formatter);
47 | } catch (DateTimeParseException e) {
48 | try {
49 | YearMonth ym = YearMonth.parse(value, formatter);
50 | return ym.atDay(1);
51 | } catch (Exception e2) {
52 | throw new ConverterException("convert [" + value + "] to LocalDate error", e2);
53 | }
54 | } catch (Exception e) {
55 | throw new ConverterException("convert [" + value + "] to LocalDate error", e);
56 | }
57 | }
58 |
59 | @Override
60 | public String toString(LocalDate localDate) {
61 | if(null == localDate){
62 | return null;
63 | }
64 | return localDate.format(formatter);
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/LocalDateTimeConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 |
20 | import java.time.LocalDateTime;
21 | import java.time.format.DateTimeFormatter;
22 |
23 | /**
24 | * LocalDateTime to string converter
25 | *
26 | * @author biezhi
27 | * @date 2018-12-12
28 | */
29 | public class LocalDateTimeConverter implements Converter {
30 |
31 | private DateTimeFormatter formatter;
32 |
33 | public LocalDateTimeConverter(String pattern) {
34 | this.formatter = DateTimeFormatter.ofPattern(pattern);
35 | }
36 |
37 | @Override
38 | public LocalDateTime stringToR(String value) throws ConverterException {
39 | try {
40 | if (null == value) {
41 | return null;
42 | }
43 | return LocalDateTime.parse(value, formatter);
44 | } catch (Exception e) {
45 | throw new ConverterException("convert [" + value + "] to LocalDateTime error", e);
46 | }
47 | }
48 |
49 | @Override
50 | public String toString(LocalDateTime localDateTime) {
51 | if (null == localDateTime) {
52 | return null;
53 | }
54 | return localDateTime.format(formatter);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/LongConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 | import io.github.biezhi.excel.plus.util.StringUtil;
20 |
21 | /**
22 | * Long to string converter
23 | *
24 | * @author biezhi
25 | * @date 2018-12-12
26 | */
27 | public class LongConverter extends NumberConverter implements Converter {
28 |
29 | @Override
30 | public Long stringToR(String value) throws ConverterException {
31 | try {
32 | value = super.replaceComma(value);
33 | if (StringUtil.isEmpty(value)) {
34 | return null;
35 | }
36 | return Long.parseLong(value);
37 | } catch (Exception e){
38 | throw new ConverterException("convert [" + value + "] to Long error", e);
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/NullConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | /**
19 | * NullConverter
20 | *
21 | * @author biezhi
22 | * @date 2018-12-12
23 | */
24 | public final class NullConverter implements Converter {
25 |
26 | @Override
27 | public Object stringToR(String value) {
28 | throw new RuntimeException("Not accessible here");
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/NumberConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.util.StringUtil;
19 |
20 | public abstract class NumberConverter {
21 |
22 | String replaceComma(String value) {
23 | if (StringUtil.isEmpty(value)) {
24 | return null;
25 | }
26 | value = value.replaceAll(",", "");
27 | if(value.endsWith(".0")){
28 | return value.substring(0, value.length() - 2);
29 | }
30 | return value;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/ShortConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | import io.github.biezhi.excel.plus.exception.ConverterException;
19 | import io.github.biezhi.excel.plus.util.StringUtil;
20 |
21 | /**
22 | * Short to string converter
23 | *
24 | * @author biezhi
25 | * @date 2018-12-12
26 | */
27 | public class ShortConverter extends NumberConverter implements Converter {
28 |
29 | @Override
30 | public Short stringToR(String value) throws ConverterException {
31 | try {
32 | value = super.replaceComma(value);
33 | if (StringUtil.isEmpty(value)) {
34 | return null;
35 | }
36 | return Short.parseShort(value);
37 | } catch (Exception e) {
38 | throw new ConverterException("convert [" + value + "] to Integer error", e);
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/conveter/StringConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.conveter;
17 |
18 | /**
19 | * String to string
20 | *
21 | * @author biezhi
22 | * @date 2018-12-12
23 | */
24 | public class StringConverter implements Converter {
25 |
26 | @Override
27 | public String stringToR(String value) {
28 | return value;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/enums/ExcelType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.enums;
17 |
18 | /**
19 | * ExcelType
20 | *
21 | * @author biezhi
22 | * @date 2018-12-11
23 | */
24 | public enum ExcelType {
25 |
26 | XLSX, XLS, CSV
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/exception/ConverterException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.exception;
17 |
18 | /**
19 | * ConverterException
20 | *
21 | * @author biezhi
22 | * @date 2018-12-11
23 | */
24 | public class ConverterException extends ExcelPlusException {
25 |
26 | public ConverterException() {
27 | }
28 |
29 | public ConverterException(String message) {
30 | super(message);
31 | }
32 |
33 | public ConverterException(String message, Throwable cause) {
34 | super(message, cause);
35 | }
36 |
37 | public ConverterException(Throwable cause) {
38 | super(cause);
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/exception/ExcelPlusException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.exception;
17 |
18 | /**
19 | * ExcelPlusException
20 | *
21 | * @author biezhi
22 | * @date 2018-12-11
23 | */
24 | public class ExcelPlusException extends Exception {
25 |
26 | public ExcelPlusException() {
27 | }
28 |
29 | public ExcelPlusException(String message) {
30 | super(message);
31 | }
32 |
33 | public ExcelPlusException(String message, Throwable cause) {
34 | super(message, cause);
35 | }
36 |
37 | public ExcelPlusException(Throwable cause) {
38 | super(cause);
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/exception/ReaderException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.exception;
17 |
18 | /**
19 | * ReaderException
20 | *
21 | * @author biezhi
22 | * @date 2018-12-11
23 | */
24 | public class ReaderException extends RuntimeException {
25 |
26 | public ReaderException() {
27 | }
28 |
29 | public ReaderException(String message) {
30 | super(message);
31 | }
32 |
33 | public ReaderException(String message, Throwable cause) {
34 | super(message, cause);
35 | }
36 |
37 | public ReaderException(Throwable cause) {
38 | super(cause);
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/exception/WriterException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.exception;
17 |
18 | /**
19 | * WriterException
20 | *
21 | * @author biezhi
22 | * @date 2018-12-11
23 | */
24 | public class WriterException extends ExcelPlusException {
25 |
26 | public WriterException() {
27 | }
28 |
29 | public WriterException(String message) {
30 | super(message);
31 | }
32 |
33 | public WriterException(String message, Throwable cause) {
34 | super(message, cause);
35 | }
36 |
37 | public WriterException(Throwable cause) {
38 | super(cause);
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/reader/ExcelReader.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.reader;
17 |
18 | import io.github.biezhi.excel.plus.Reader;
19 | import io.github.biezhi.excel.plus.exception.ReaderException;
20 |
21 | import java.util.stream.Stream;
22 |
23 | /**
24 | * Excel Reader
25 | *
26 | * @author biezhi
27 | * @date 2018-12-11
28 | */
29 | public interface ExcelReader {
30 |
31 | Stream readExcel(Reader reader) throws ReaderException;
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/reader/ReaderConverter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.reader;
17 |
18 | import io.github.biezhi.excel.plus.annotation.ExcelColumn;
19 | import io.github.biezhi.excel.plus.conveter.Converter;
20 | import io.github.biezhi.excel.plus.conveter.ConverterCache;
21 | import io.github.biezhi.excel.plus.conveter.NullConverter;
22 | import io.github.biezhi.excel.plus.exception.ConverterException;
23 | import lombok.extern.slf4j.Slf4j;
24 | import org.apache.poi.ss.usermodel.Cell;
25 | import org.apache.poi.ss.usermodel.CellType;
26 | import org.apache.poi.ss.usermodel.DateUtil;
27 | import org.apache.poi.ss.usermodel.Row;
28 |
29 | import java.lang.reflect.Field;
30 | import java.util.HashMap;
31 | import java.util.Map;
32 |
33 | /**
34 | * ReaderConverter
35 | *
36 | * @author biezhi
37 | * @date 2018-12-13
38 | */
39 | @Slf4j
40 | public class ReaderConverter {
41 |
42 | Map fieldIndexes;
43 |
44 | Map> fieldConverters;
45 |
46 | void initFieldConverter(Field[] fields) throws Exception {
47 | this.fieldConverters = new HashMap<>();
48 | this.fieldIndexes = new HashMap<>(fields.length);
49 |
50 | for (Field field : fields) {
51 | ExcelColumn column = field.getAnnotation(ExcelColumn.class);
52 | if (null == column) {
53 | continue;
54 | }
55 | field.setAccessible(true);
56 | fieldIndexes.put(column.index(), field);
57 |
58 | Converter converter;
59 | if (NullConverter.class.equals(column.converter())) {
60 | converter = ConverterCache.computeConvert(field);
61 | } else {
62 | converter = column.converter().newInstance();
63 | }
64 | if (null != converter) {
65 | fieldConverters.put(field, converter);
66 | }
67 | }
68 | }
69 |
70 | void writeFiledValue(Row row, Object instance, Field field) {
71 | ExcelColumn column = field.getAnnotation(ExcelColumn.class);
72 | Cell cell = row.getCell(column.index());
73 | if (null == cell) {
74 | return;
75 | }
76 | try {
77 | Object cellValue = getCellValue(field, cell);
78 | field.set(instance, cellValue);
79 | } catch (Exception e) {
80 | log.error("write value {} to field {} failed", cell.getStringCellValue(), field.getName(), e);
81 | }
82 | }
83 |
84 | public Object getCellValue(Field field, Cell cell) throws ConverterException {
85 | return cell.getStringCellValue();
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/reader/ReaderFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.reader;
17 |
18 | import io.github.biezhi.excel.plus.Reader;
19 | import io.github.biezhi.excel.plus.exception.ReaderException;
20 | import io.github.biezhi.excel.plus.util.ExcelUtil;
21 | import lombok.experimental.UtilityClass;
22 | import lombok.extern.slf4j.Slf4j;
23 |
24 | import java.io.ByteArrayInputStream;
25 | import java.io.FileInputStream;
26 | import java.io.FileNotFoundException;
27 | import java.io.IOException;
28 | import java.util.stream.Stream;
29 |
30 | /**
31 | * Excel Reader Factory
32 | *
33 | * @author biezhi
34 | * @date 2018-12-13
35 | */
36 | @Slf4j
37 | @UtilityClass
38 | public class ReaderFactory {
39 |
40 | public static Stream readByFile(Reader reader) {
41 | if (ExcelUtil.isXLSX(reader.fromFile())) {
42 | return new ReaderWith2007(null).readExcel(reader);
43 | } else {
44 | if (ExcelUtil.isCSV(reader.fromFile())) {
45 | try {
46 | return new ReaderWithCSV(new FileInputStream(reader.fromFile())).readExcel(reader);
47 | } catch (FileNotFoundException e) {
48 | throw new ReaderException(reader.fromFile().getName() + " not found", e);
49 | }
50 | } else if (ExcelUtil.isXLS(reader.fromFile())) {
51 | return new ReaderWith2003(ExcelUtil.create(reader.fromFile())).readExcel(reader);
52 | } else {
53 | throw new ReaderException(reader.fromFile().getName() + " is the wrong format");
54 | }
55 | }
56 | }
57 |
58 | public static Stream readByStream(Reader reader) throws ReaderException {
59 | byte[] bytes;
60 | try {
61 | bytes = ExcelUtil.streamAsBytes(reader.fromStream());
62 | } catch (IOException e) {
63 | throw new ReaderException(e);
64 | }
65 |
66 | if (ExcelUtil.isXLSX(new ByteArrayInputStream(bytes))) {
67 | reader.from(new ByteArrayInputStream(bytes));
68 | return new ReaderWith2007(null).readExcel(reader);
69 | } else {
70 | if (ExcelUtil.isXLS(new ByteArrayInputStream(bytes))) {
71 | return new ReaderWith2003(ExcelUtil.create(new ByteArrayInputStream(bytes))).readExcel(reader);
72 | } else {
73 | return new ReaderWithCSV(new ByteArrayInputStream(bytes)).readExcel(reader);
74 | }
75 | }
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/reader/ReaderWith2003.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.reader;
17 |
18 | import io.github.biezhi.excel.plus.Reader;
19 | import io.github.biezhi.excel.plus.exception.ReaderException;
20 | import io.github.biezhi.excel.plus.util.StringUtil;
21 | import org.apache.poi.ooxml.util.SAXHelper;
22 | import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
23 | import org.apache.poi.openxml4j.opc.OPCPackage;
24 | import org.apache.poi.openxml4j.opc.PackageAccess;
25 | import org.apache.poi.ss.usermodel.DataFormatter;
26 | import org.apache.poi.ss.usermodel.Workbook;
27 | import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
28 | import org.apache.poi.xssf.eventusermodel.XSSFReader;
29 | import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
30 | import org.apache.poi.xssf.model.SharedStrings;
31 | import org.apache.poi.xssf.model.Styles;
32 | import org.apache.poi.xssf.model.StylesTable;
33 | import org.xml.sax.ContentHandler;
34 | import org.xml.sax.InputSource;
35 | import org.xml.sax.SAXException;
36 | import org.xml.sax.XMLReader;
37 |
38 | import javax.xml.parsers.ParserConfigurationException;
39 | import java.io.IOException;
40 | import java.io.InputStream;
41 | import java.util.stream.Stream;
42 |
43 | /**
44 | * Read 2007 Excel
45 | *
46 | * @author biezhi
47 | * @date 2018-12-11
48 | */
49 | public class ReaderWith2007 implements ExcelReader {
50 |
51 | public ReaderWith2007(Workbook workbook) {
52 | // ignore
53 | }
54 |
55 | public Stream readExcel(Reader reader) throws ReaderException {
56 | Class type = reader.modelType();
57 | try {
58 | // The package open is instantaneous, as it should be.
59 | try (OPCPackage p = getPackage(reader)) {
60 |
61 | SheetToCSV sheetToCSV = new SheetToCSV<>(p, reader.startRow(), type);
62 |
63 | this.process(reader, sheetToCSV);
64 |
65 | Stream.Builder stream = sheetToCSV.getRowsStream();
66 | return stream.build();
67 | }
68 | } catch (Exception e) {
69 | throw new ReaderException(e);
70 | }
71 | }
72 |
73 | private OPCPackage getPackage(Reader reader) throws Exception {
74 | if (reader.fromFile() != null) {
75 | return OPCPackage.open(reader.fromFile(), PackageAccess.READ);
76 | } else {
77 | return OPCPackage.open(reader.fromStream());
78 | }
79 | }
80 |
81 | /**
82 | * Initiates the processing of the XLS workbook file to CSV.
83 | *
84 | * @throws IOException If reading the data from the package fails.
85 | * @throws SAXException if parsing the XML data fails.
86 | */
87 | public void process(Reader reader, SheetToCSV sheetToCSV) throws IOException, OpenXML4JException, SAXException {
88 | ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(sheetToCSV.getOpcPackage());
89 | XSSFReader xssfReader = new XSSFReader(sheetToCSV.getOpcPackage());
90 | StylesTable styles = xssfReader.getStylesTable();
91 | XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
92 | int index = 0;
93 |
94 | boolean bySheetName = StringUtil.isNotEmpty(reader.sheetName());
95 |
96 | while (iter.hasNext()) {
97 | try (InputStream stream = iter.next()) {
98 | String sheetName = iter.getSheetName();
99 | if (bySheetName && reader.sheetName().equals(sheetName)) {
100 | processSheet(styles, strings, sheetToCSV, stream);
101 | break;
102 | }
103 | if (!bySheetName && reader.sheetIndex() == index) {
104 | processSheet(styles, strings, sheetToCSV, stream);
105 | break;
106 | }
107 | }
108 | ++index;
109 | }
110 | }
111 |
112 | /**
113 | * Parses and shows the content of one sheet
114 | * using the specified styles and shared-strings tables.
115 | *
116 | * @param styles The table of styles that may be referenced by cells in the sheet
117 | * @param strings The table of strings that may be referenced by cells in the sheet
118 | * @param sheetInputStream The stream to read the sheet-data from.
119 | * @throws java.io.IOException An IO exception from the parser,
120 | * possibly from a byte stream or character stream
121 | * supplied by the application.
122 | * @throws SAXException if parsing the XML data fails.
123 | */
124 | public void processSheet(
125 | Styles styles,
126 | SharedStrings strings,
127 | XSSFSheetXMLHandler.SheetContentsHandler sheetHandler,
128 | InputStream sheetInputStream) throws IOException, SAXException {
129 |
130 | DataFormatter formatter = new XLSXDataFormatter();
131 | InputSource sheetSource = new InputSource(sheetInputStream);
132 |
133 | try {
134 | XMLReader sheetParser = SAXHelper.newXMLReader();
135 | ContentHandler handler = new XSSFSheetXMLHandler(
136 | styles, null, strings, sheetHandler, formatter, false);
137 | sheetParser.setContentHandler(handler);
138 | sheetParser.parse(sheetSource);
139 | } catch (ParserConfigurationException e) {
140 | throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
141 | }
142 | }
143 |
144 | }
145 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/reader/ReaderWithCSV.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.reader;
17 |
18 | import io.github.biezhi.excel.plus.Reader;
19 | import io.github.biezhi.excel.plus.annotation.ExcelColumn;
20 | import io.github.biezhi.excel.plus.conveter.Converter;
21 | import io.github.biezhi.excel.plus.exception.ReaderException;
22 | import lombok.extern.slf4j.Slf4j;
23 |
24 | import java.io.BufferedReader;
25 | import java.io.InputStream;
26 | import java.io.InputStreamReader;
27 | import java.lang.reflect.Field;
28 | import java.util.stream.Stream;
29 |
30 | /**
31 | * CSV Reader
32 | *
33 | * @author biezhi
34 | * @date 2018-12-13
35 | */
36 | @Slf4j
37 | public class ReaderWithCSV extends ReaderConverter implements ExcelReader {
38 |
39 | private InputStream inputStream;
40 |
41 | public ReaderWithCSV(InputStream inputStream) {
42 | this.inputStream = inputStream;
43 | }
44 |
45 | @Override
46 | public Stream readExcel(Reader reader) throws ReaderException {
47 | Class type = reader.modelType();
48 |
49 | try {
50 | this.initFieldConverter(type.getDeclaredFields());
51 | } catch (Exception e) {
52 | throw new ReaderException(e);
53 | }
54 |
55 | Stream.Builder builder = Stream.builder();
56 |
57 | try (BufferedReader br = new BufferedReader(
58 | new InputStreamReader(inputStream, reader.charset()))) {
59 |
60 | int startRow = reader.startRow();
61 |
62 | int pos = 0;
63 | String line = "";
64 | while ((line = br.readLine()) != null) {
65 | if (pos++ < startRow) {
66 | continue;
67 | }
68 | Object instance = type.newInstance();
69 | String[] csvLine = line.split(",");
70 | this.csvLineToInstance(instance, csvLine);
71 | builder.add((T) instance);
72 | }
73 | return builder.build();
74 | } catch (Exception e) {
75 | throw new ReaderException(e);
76 | }
77 | }
78 |
79 | private void csvLineToInstance(Object instance, String[] csvLine) {
80 | for (Field field : fieldIndexes.values()) {
81 | ExcelColumn column = field.getAnnotation(ExcelColumn.class);
82 | try {
83 | if (csvLine.length < (column.index() + 1)) {
84 | continue;
85 | }
86 | Object cellValue = csvLine[column.index()];
87 | Converter converter = fieldConverters.get(field);
88 | if (null != converter) {
89 | cellValue = converter.stringToR(csvLine[column.index()]);
90 | }
91 | field.set(instance, cellValue);
92 | } catch (Exception e) {
93 | log.error("write value {} to field {} failed", csvLine[column.index()], field.getName(), e);
94 | }
95 | }
96 | }
97 |
98 | }
99 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/reader/SheetToCSV.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.util;
17 |
18 | import io.github.biezhi.excel.plus.exception.ReaderException;
19 | import lombok.experimental.UtilityClass;
20 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
21 | import org.apache.poi.ss.usermodel.Workbook;
22 | import org.apache.poi.ss.usermodel.WorkbookFactory;
23 | import org.apache.poi.xssf.usermodel.XSSFWorkbook;
24 |
25 | import java.io.*;
26 |
27 | /**
28 | * Excel Utils
29 | *
30 | * @author biezhi
31 | * @date 2018-12-11
32 | */
33 | @UtilityClass
34 | public class ExcelUtil {
35 |
36 | public static T newInstance(Class type) {
37 | try {
38 | return type.newInstance();
39 | } catch (Exception e) {
40 | return null;
41 | }
42 | }
43 |
44 | public static Workbook create(File file) throws ReaderException {
45 | try {
46 | return WorkbookFactory.create(file);
47 | } catch (IOException e) {
48 | throw new ReaderException(e);
49 | }
50 | }
51 |
52 | public static Workbook create(InputStream inputStream) throws ReaderException {
53 | try {
54 | return WorkbookFactory.create(inputStream);
55 | } catch (IOException e) {
56 | throw new ReaderException(e);
57 | }
58 | }
59 |
60 | public static String getFileExtension(String fileName) {
61 | int lastIndexOf = fileName.lastIndexOf(".");
62 | if (lastIndexOf == -1) {
63 | return ""; // empty extension
64 | }
65 | return fileName.substring(lastIndexOf + 1);
66 | }
67 |
68 | public static boolean isXLSX(File file) {
69 | if (null == file || !file.exists()) {
70 | return false;
71 | }
72 | String ext = getFileExtension(file.getName());
73 | return ext.toUpperCase().equals("XLSX");
74 | }
75 |
76 | public static boolean isXLS(File file) {
77 | if (null == file || !file.exists()) {
78 | return false;
79 | }
80 | String ext = getFileExtension(file.getName());
81 | return ext.toUpperCase().equals("XLS");
82 | }
83 |
84 | public static boolean isCSV(File file) {
85 | if (null == file || !file.exists()) {
86 | return false;
87 | }
88 | String ext = getFileExtension(file.getName());
89 | return ext.toUpperCase().equals("CSV");
90 | }
91 |
92 | public static byte[] streamAsBytes(InputStream inputStream) throws IOException {
93 | if (null == inputStream) {
94 | return null;
95 | }
96 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
97 | // Fake code simulating the copy
98 | // You can generally do better with nio if you need...
99 | // And please, unlike me, do something about the Exceptions :D
100 | byte[] buffer = new byte[1024];
101 | int len;
102 | while ((len = inputStream.read(buffer)) > -1) {
103 | baos.write(buffer, 0, len);
104 | }
105 | baos.flush();
106 | return baos.toByteArray();
107 | }
108 |
109 | public static boolean isXLSX(InputStream inputStream) {
110 | try {
111 | new XSSFWorkbook(inputStream);
112 | } catch (Exception e) {
113 | return false;
114 | }
115 | return true;
116 | }
117 |
118 | public static boolean isXLS(InputStream inputStream) {
119 | try {
120 | new HSSFWorkbook(inputStream);
121 | } catch (Exception e) {
122 | return false;
123 | }
124 | return true;
125 | }
126 |
127 | }
128 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/util/StringUtil.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.util;
17 |
18 | import lombok.experimental.UtilityClass;
19 |
20 | /**
21 | * StringUtil
22 | *
23 | * @author biezhi
24 | * @date 2018-12-12
25 | */
26 | @UtilityClass
27 | public class StringUtil {
28 |
29 | public static boolean isNotEmpty(String value) {
30 | return null != value && !value.isEmpty();
31 | }
32 |
33 | public static boolean isEmpty(String value) {
34 | return null == value || value.isEmpty();
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.writer;
17 |
18 | import io.github.biezhi.excel.plus.exception.WriterException;
19 | import io.github.biezhi.excel.plus.util.StringUtil;
20 | import lombok.experimental.UtilityClass;
21 |
22 | import javax.servlet.http.HttpServletResponse;
23 | import java.io.OutputStream;
24 | import java.nio.charset.StandardCharsets;
25 |
26 | import static io.github.biezhi.excel.plus.Constant.XLSX_CONTENT_TYPE;
27 | import static io.github.biezhi.excel.plus.Constant.XLS_CONTENT_TYPE;
28 |
29 | /**
30 | * Used to wrap the HttpServletResponse and download file name
31 | *
32 | * @author biezhi
33 | * @date 2018/12/12
34 | */
35 | @UtilityClass
36 | public class ResponseWrapper {
37 |
38 | public static OutputStream createXLSX(HttpServletResponse servletResponse, String fileName) throws WriterException {
39 | servletResponse.setContentType(XLSX_CONTENT_TYPE);
40 | return create(servletResponse, fileName);
41 | }
42 |
43 | public static OutputStream createXLS(HttpServletResponse servletResponse, String fileName) throws WriterException {
44 | servletResponse.setContentType(XLS_CONTENT_TYPE);
45 | return create(servletResponse, fileName);
46 | }
47 |
48 | public static OutputStream create(HttpServletResponse servletResponse, String fileName) throws WriterException {
49 | try {
50 | if (null == servletResponse) {
51 | throw new WriterException("response instance not null");
52 | }
53 | if (StringUtil.isEmpty(fileName)) {
54 | throw new WriterException("response file name not empty");
55 | }
56 | fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1");
57 | servletResponse.setHeader("Content-Disposition", "attachment; filename=" + fileName);
58 | return servletResponse.getOutputStream();
59 | } catch (Exception e) {
60 | throw new WriterException(e);
61 | }
62 | }
63 |
64 | }
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/writer/WriterWith2003.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.writer;
17 |
18 | import io.github.biezhi.excel.plus.Writer;
19 | import io.github.biezhi.excel.plus.exception.WriterException;
20 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
21 | import org.apache.poi.ss.usermodel.WorkbookFactory;
22 |
23 | import java.io.IOException;
24 | import java.io.OutputStream;
25 |
26 | /**
27 | * Excel Writer by 2003
28 | *
29 | * @author biezhi
30 | * @date 2018-12-11
31 | */
32 | public class WriterWith2003 extends ExcelWriter {
33 |
34 | public WriterWith2003(OutputStream outputStream) {
35 | super(outputStream);
36 | }
37 |
38 | @Override
39 | public void writeSheet(Writer writer) throws WriterException {
40 | if (writer.template() != null) {
41 | try {
42 | this.workbook = WorkbookFactory.create(writer.template());
43 | super.writeSheet(writer);
44 | } catch (IOException e) {
45 | throw new WriterException(e);
46 | }
47 | } else {
48 | this.workbook = new HSSFWorkbook();
49 | super.writeSheet(writer);
50 | }
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/writer/WriterWith2007.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.biezhi.excel.plus.writer;
17 |
18 | import io.github.biezhi.excel.plus.Writer;
19 | import io.github.biezhi.excel.plus.exception.WriterException;
20 | import org.apache.poi.ss.usermodel.WorkbookFactory;
21 | import org.apache.poi.xssf.streaming.SXSSFWorkbook;
22 |
23 | import java.io.IOException;
24 | import java.io.OutputStream;
25 |
26 | /**
27 | * Excel Writer by 2007
28 | *
29 | * @author biezhi
30 | * @date 2018-12-11
31 | */
32 | public class WriterWith2007 extends ExcelWriter {
33 |
34 | public WriterWith2007(OutputStream outputStream) {
35 | super(outputStream);
36 | }
37 |
38 | @Override
39 | public void writeSheet(Writer writer) throws WriterException {
40 | if (writer.template() != null) {
41 | try {
42 | this.workbook = WorkbookFactory.create(writer.template());
43 | super.writeSheet(writer);
44 | } catch (IOException e) {
45 | throw new WriterException(e);
46 | }
47 | } else {
48 | this.workbook = new SXSSFWorkbook(writer.bufferSize());
49 | super.writeSheet(writer);
50 | }
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/io/github/biezhi/excel/plus/writer/WriterWithCSV.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2018, biezhi (biezhi.me@gmail.com)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *