diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
index 2b7ced3e..da12189c 100644
--- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
+++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
@@ -50,7 +50,7 @@ import org.slf4j.LoggerFactory;
public class WriteContextImpl implements WriteContext {
private static final Logger LOGGER = LoggerFactory.getLogger(WriteContextImpl.class);
- private static final String NO_SHEETS="no sheets";
+ private static final String NO_SHEETS = "no sheets";
/**
* The Workbook currently written
@@ -248,7 +248,8 @@ public class WriteContextImpl implements WriteContext {
Cell cell = row.createCell(columnIndex);
WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE);
cell.setCellValue(head.getHeadNameList().get(relativeRowIndex));
- WriteHandlerUtils.afterCellDispose(this, (WriteCellData>) null, cell, head, relativeRowIndex, Boolean.TRUE);
+ WriteHandlerUtils.afterCellDispose(this, (WriteCellData>)null, cell, head, relativeRowIndex,
+ Boolean.TRUE);
}
}
@@ -351,7 +352,7 @@ public class WriteContextImpl implements WriteContext {
try {
Workbook workbook = writeWorkbookHolder.getWorkbook();
if (workbook instanceof SXSSFWorkbook) {
- ((SXSSFWorkbook) workbook).dispose();
+ ((SXSSFWorkbook)workbook).dispose();
}
} catch (Throwable t) {
throwable = t;
@@ -446,14 +447,9 @@ public class WriteContextImpl implements WriteContext {
throw e;
}
}
- POIFSFileSystem fileSystem = null;
- try {
- fileSystem = openFileSystemAndEncrypt(tempXlsx);
+ try (POIFSFileSystem fileSystem = openFileSystemAndEncrypt(tempXlsx)) {
fileSystem.writeFilesystem(writeWorkbookHolder.getOutputStream());
} finally {
- if (fileSystem != null) {
- fileSystem.close();
- }
if (!tempXlsx.delete()) {
throw new ExcelGenerateException("Can not delete temp File!");
}
@@ -472,19 +468,9 @@ public class WriteContextImpl implements WriteContext {
if (writeWorkbookHolder.getFile() == null) {
return;
}
- FileOutputStream fileOutputStream = null;
- POIFSFileSystem fileSystem = null;
- try {
- fileSystem = openFileSystemAndEncrypt(writeWorkbookHolder.getFile());
- fileOutputStream = new FileOutputStream(writeWorkbookHolder.getFile());
+ try (POIFSFileSystem fileSystem = openFileSystemAndEncrypt(writeWorkbookHolder.getFile());
+ FileOutputStream fileOutputStream = new FileOutputStream(writeWorkbookHolder.getFile())) {
fileSystem.writeFilesystem(fileOutputStream);
- } finally {
- if (fileOutputStream != null) {
- fileOutputStream.close();
- }
- if (fileSystem != null) {
- fileSystem.close();
- }
}
}
@@ -492,15 +478,9 @@ public class WriteContextImpl implements WriteContext {
POIFSFileSystem fileSystem = new POIFSFileSystem();
Encryptor encryptor = new EncryptionInfo(EncryptionMode.standard).getEncryptor();
encryptor.confirmPassword(writeWorkbookHolder.getPassword());
- OPCPackage opcPackage = null;
- try {
- opcPackage = OPCPackage.open(file, PackageAccess.READ_WRITE);
- OutputStream outputStream = encryptor.getDataStream(fileSystem);
+ try (OPCPackage opcPackage = OPCPackage.open(file, PackageAccess.READ_WRITE);
+ OutputStream outputStream = encryptor.getDataStream(fileSystem)) {
opcPackage.save(outputStream);
- } finally {
- if (opcPackage != null) {
- opcPackage.close();
- }
}
return fileSystem;
}
diff --git a/src/main/java/com/alibaba/excel/metadata/NullObject.java b/src/main/java/com/alibaba/excel/metadata/NullObject.java
new file mode 100644
index 00000000..845d98a1
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/metadata/NullObject.java
@@ -0,0 +1,9 @@
+package com.alibaba.excel.metadata;
+
+/**
+ * Null object.
+ *
+ * @author Jiaju Zhuang
+ */
+public class NullObject {
+}
diff --git a/src/main/java/com/alibaba/excel/util/FieldUtils.java b/src/main/java/com/alibaba/excel/util/FieldUtils.java
index 1e788907..fd692235 100644
--- a/src/main/java/com/alibaba/excel/util/FieldUtils.java
+++ b/src/main/java/com/alibaba/excel/util/FieldUtils.java
@@ -2,7 +2,12 @@ package com.alibaba.excel.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.util.Map;
+import com.alibaba.excel.metadata.NullObject;
+import com.alibaba.excel.write.metadata.RowData;
+
+import net.sf.cglib.beans.BeanMap;
/**
* Field utils
@@ -11,8 +16,29 @@ import java.lang.reflect.Modifier;
**/
public class FieldUtils {
+ public static Class> nullObjectClass = NullObject.class;
+
private static final int START_RESOLVE_FIELD_LENGTH = 2;
+ public static Class> getFieldClass(Map dataMap, String filedName) {
+ if (dataMap instanceof BeanMap) {
+ return ((BeanMap)dataMap).getPropertyType(filedName);
+ }
+ Object value = dataMap.get(filedName);
+ if (value != null) {
+ return value.getClass();
+ }
+ return nullObjectClass;
+ }
+
+ public static Class> getFieldClass(RowData rowData, int dataIndex) {
+ Object value = rowData.get(dataIndex);
+ if (value != null) {
+ return value.getClass();
+ }
+ return nullObjectClass;
+ }
+
/**
* Parsing the name matching cglib。
*
@@ -51,17 +77,13 @@ public class FieldUtils {
return firstChar + fieldName.substring(1);
}
-
/**
* Gets an accessible {@link Field} by name respecting scope. Superclasses/interfaces will be considered.
*
- * @param cls
- * the {@link Class} to reflect, must not be {@code null}
- * @param fieldName
- * the field name to obtain
+ * @param cls the {@link Class} to reflect, must not be {@code null}
+ * @param fieldName the field name to obtain
* @return the Field object
- * @throws IllegalArgumentException
- * if the class is {@code null}, or the field name is blank or empty
+ * @throws IllegalArgumentException if the class is {@code null}, or the field name is blank or empty
*/
public static Field getField(final Class> cls, final String fieldName) {
final Field field = getField(cls, fieldName, false);
@@ -69,20 +91,16 @@ public class FieldUtils {
return field;
}
-
-
/**
* Gets an accessible {@link Field} by name, breaking scope if requested. Superclasses/interfaces will be
* considered.
*
- * @param cls
- * the {@link Class} to reflect, must not be {@code null}
- * @param fieldName
- * the field name to obtain
- * @param forceAccess
- * whether to break scope restrictions using the
- * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only
- * match {@code public} fields.
+ * @param cls the {@link Class} to reflect, must not be {@code null}
+ * @param fieldName the field name to obtain
+ * @param forceAccess whether to break scope restrictions using the
+ * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will
+ * only
+ * match {@code public} fields.
* @return the Field object
* @throws NullPointerException if the class is {@code null}
* @throws IllegalArgumentException if the field name is blank or empty or is matched at multiple places
@@ -140,6 +158,4 @@ public class FieldUtils {
return match;
}
-
-
}
diff --git a/src/main/java/com/alibaba/excel/util/MapUtils.java b/src/main/java/com/alibaba/excel/util/MapUtils.java
index 2ecee78a..eb4409a3 100644
--- a/src/main/java/com/alibaba/excel/util/MapUtils.java
+++ b/src/main/java/com/alibaba/excel/util/MapUtils.java
@@ -9,8 +9,10 @@ import java.util.LinkedHashMap;
* @author Jiaju Zhuang
**/
public class MapUtils {
+
private MapUtils() {}
+
/**
* Creates a mutable, empty {@code HashMap} instance.
*
diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
index b1777c02..d2677d3d 100644
--- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
@@ -113,8 +113,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE);
Object value = oneRowData.get(dataIndex);
WriteCellData> cellData = converterAndSet(writeContext.currentWriteHolder(),
- value == null ? null : value.getClass(),
- null, cell, value, null, head, relativeRowIndex);
+ FieldUtils.getFieldClass(oneRowData, dataIndex), null, cell, value, null, head, relativeRowIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
}
@@ -143,8 +142,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
Object value = beanMap.get(name);
WriteCellData> cellData = converterAndSet(currentWriteHolder,
excelContentProperty.getField().getType(),
- null,
- cell, value, excelContentProperty, head, relativeRowIndex);
+ null, cell, value, excelContentProperty, head, relativeRowIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
beanMapHandledSet.add(name);
}
@@ -168,9 +166,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
WriteHandlerUtils.beforeCellCreate(writeContext, row, null, cellIndex, relativeRowIndex, Boolean.FALSE);
Cell cell = WorkBookUtil.createCell(row, cellIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE);
- WriteCellData> cellData = converterAndSet(currentWriteHolder, value == null ? null : value.getClass(),
- null, cell,
- value, null, null, relativeRowIndex);
+ WriteCellData> cellData = converterAndSet(currentWriteHolder,
+ FieldUtils.getFieldClass(beanMap, filedName), null, cell, value, null, null, relativeRowIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE);
}
}
diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
index 7675bcf3..fd3802c0 100644
--- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
@@ -16,6 +16,7 @@ import com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
+import com.alibaba.excel.util.FieldUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.metadata.fill.AnalysisCell;
@@ -198,8 +199,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
continue;
}
Object value = dataMap.get(variable);
- WriteCellData> cellData = converterAndSet(writeSheetHolder, value == null ? null : value.getClass(), null,
- cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex);
+ WriteCellData> cellData = converterAndSet(writeSheetHolder, FieldUtils.getFieldClass(dataMap, variable),
+ null, cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE);
} else {
StringBuilder cellValueBuild = new StringBuilder();
diff --git a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
index 5569492e..d2c12a70 100644
--- a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
+++ b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java
@@ -30,7 +30,7 @@ public class EncryptDataTest {
@BeforeClass
public static void init() {
- file07 = TestFileUtil.createNewFile("encrypt07111.xlsx");
+ file07 = TestFileUtil.createNewFile("encrypt07.xlsx");
file03 = TestFileUtil.createNewFile("encrypt03.xls");
file07OutputStream = TestFileUtil.createNewFile("encryptOutputStream07.xlsx");
file03OutputStream = TestFileUtil.createNewFile("encryptOutputStream03.xls");
diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiEncryptTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiEncryptTest.java
new file mode 100644
index 00000000..67b6cfc7
--- /dev/null
+++ b/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiEncryptTest.java
@@ -0,0 +1,79 @@
+package com.alibaba.easyexcel.test.temp.poi;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.alibaba.easyexcel.test.core.encrypt.EncryptData;
+import com.alibaba.easyexcel.test.core.simple.SimpleData;
+import com.alibaba.easyexcel.test.util.TestFileUtil;
+import com.alibaba.excel.EasyExcel;
+
+import org.apache.poi.poifs.crypt.EncryptionInfo;
+import org.apache.poi.poifs.crypt.EncryptionMode;
+import org.apache.poi.poifs.crypt.Encryptor;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * TODO
+ *
+ * @author Jiaju Zhuang
+ */
+@Ignore
+public class PoiEncryptTest {
+ @Test
+ public void encrypt() throws Exception {
+
+
+
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook);
+
+ Sheet sheet = sxssfWorkbook.createSheet("sheet1");
+ sheet.createRow(0).createCell(0).setCellValue("T2");
+
+
+ POIFSFileSystem fs = new POIFSFileSystem();
+ EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
+
+ Encryptor enc = info.getEncryptor();
+ enc.confirmPassword("123456");
+
+ // write the workbook into the encrypted OutputStream
+ OutputStream encos = enc.getDataStream(fs);
+ sxssfWorkbook.write(encos);
+ sxssfWorkbook.dispose();
+ sxssfWorkbook.close();
+ encos.close(); // this is necessary before writing out the FileSystem
+
+ OutputStream os = new FileOutputStream(
+ TestFileUtil.createNewFile("encrypt" + System.currentTimeMillis() + ".xlsx"));
+ fs.writeFilesystem(os);
+ os.close();
+ fs.close();
+ }
+
+ @Test
+ public void encryptExcel() throws Exception {
+ EasyExcel.write(TestFileUtil.createNewFile("encryptv2" + System.currentTimeMillis() + ".xlsx"),
+ EncryptData.class).password("123456")
+ .sheet().doWrite(data());
+ }
+
+ private List data() {
+ List list = new ArrayList<>();
+ for (int i = 0; i < 10; i++) {
+ SimpleData simpleData = new SimpleData();
+ simpleData.setName("姓名" + i);
+ list.add(simpleData);
+ }
+ return list;
+ }
+
+}