├── src
├── test
│ ├── resources
│ │ ├── img
│ │ │ ├── read-demo-1.png
│ │ │ └── write-demo-2.png
│ │ └── excel
│ │ │ └── xlsx
│ │ │ ├── data_file1.xlsx
│ │ │ ├── data_file2.xlsx
│ │ │ ├── template_file1.xlsx
│ │ │ ├── template_file2.xlsx
│ │ │ └── template_file3.xlsx
│ └── java
│ │ └── org
│ │ └── hellojavaer
│ │ └── poi
│ │ └── excel
│ │ └── utils
│ │ ├── TestEnum.java
│ │ ├── read
│ │ ├── ReadDemo3.java
│ │ ├── ReadDemo2.java
│ │ ├── ReadDemo4.java
│ │ └── ReadDemo1.java
│ │ ├── ExcelReadExceptionFormat.java
│ │ ├── TestBean.java
│ │ └── write
│ │ ├── WriteDemo3.java
│ │ ├── WriteDemo1.java
│ │ └── WriteDemo2.java
└── main
│ └── java
│ └── org
│ └── hellojavaer
│ └── poi
│ └── excel
│ └── utils
│ ├── ExcelType.java
│ ├── write
│ ├── ExcelWriteTheme.java
│ ├── ExcelWriteRowProcessor.java
│ ├── ExcelWriteCellProcessor.java
│ ├── ExcelWriteCellValueMapping.java
│ ├── ExcelWriteSheetProcessor.java
│ ├── ExcelWriteFieldMapping.java
│ ├── ExcelWriteException.java
│ └── ExcelWriteContext.java
│ ├── ExcelProcessController.java
│ ├── read
│ ├── ExcelReadRowProcessor.java
│ ├── ExcelReadCellProcessor.java
│ ├── ExcelReadCellValueMapping.java
│ ├── ExcelCellValue.java
│ ├── ExcelReadFieldMapping.java
│ ├── ExcelReadSheetProcessor.java
│ ├── ExcelReadContext.java
│ └── ExcelReadException.java
│ ├── common
│ └── Assert.java
│ └── ExcelUtils.java
├── .gitignore
├── README.md
├── pom.xml
└── LICENSE
/src/test/resources/img/read-demo-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hellojavaer/poi-excel-utils/HEAD/src/test/resources/img/read-demo-1.png
--------------------------------------------------------------------------------
/src/test/resources/img/write-demo-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hellojavaer/poi-excel-utils/HEAD/src/test/resources/img/write-demo-2.png
--------------------------------------------------------------------------------
/src/test/resources/excel/xlsx/data_file1.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hellojavaer/poi-excel-utils/HEAD/src/test/resources/excel/xlsx/data_file1.xlsx
--------------------------------------------------------------------------------
/src/test/resources/excel/xlsx/data_file2.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hellojavaer/poi-excel-utils/HEAD/src/test/resources/excel/xlsx/data_file2.xlsx
--------------------------------------------------------------------------------
/src/test/resources/excel/xlsx/template_file1.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hellojavaer/poi-excel-utils/HEAD/src/test/resources/excel/xlsx/template_file1.xlsx
--------------------------------------------------------------------------------
/src/test/resources/excel/xlsx/template_file2.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hellojavaer/poi-excel-utils/HEAD/src/test/resources/excel/xlsx/template_file2.xlsx
--------------------------------------------------------------------------------
/src/test/resources/excel/xlsx/template_file3.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hellojavaer/poi-excel-utils/HEAD/src/test/resources/excel/xlsx/template_file3.xlsx
--------------------------------------------------------------------------------
/src/test/java/org/hellojavaer/poi/excel/utils/TestEnum.java:
--------------------------------------------------------------------------------
1 | package org.hellojavaer.poi.excel.utils;
2 |
3 | /**
4 | * @author zoukaiming
5 | */
6 | public enum TestEnum {
7 | AA, BB, CC;
8 | }
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | # Package Files #
7 | *.jar
8 | *.war
9 | *.ear
10 |
11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12 | hs_err_pid*
13 | /target
14 |
15 | .*
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/ExcelType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils;
17 |
18 | /**
19 | * @author zoukaiming
20 | */
21 | public enum ExcelType {
22 | XLS, XLSX
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/write/ExcelWriteTheme.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.write;
17 |
18 | /**
19 | * @author zoukaiming
20 | */
21 | public class ExcelWriteTheme {
22 |
23 | public static final int BASE = 0;
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/ExcelProcessController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils;
17 |
18 | /**
19 | *
20 | * @author zoukaiming
21 | */
22 | public interface ExcelProcessController {
23 |
24 | void doSkip();
25 |
26 | void doBreak();
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelReadRowProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.read;
17 |
18 | import org.apache.poi.ss.usermodel.Row;
19 | import org.hellojavaer.poi.excel.utils.ExcelProcessController;
20 |
21 | /**
22 | * @author zoukaiming
23 | */
24 | public interface ExcelReadRowProcessor {
25 |
26 | T process(ExcelProcessController controller, ExcelReadContext context, Row row, T t);
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # poi-excel-utils
2 | [](https://maven-badges.herokuapp.com/maven-central/org.hellojavaer/poi-excel-utils/)
3 | [](https://github.com/hellojavaer/poi-excel-utils/releases)
4 | [](https://www.apache.org/licenses/LICENSE-2.0.html)
5 |
6 | ExcelUtils
7 |
8 | ### Reading example
9 |
10 |
11 |
12 | ### Writing example
13 |
14 |
15 |
16 | ## [Get some examples](https://github.com/hellojavaer/poi-excel-utils/tree/master/src/test/java/org/hellojavaer/poi/excel/utils)
17 |
18 | ## Download
19 |
20 | http://repo1.maven.org/maven2/org/hellojavaer/poi-excel-utils/
21 |
22 | ## Maven
23 |
24 | ```xml
25 |
26 | org.hellojavaer
27 | poi-excel-utils
28 | x.x.x
29 |
30 | ```
31 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/write/ExcelWriteRowProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.write;
17 |
18 | import org.apache.poi.ss.usermodel.Row;
19 | import org.hellojavaer.poi.excel.utils.ExcelProcessController;
20 |
21 | /**
22 | * @author zoukaiming
23 | */
24 | public interface ExcelWriteRowProcessor {
25 |
26 | /**
27 | *
28 | * @param controller
29 | * @param context
30 | * @param t
31 | * @param row
32 | * @return
33 | */
34 | void process(ExcelProcessController controller, ExcelWriteContext context, T t, Row row);
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/write/ExcelWriteCellProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.write;
17 |
18 | import org.apache.poi.ss.usermodel.Cell;
19 |
20 | /**
21 | * @author zoukaiming
22 | */
23 | public interface ExcelWriteCellProcessor {
24 |
25 | /**
26 | * Provide you a chance to modify cell value after system has written.
27 | * If null is returned, this cell will been removed.
28 | *
29 | * @param context
30 | * @param t
31 | * @param cell
32 | * @return
33 | */
34 | void process(ExcelWriteContext context, Object fieldVal, Cell cell);
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelReadCellProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.read;
17 |
18 | import org.apache.poi.ss.usermodel.Cell;
19 |
20 | /**
21 | * Provide you a chance to modify current read cell value before the cell value is set to target object.
22 | *
23 | * @author zoukaiming
24 | */
25 | public interface ExcelReadCellProcessor {
26 |
27 | /**
28 | * @param context
29 | * @param cell
30 | * @param cellValue
31 | * @return value. if return cellValue, cellValue.getOriginalValue() will be
32 | * set as final result.
33 | */
34 | Object process(ExcelReadContext> context, Cell cell, ExcelCellValue cellValue);
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/common/Assert.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.common;
17 |
18 | /**
19 | *
20 | * @author zoukaiming 2016年4月2日 下午8:28:11
21 | */
22 | public class Assert {
23 |
24 | public static void notNull(Object object) {
25 | notNull(object, "[Assertion failed] - the object argument must be null");
26 | }
27 |
28 | public static void notNull(Object object, String message) {
29 | if (object == null) {
30 | throw new IllegalArgumentException(message);
31 | }
32 | }
33 |
34 | public static void isTrue(boolean expression) {
35 | isTrue(expression, "[Assertion failed] - this expression must be true");
36 | }
37 |
38 | public static void isTrue(boolean expression, String message) {
39 | if (!expression) {
40 | throw new IllegalArgumentException(message);
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/test/java/org/hellojavaer/poi/excel/utils/read/ReadDemo3.java:
--------------------------------------------------------------------------------
1 | package org.hellojavaer.poi.excel.utils.read;
2 |
3 | import org.apache.poi.ss.usermodel.Cell;
4 | import org.apache.poi.ss.usermodel.Row;
5 | import org.hellojavaer.poi.excel.utils.ExcelProcessController;
6 | import org.hellojavaer.poi.excel.utils.ExcelUtils;
7 | import org.hellojavaer.poi.excel.utils.TestBean;
8 |
9 | import java.io.FileNotFoundException;
10 | import java.io.InputStream;
11 | import java.util.List;
12 |
13 | /**
14 | * @author zoukaiming
15 | */
16 | public class ReadDemo3 {
17 |
18 | public static void main(String[] args) throws FileNotFoundException {
19 | InputStream in = ReadDemo3.class.getResourceAsStream("/excel/xlsx/data_file1.xlsx");
20 | ExcelReadSheetProcessor sheetProcessor = new ExcelReadSheetProcessor() {
21 |
22 | @Override
23 | public void beforeProcess(ExcelReadContext context) {
24 | }
25 |
26 | @Override
27 | public void process(ExcelReadContext context, List list) {
28 | }
29 |
30 | @Override
31 | public void onException(ExcelReadContext context, ExcelReadException e) {
32 | throw e;
33 | }
34 |
35 | @Override
36 | public void afterProcess(ExcelReadContext context) {
37 | }
38 | };
39 |
40 | sheetProcessor.setSheetIndex(0);// required.it can be replaced with 'setSheetName(sheetName)';
41 | sheetProcessor.setStartRowIndex(1);//
42 | // sheetProcessor.setRowEndIndex(3);//
43 | sheetProcessor.setPageSize(2);//
44 | sheetProcessor.setRowProcessor(new ExcelReadRowProcessor() {
45 |
46 | public TestBean process(ExcelProcessController controller, ExcelReadContext context, Row row,
47 | TestBean t) {
48 | if (row != null) {
49 | Cell cell = row.getCell(0);
50 | System.out.println(ExcelUtils.readCell(cell));
51 | }
52 | return t;
53 | }
54 | });
55 | ExcelUtils.read(in, sheetProcessor);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/test/java/org/hellojavaer/poi/excel/utils/ExcelReadExceptionFormat.java:
--------------------------------------------------------------------------------
1 | package org.hellojavaer.poi.excel.utils;
2 |
3 | import org.hellojavaer.poi.excel.utils.read.ExcelReadException;
4 |
5 | /**
6 | *
7 | * @author zoukaiming,created on 08/12/2016.
8 | */
9 | public class ExcelReadExceptionFormat {
10 |
11 | public static String format(ExcelReadException e) {
12 | StringBuilder sb = new StringBuilder();
13 | if (e.getCode() == ExcelReadException.CODE_OF_SHEET_NOT_EXIST) {
14 | sb.append("表:");
15 | if (e.getSheetName() != null) {
16 | sb.append(e.getSheetName());
17 | }
18 | if (e.getSheetIndex() != null) {
19 | sb.append('[');
20 | sb.append(e.getSheetIndex());
21 | sb.append(']');
22 | }
23 | sb.append("不存在");
24 | return sb.toString();
25 | }
26 | if (e.getCode() == ExcelReadException.CODE_OF_SHEET_NAME_AND_INDEX_NOT_MATCH) {
27 | sb.append("表名:");
28 | sb.append(e.getSheetName());
29 | sb.append("和表索引:[");
30 | sb.append(e.getSheetIndex());
31 | sb.append("]");
32 | sb.append("不匹配");
33 | return sb.toString();
34 | }
35 | if (e.getCode() == ExcelReadException.CODE_OF_SHEET_NAME_AND_INDEX_IS_EMPTY) {
36 | sb.append("表名和索引不能都为空");
37 | return sb.toString();
38 | }
39 | sb.append("表 ");
40 | sb.append(e.getSheetName());
41 | //sb.append("[");
42 | //sb.append(e.getSheetIndex());
43 | //sb.append("]");
44 | if (e.getRowIndex() == null) {
45 | sb.append(" 在处理表数据时异常");
46 | } else {
47 | sb.append(" , 第 ");
48 | sb.append(e.getRowIndex());
49 | sb.append(" 行");
50 | if (e.getColIndex() == null) {
51 | sb.append("时处理异常");
52 | } else {
53 | sb.append(", 第 ");
54 | sb.append(e.getColStrIndex());
55 | sb.append(" 列");
56 | if (e.getCode() == ExcelReadException.CODE_OF_PROCESS_EXCEPTION) {
57 | sb.append(",数据处理异常,请校验数据格式的正确性.");
58 | } else if (e.getCode() == ExcelReadException.CODE_OF_CELL_VALUE_NOT_MATCH) {
59 | sb.append(",所输入的值不合法,请校验数据格式的正确性.");
60 | } else if (e.getCode() == ExcelReadException.CODE_OF_CELL_VALUE_REQUIRED) {
61 | sb.append("是必填项.");
62 | }
63 | }
64 | }
65 | return sb.toString();
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelReadCellValueMapping.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.read;
17 |
18 | import org.hellojavaer.poi.excel.utils.write.ExcelWriteCellValueMapping;
19 |
20 | import java.util.HashMap;
21 |
22 | /**
23 | * Config the mapping between source value(which must be convertd to type of
24 | * String) to target value.
25 | * If you have used this configration, it also requries that very source value
26 | * must have a target value. If not, {@code #ExcelWriteException} will been
27 | * thrown. To process this case, you can use 'setDefaultValue' method or
28 | * 'setDefaultProcessor' to set default value.
29 | *
30 | * @see ExcelWriteCellValueMapping
31 | * @author zoukaiming
32 | */
33 | public class ExcelReadCellValueMapping extends HashMap {
34 |
35 | private static final long serialVersionUID = 1L;
36 |
37 | private static final Object DEFAULT_INPUT = new Object();
38 | private boolean useDefaultValue = false;
39 | private Object defaultValue = null;
40 | private ExcelReadCellProcessor defaultProcessor;
41 |
42 | public Object getDefaultValue() {
43 | return this.defaultValue;
44 | }
45 |
46 | public void setDefaultValue(Object val) {
47 | this.defaultValue = val;
48 | this.useDefaultValue = true;
49 | }
50 |
51 | public void setDefaultValueWithDefaultInput() {
52 | this.defaultValue = DEFAULT_INPUT;
53 | this.useDefaultValue = true;
54 | }
55 |
56 | public void resetDefaultValue() {
57 | this.defaultValue = null;
58 | this.useDefaultValue = false;
59 | }
60 |
61 | public boolean isUseDefaultValue() {
62 | return useDefaultValue;
63 | }
64 |
65 | public boolean isUseDefaultValueWithDefaultInput() {
66 | return useDefaultValue && (defaultValue == DEFAULT_INPUT);
67 | }
68 |
69 | public ExcelReadCellProcessor getDefaultProcessor() {
70 | return defaultProcessor;
71 | }
72 |
73 | public void setDefaultProcessor(ExcelReadCellProcessor defaultProcessor) {
74 | this.defaultProcessor = defaultProcessor;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelCellValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.read;
17 |
18 | import java.io.Serializable;
19 | import java.math.BigDecimal;
20 | import java.math.BigInteger;
21 | import java.util.Date;
22 |
23 | import com.alibaba.fastjson.util.TypeUtils;
24 |
25 | /**
26 | * provide rich methods for type castting.
27 | *
28 | * @author zoukaiming
29 | */
30 | public class ExcelCellValue implements Serializable {
31 |
32 | private static final long serialVersionUID = 1L;
33 | private Object originalValue;
34 |
35 | public ExcelCellValue(Object originalValue) {
36 | this.originalValue = originalValue;
37 | }
38 |
39 | public Byte getByteValue() {
40 | return TypeUtils.castToByte(originalValue);
41 | }
42 |
43 | public Short getShortValue() {
44 | return TypeUtils.castToShort(originalValue);
45 | }
46 |
47 | public Integer getIntValue() {
48 | return TypeUtils.castToInt(originalValue);
49 | }
50 |
51 | public Long getLongValue() {
52 | return TypeUtils.castToLong(originalValue);
53 | }
54 |
55 | public Float getFloatValue() {
56 | return TypeUtils.castToFloat(originalValue);
57 | }
58 |
59 | public Double getDoubleValue() {
60 | return TypeUtils.castToDouble(originalValue);
61 | }
62 |
63 | public String getStringValue() {
64 | return TypeUtils.castToString(originalValue);
65 | }
66 |
67 | public Boolean getBooleanValue() {
68 | return TypeUtils.castToBoolean(originalValue);
69 | }
70 |
71 | public Date getDateValue() {
72 | return TypeUtils.castToDate(originalValue);
73 | }
74 |
75 | public BigDecimal getBigDecimal() {
76 | return TypeUtils.castToBigDecimal(originalValue);
77 | }
78 |
79 | public BigInteger getBigInteger() {
80 | return TypeUtils.castToBigInteger(originalValue);
81 | }
82 |
83 | public java.sql.Timestamp getTimestamp() {
84 | return TypeUtils.castToTimestamp(originalValue);
85 | }
86 |
87 | public Object getOriginalValue() {
88 | return originalValue;
89 | }
90 |
91 | @Override
92 | public String toString() {
93 | return TypeUtils.castToString(originalValue);
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/write/ExcelWriteCellValueMapping.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.write;
17 |
18 | import org.hellojavaer.poi.excel.utils.read.ExcelReadCellValueMapping;
19 |
20 | import java.util.HashMap;
21 |
22 | /**
23 | * Config the mapping between source value(which must be convertd to type of
24 | * String) to target value.
25 | * If you have used this configration, it also requries that very source value
26 | * must have a target value. If not, {@code #ExcelWriteException} will been
27 | * thrown. To process this case, you can use 'useDefaultValue' method or
28 | * 'setDefaultProcessor' to set default value.
29 | *
30 | * @see ExcelReadCellValueMapping
31 | * @author zoukaiming
32 | */
33 | public class ExcelWriteCellValueMapping extends HashMap {
34 |
35 | private static final long serialVersionUID = 1L;
36 |
37 | private static final Object DEFAULT_INPUT = new Object();
38 | private boolean useDefaultValue = false;
39 | private Object defaultValue = null;
40 | @SuppressWarnings("rawtypes")
41 | private ExcelWriteCellProcessor defaultProcessor;
42 |
43 | public Object getDefaultValue() {
44 | return this.defaultValue;
45 | }
46 |
47 | public void setDefaultValue(Object val) {
48 | this.defaultValue = val;
49 | this.useDefaultValue = true;
50 | }
51 |
52 | public void setDefaultValueWithDefaultInput() {
53 | this.defaultValue = DEFAULT_INPUT;
54 | this.useDefaultValue = true;
55 | }
56 |
57 | public void resetDefaultValue() {
58 | this.defaultValue = null;
59 | this.useDefaultValue = false;
60 | }
61 |
62 | public boolean isUseDefaultValue() {
63 | return useDefaultValue;
64 | }
65 |
66 | public boolean isUseDefaultValueWithDefaultInput() {
67 | return useDefaultValue && (defaultValue == DEFAULT_INPUT);
68 | }
69 |
70 | @SuppressWarnings("rawtypes")
71 | public ExcelWriteCellProcessor getDefaultProcessor() {
72 | return defaultProcessor;
73 | }
74 |
75 | public void setDefaultProcessor(@SuppressWarnings("rawtypes") ExcelWriteCellProcessor defaultProcessor) {
76 | this.defaultProcessor = defaultProcessor;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/test/java/org/hellojavaer/poi/excel/utils/TestBean.java:
--------------------------------------------------------------------------------
1 | package org.hellojavaer.poi.excel.utils;
2 |
3 | import java.io.Serializable;
4 | import java.util.Date;
5 |
6 | /**
7 | * @author zoukaiming
8 | */
9 | public class TestBean implements Serializable {
10 |
11 | private static final long serialVersionUID = 1L;
12 |
13 | private Byte byteField;
14 | private Short shortField;
15 | private Integer intField;
16 | private Long longField;
17 | private Float floatField;
18 | private Double doubleField;
19 | private Boolean boolField;
20 | private String stringField;
21 | private Date dateField;
22 | private String enumField1;
23 | private String enumField2;
24 | private String url;
25 |
26 | public Byte getByteField() {
27 | return byteField;
28 | }
29 |
30 | public void setByteField(Byte byteField) {
31 | this.byteField = byteField;
32 | }
33 |
34 | public Short getShortField() {
35 | return shortField;
36 | }
37 |
38 | public void setShortField(Short shortField) {
39 | this.shortField = shortField;
40 | }
41 |
42 | public Integer getIntField() {
43 | return intField;
44 | }
45 |
46 | public void setIntField(Integer intField) {
47 | this.intField = intField;
48 | }
49 |
50 | public Long getLongField() {
51 | return longField;
52 | }
53 |
54 | public void setLongField(Long longField) {
55 | this.longField = longField;
56 | }
57 |
58 | public Float getFloatField() {
59 | return floatField;
60 | }
61 |
62 | public void setFloatField(Float floatField) {
63 | this.floatField = floatField;
64 | }
65 |
66 | public Double getDoubleField() {
67 | return doubleField;
68 | }
69 |
70 | public void setDoubleField(Double doubleField) {
71 | this.doubleField = doubleField;
72 | }
73 |
74 | public Boolean getBoolField() {
75 | return boolField;
76 | }
77 |
78 | public void setBoolField(Boolean boolField) {
79 | this.boolField = boolField;
80 | }
81 |
82 | public String getStringField() {
83 | return stringField;
84 | }
85 |
86 | public void setStringField(String stringField) {
87 | this.stringField = stringField;
88 | }
89 |
90 | public Date getDateField() {
91 | return dateField;
92 | }
93 |
94 | public void setDateField(Date dateField) {
95 | this.dateField = dateField;
96 | }
97 |
98 | public String getEnumField1() {
99 | return enumField1;
100 | }
101 |
102 | public void setEnumField1(String enumField1) {
103 | this.enumField1 = enumField1;
104 | }
105 |
106 | public String getEnumField2() {
107 | return enumField2;
108 | }
109 |
110 | public void setEnumField2(String enumField2) {
111 | this.enumField2 = enumField2;
112 | }
113 |
114 | public String getUrl() {
115 | return url;
116 | }
117 |
118 | public void setUrl(String url) {
119 | this.url = url;
120 | }
121 |
122 | }
123 |
--------------------------------------------------------------------------------
/src/main/java/org/hellojavaer/poi/excel/utils/read/ExcelReadFieldMapping.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2016 the original author or authors.
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 org.hellojavaer.poi.excel.utils.read;
17 |
18 | import java.io.Serializable;
19 | import java.util.LinkedHashMap;
20 | import java.util.Map;
21 | import java.util.concurrent.ConcurrentHashMap;
22 |
23 | import org.hellojavaer.poi.excel.utils.common.Assert;
24 |
25 | /**
26 | * @author zoukaiming
27 | */
28 | public class ExcelReadFieldMapping implements Serializable {
29 |
30 | private static final long serialVersionUID = 1L;
31 |
32 | private Map> fieldMapping = new LinkedHashMap>();
33 |
34 | public ExcelReadFieldMappingAttribute put(String colIndexOrColName, String fieldName) {
35 | Assert.notNull(colIndexOrColName);
36 | Assert.notNull(fieldName);
37 | Map map = fieldMapping.get(colIndexOrColName);
38 | if (map == null) {
39 | synchronized (fieldMapping) {
40 | if (fieldMapping.get(colIndexOrColName) == null) {
41 | map = new ConcurrentHashMap();
42 | fieldMapping.put(colIndexOrColName, map);
43 | }
44 | }
45 | }
46 | ExcelReadFieldMappingAttribute attribute = new ExcelReadFieldMappingAttribute();
47 | map.put(fieldName, attribute);
48 | return attribute;
49 | }
50 |
51 | public Map> export() {
52 | return fieldMapping;
53 | }
54 |
55 | public class ExcelReadFieldMappingAttribute implements Serializable {
56 |
57 | private static final long serialVersionUID = 1L;
58 |
59 | private boolean required = false;
60 | private ExcelReadCellProcessor cellProcessor;
61 | private ExcelReadCellValueMapping valueMapping;
62 | private String linkField;
63 |
64 | public ExcelReadFieldMappingAttribute setRequired(boolean required) {
65 | this.required = required;
66 | return this;
67 | }
68 |
69 | public ExcelReadFieldMappingAttribute setCellProcessor(ExcelReadCellProcessor cellProcessor) {
70 | this.cellProcessor = cellProcessor;
71 | return this;
72 | }
73 |
74 | public ExcelReadFieldMappingAttribute setValueMapping(ExcelReadCellValueMapping valueMapping) {
75 | this.valueMapping = valueMapping;
76 | return this;
77 | }
78 |
79 | public ExcelReadFieldMappingAttribute setLinkField(String linkField) {
80 | this.linkField = linkField;
81 | return this;
82 | }
83 |
84 | public boolean isRequired() {
85 | return required;
86 | }
87 |
88 | public ExcelReadCellProcessor getCellProcessor() {
89 | return cellProcessor;
90 | }
91 |
92 | public ExcelReadCellValueMapping getValueMapping() {
93 | return valueMapping;
94 | }
95 |
96 | public String getLinkField() {
97 | return linkField;
98 | }
99 |
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/src/test/java/org/hellojavaer/poi/excel/utils/read/ReadDemo2.java:
--------------------------------------------------------------------------------
1 | package org.hellojavaer.poi.excel.utils.read;
2 |
3 | import com.alibaba.fastjson.JSONObject;
4 | import com.alibaba.fastjson.serializer.SerializerFeature;
5 | import org.apache.poi.ss.usermodel.Cell;
6 | import org.apache.poi.ss.usermodel.Row;
7 | import org.hellojavaer.poi.excel.utils.*;
8 |
9 | import java.io.FileNotFoundException;
10 | import java.io.InputStream;
11 | import java.util.List;
12 |
13 | /**
14 | * @author zoukaiming
15 | */
16 | public class ReadDemo2 {
17 |
18 | public static void main(String[] args) throws FileNotFoundException {
19 | InputStream in = ReadDemo2.class.getResourceAsStream("/excel/xlsx/data_file1.xlsx");
20 | ExcelReadSheetProcessor sheetProcessor = new ExcelReadSheetProcessor() {
21 |
22 | @Override
23 | public void beforeProcess(ExcelReadContext context) {
24 |
25 | }
26 |
27 | @Override
28 | public void process(ExcelReadContext context, List list) {
29 | System.out.println(JSONObject.toJSONString(list, SerializerFeature.WriteDateUseDateFormat));
30 | }
31 |
32 | @Override
33 | public void onException(ExcelReadContext context, ExcelReadException e) {
34 | throw e;
35 | }
36 |
37 | @Override
38 | public void afterProcess(ExcelReadContext context) {
39 |
40 | }
41 | };
42 | ExcelReadFieldMapping fieldMapping = new ExcelReadFieldMapping();
43 | fieldMapping.put("byte", "byteField").setRequired(true);
44 | fieldMapping.put("short", "shortField");
45 | fieldMapping.put("int", "intField");
46 | fieldMapping.put("long", "longField");
47 | fieldMapping.put("float", "floatField");
48 | fieldMapping.put("double", "doubleField");
49 | fieldMapping.put("boolean", "boolField");
50 | fieldMapping.put("string", "stringField");
51 | fieldMapping.put("date", "dateField");
52 |
53 | fieldMapping.put("enum1", "enumField1").setCellProcessor(new ExcelReadCellProcessor() {
54 |
55 | public Object process(ExcelReadContext> context, Cell cell, ExcelCellValue cellValue) {
56 | throw new ExcelReadException("test throw exception");
57 | //return cellValue.getStringValue() + "=>row:" + context.getCurRowIndex() + ",col:"
58 | // + context.getCurColStrIndex();
59 | }
60 | });
61 |
62 | ExcelReadCellValueMapping valueMapping = new ExcelReadCellValueMapping();
63 | valueMapping.put("Please select", null);
64 | valueMapping.put("Option1", TestEnum.AA.toString());
65 | valueMapping.put("Option2", TestEnum.BB.toString());
66 | valueMapping.put("Option3", TestEnum.CC.toString());
67 | // valueMapping.setDefaultValueWithDefaultInput();
68 | fieldMapping.put("enum2", "enumField2").setValueMapping(valueMapping).setRequired(false);
69 |
70 | sheetProcessor.setSheetIndex(0);// required.it can be replaced with 'setSheetName(sheetName)';
71 | sheetProcessor.setStartRowIndex(1);//
72 | // sheetProcessor.setRowEndIndex(3);//
73 | sheetProcessor.setTargetClass(TestBean.class);// required
74 | sheetProcessor.setFieldMapping(fieldMapping);// required
75 | sheetProcessor.setPageSize(2);//
76 | sheetProcessor.setTrimSpace(true);
77 | sheetProcessor.setHeadRowIndex(0);
78 | sheetProcessor.setRowProcessor(new ExcelReadRowProcessor() {
79 |
80 | public TestBean process(ExcelProcessController controller, ExcelReadContext context, Row row,
81 | TestBean t) {
82 | return t;
83 | }
84 | });
85 |
86 | try {
87 | ExcelUtils.read(in, sheetProcessor);
88 | } catch (ExcelReadException e) {
89 | System.out.println(ExcelReadExceptionFormat.format(e));
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/test/java/org/hellojavaer/poi/excel/utils/write/WriteDemo3.java:
--------------------------------------------------------------------------------
1 | package org.hellojavaer.poi.excel.utils.write;
2 |
3 | import org.apache.poi.ss.usermodel.Cell;
4 | import org.apache.poi.ss.usermodel.CellStyle;
5 | import org.hellojavaer.poi.excel.utils.ExcelType;
6 | import org.hellojavaer.poi.excel.utils.ExcelUtils;
7 |
8 | import java.io.File;
9 | import java.io.FileOutputStream;
10 | import java.io.IOException;
11 | import java.net.URL;
12 | import java.util.*;
13 | import java.util.concurrent.atomic.AtomicBoolean;
14 |
15 | /**
16 | * @author zoukaiming
17 | */
18 | public class WriteDemo3 {
19 |
20 | public static void main(String[] args) throws IOException {
21 | URL url = WriteDemo3.class.getResource("/");
22 | final String outputFilePath = url.getPath() + "output_file3.xlsx";
23 | File outputFile = new File(outputFilePath);
24 | outputFile.createNewFile();
25 | FileOutputStream output = new FileOutputStream(outputFile);
26 | final AtomicBoolean b = new AtomicBoolean(false);
27 |
28 | ExcelWriteSheetProcessor