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。 *