());
+ xlsxReadSheetHolder.setCellMap(new LinkedHashMap<>());
}
}
diff --git a/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java b/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java
index 5c778a84..e763ab37 100644
--- a/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java
+++ b/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java
@@ -23,7 +23,7 @@ public @interface ExcelProperty {
*
* write: It automatically merges when you have more than one head
*
- * read: When you have multiple heads, take the first one
+ * read: When you have multiple heads, take the last one
*
* @return The name of the sheet header
*/
@@ -32,7 +32,7 @@ public @interface ExcelProperty {
/**
* Index of column
*
- * Read or write it on the index of column,If it's equal to -1, it's sorted by Java class.
+ * Read or write it on the index of column, If it's equal to -1, it's sorted by Java class.
*
* priority: index > order > default sort
*
diff --git a/src/main/java/com/alibaba/excel/annotation/write/style/ContentFontStyle.java b/src/main/java/com/alibaba/excel/annotation/write/style/ContentFontStyle.java
index d593aef3..533ae6aa 100644
--- a/src/main/java/com/alibaba/excel/annotation/write/style/ContentFontStyle.java
+++ b/src/main/java/com/alibaba/excel/annotation/write/style/ContentFontStyle.java
@@ -52,7 +52,7 @@ public @interface ContentFontStyle {
short color() default -1;
/**
- * Set normal,super or subscript.
+ * Set normal, super or subscript.
*
* @see Font#SS_NONE
* @see Font#SS_SUPER
diff --git a/src/main/java/com/alibaba/excel/annotation/write/style/HeadFontStyle.java b/src/main/java/com/alibaba/excel/annotation/write/style/HeadFontStyle.java
index 957e50f5..d25dad77 100644
--- a/src/main/java/com/alibaba/excel/annotation/write/style/HeadFontStyle.java
+++ b/src/main/java/com/alibaba/excel/annotation/write/style/HeadFontStyle.java
@@ -52,7 +52,7 @@ public @interface HeadFontStyle {
short color() default -1;
/**
- * Set normal,super or subscript.
+ * Set normal, super or subscript.
*
* @see Font#SS_NONE
* @see Font#SS_SUPER
diff --git a/src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java b/src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java
index d882da8f..5bc3ffc6 100644
--- a/src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java
+++ b/src/main/java/com/alibaba/excel/annotation/write/style/HeadStyle.java
@@ -53,7 +53,6 @@ public @interface HeadStyle {
/**
* Set whether the text should be wrapped. Setting this flag to true
make all content visible within a
* cell by displaying it on multiple lines
- *
*/
boolean wrapped() default true;
@@ -108,7 +107,6 @@ public @interface HeadStyle {
* Set the color to use for the right border
*
* @see IndexedColors
- *
*/
short rightBorderColor() default -1;
@@ -116,7 +114,6 @@ public @interface HeadStyle {
* Set the color to use for the top border
*
* @see IndexedColors
- *
*/
short topBorderColor() default -1;
@@ -124,7 +121,6 @@ public @interface HeadStyle {
* Set the color to use for the bottom border
*
* @see IndexedColors
- *
*/
short bottomBorderColor() default -1;
@@ -139,7 +135,6 @@ public @interface HeadStyle {
* Set the background fill color.
*
* @see IndexedColors
- *
*/
short fillBackgroundColor() default -1;
@@ -147,7 +142,6 @@ public @interface HeadStyle {
* Set the foreground fill color Note: Ensure Foreground color is set prior to background color.
*
* @see IndexedColors
- *
*/
short fillForegroundColor() default -1;
diff --git a/src/main/java/com/alibaba/excel/cache/Ehcache.java b/src/main/java/com/alibaba/excel/cache/Ehcache.java
index 4e757fcd..a5250900 100644
--- a/src/main/java/com/alibaba/excel/cache/Ehcache.java
+++ b/src/main/java/com/alibaba/excel/cache/Ehcache.java
@@ -1,50 +1,49 @@
package com.alibaba.excel.cache;
import java.io.File;
-import java.util.HashMap;
+import java.util.ArrayList;
import java.util.UUID;
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.util.FileUtils;
+import com.alibaba.excel.util.ListUtils;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.MemoryUnit;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.alibaba.excel.context.AnalysisContext;
-import com.alibaba.excel.util.CollectionUtils;
-import com.alibaba.excel.util.FileUtils;
/**
* Default cache
*
* @author Jiaju Zhuang
*/
+@Slf4j
public class Ehcache implements ReadCache {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(Ehcache.class);
- private static final int BATCH_COUNT = 1000;
- private static final int DEBUG_WRITE_SIZE = 100 * 10000;
- private static final int DEBUG_CACHE_MISS_SIZE = 1000;
+ public static final int BATCH_COUNT = 1000;
/**
* Key index
*/
- private int index = 0;
- private HashMap dataMap = new HashMap(BATCH_COUNT * 4 / 3 + 1);
- private static CacheManager fileCacheManager;
- private static CacheConfiguration fileCacheConfiguration;
- private static CacheManager activeCacheManager;
- private CacheConfiguration activeCacheConfiguration;
+ private int activeIndex = 0;
+ public static final int DEBUG_CACHE_MISS_SIZE = 1000;
+ public static final int DEBUG_WRITE_SIZE = 100 * 10000;
+ private ArrayList dataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
+ private static final CacheManager FILE_CACHE_MANAGER;
+ private static final CacheConfiguration FILE_CACHE_CONFIGURATION;
+ private static final CacheManager ACTIVE_CACHE_MANAGER;
+ private final CacheConfiguration activeCacheConfiguration;
/**
* Bulk storage data
*/
- private org.ehcache.Cache fileCache;
+ private org.ehcache.Cache fileCache;
/**
* Currently active cache
*/
- private org.ehcache.Cache activeCache;
+ private org.ehcache.Cache activeCache;
private String cacheAlias;
/**
* Count the number of cache misses
@@ -53,7 +52,7 @@ public class Ehcache implements ReadCache {
public Ehcache(int maxCacheActivateSize) {
activeCacheConfiguration = CacheConfigurationBuilder
- .newCacheConfigurationBuilder(Integer.class, HashMap.class,
+ .newCacheConfigurationBuilder(Integer.class, ArrayList.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(maxCacheActivateSize, MemoryUnit.MB))
.withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(maxCacheActivateSize, MemoryUnit.MB)
.build();
@@ -61,11 +60,11 @@ public class Ehcache implements ReadCache {
static {
File cacheFile = FileUtils.createCacheTmpFile();
- fileCacheManager =
+ FILE_CACHE_MANAGER =
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(cacheFile)).build(true);
- activeCacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);
- fileCacheConfiguration = CacheConfigurationBuilder
- .newCacheConfigurationBuilder(Integer.class, HashMap.class,
+ ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true);
+ FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder
+ .newCacheConfigurationBuilder(Integer.class, ArrayList.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB))
.withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(10, MemoryUnit.GB).build();
}
@@ -73,21 +72,22 @@ public class Ehcache implements ReadCache {
@Override
public void init(AnalysisContext analysisContext) {
cacheAlias = UUID.randomUUID().toString();
- fileCache = fileCacheManager.createCache(cacheAlias, fileCacheConfiguration);
- activeCache = activeCacheManager.createCache(cacheAlias, activeCacheConfiguration);
+ fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
+ activeCache = ACTIVE_CACHE_MANAGER.createCache(cacheAlias, activeCacheConfiguration);
}
@Override
public void put(String value) {
- dataMap.put(index, value);
- if ((index + 1) % BATCH_COUNT == 0) {
- fileCache.put(index / BATCH_COUNT, dataMap);
- dataMap = new HashMap(BATCH_COUNT * 4 / 3 + 1);
+ dataList.add(value);
+ if (dataList.size() >= BATCH_COUNT) {
+ fileCache.put(activeIndex, dataList);
+ activeIndex++;
+ dataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
- index++;
- if (LOGGER.isDebugEnabled()) {
- if (index % DEBUG_WRITE_SIZE == 0) {
- LOGGER.debug("Already put :{}", index);
+ if (log.isDebugEnabled()) {
+ int alreadyPut = activeIndex * BATCH_COUNT + dataList.size();
+ if (alreadyPut % DEBUG_WRITE_SIZE == 0) {
+ log.debug("Already put :{}", alreadyPut);
}
}
}
@@ -98,31 +98,31 @@ public class Ehcache implements ReadCache {
return null;
}
int route = key / BATCH_COUNT;
- HashMap dataMap = activeCache.get(route);
- if (dataMap == null) {
- dataMap = fileCache.get(route);
- activeCache.put(route, dataMap);
- if (LOGGER.isDebugEnabled()) {
+ ArrayList dataList = activeCache.get(route);
+ if (dataList == null) {
+ dataList = fileCache.get(route);
+ activeCache.put(route, dataList);
+ if (log.isDebugEnabled()) {
if (cacheMiss++ % DEBUG_CACHE_MISS_SIZE == 0) {
- LOGGER.debug("Cache misses count:{}", cacheMiss);
+ log.debug("Cache misses count:{}", cacheMiss);
}
}
}
- return dataMap.get(key);
+ return dataList.get(key % BATCH_COUNT);
}
@Override
public void putFinished() {
- if (CollectionUtils.isEmpty(dataMap)) {
+ if (CollectionUtils.isEmpty(dataList)) {
return;
}
- fileCache.put(index / BATCH_COUNT, dataMap);
+ fileCache.put(activeIndex, dataList);
}
@Override
public void destroy() {
- fileCacheManager.removeCache(cacheAlias);
- activeCacheManager.removeCache(cacheAlias);
+ FILE_CACHE_MANAGER.removeCache(cacheAlias);
+ ACTIVE_CACHE_MANAGER.removeCache(cacheAlias);
}
}
diff --git a/src/main/java/com/alibaba/excel/cache/MapCache.java b/src/main/java/com/alibaba/excel/cache/MapCache.java
index ae948fbc..f83a1233 100644
--- a/src/main/java/com/alibaba/excel/cache/MapCache.java
+++ b/src/main/java/com/alibaba/excel/cache/MapCache.java
@@ -1,26 +1,24 @@
package com.alibaba.excel.cache;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.List;
import com.alibaba.excel.context.AnalysisContext;
/**
- *
* Putting temporary data directly into a map is a little more efficient but very memory intensive
*
* @author Jiaju Zhuang
*/
public class MapCache implements ReadCache {
- private Map cache = new HashMap();
- private int index = 0;
+ private List cache = new ArrayList<>();
@Override
public void init(AnalysisContext analysisContext) {}
@Override
public void put(String value) {
- cache.put(index++, value);
+ cache.add(value);
}
@Override
diff --git a/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java b/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
index 95eb0b8f..ec7d5c9a 100644
--- a/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
+++ b/src/main/java/com/alibaba/excel/constant/BuiltinFormats.java
@@ -1,6 +1,9 @@
package com.alibaba.excel.constant;
import java.util.Locale;
+import java.util.Map;
+
+import com.alibaba.excel.util.MapUtils;
/**
* Excel's built-in format conversion.Currently only supports Chinese.
@@ -17,7 +20,9 @@ import java.util.Locale;
**/
public class BuiltinFormats {
- private static final String[] BUILTIN_FORMATS_CN = {
+ public static short GENERAL = 0;
+
+ public static final String[] BUILTIN_FORMATS_CN = {
// 0
"General",
// 1
@@ -189,7 +194,7 @@ public class BuiltinFormats {
// end
};
- private static final String[] BUILTIN_FORMATS_US = {
+ public static final String[] BUILTIN_FORMATS_US = {
// 0
"General",
// 1
@@ -361,7 +366,11 @@ public class BuiltinFormats {
// end
};
- public static String getBuiltinFormat(Integer index, String defaultFormat, Locale locale) {
+ public static final Map BUILTIN_FORMATS_MAP_CN = buildMap(BUILTIN_FORMATS_CN);
+ public static final Map BUILTIN_FORMATS_MAP_US = buildMap(BUILTIN_FORMATS_US);
+ public static final short MIN_CUSTOM_DATA_FORMAT_INDEX = 82;
+
+ public static String getBuiltinFormat(Short index, String defaultFormat, Locale locale) {
String[] builtinFormat = switchBuiltinFormats(locale);
if (index == null || index < 0 || index >= builtinFormat.length) {
return defaultFormat;
@@ -369,11 +378,26 @@ public class BuiltinFormats {
return builtinFormat[index];
}
- private static String[] switchBuiltinFormats(Locale locale) {
+ public static String[] switchBuiltinFormats(Locale locale) {
if (locale != null && Locale.US.getCountry().equals(locale.getCountry())) {
return BUILTIN_FORMATS_US;
}
return BUILTIN_FORMATS_CN;
}
+ public static Map switchBuiltinFormatsMap(Locale locale) {
+ if (locale != null && Locale.US.getCountry().equals(locale.getCountry())) {
+ return BUILTIN_FORMATS_MAP_US;
+ }
+ return BUILTIN_FORMATS_MAP_CN;
+ }
+
+ private static Map buildMap(String[] builtinFormats) {
+ Map map = MapUtils.newHashMapWithExpectedSize(builtinFormats.length);
+ for (int i = 0; i < builtinFormats.length; i++) {
+ map.put(builtinFormats[i], (short)i);
+ }
+ return map;
+ }
+
}
diff --git a/src/main/java/com/alibaba/excel/constant/OrderConstant.java b/src/main/java/com/alibaba/excel/constant/OrderConstant.java
new file mode 100644
index 00000000..54d438e5
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/constant/OrderConstant.java
@@ -0,0 +1,24 @@
+package com.alibaba.excel.constant;
+
+/**
+ * Order constant.
+ *
+ * @author Jiaju Zhuang
+ */
+public class OrderConstant {
+
+ /**
+ * Define style.
+ */
+ public static final int DEFINE_STYLE = -50000;
+
+ /**
+ * default order.
+ */
+ public static final int DEFAULT_ORDER = 0;
+
+ /**
+ * Sorting of styles written to cells.
+ */
+ public static final int FILL_STYLE = 50000;
+}
diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContext.java b/src/main/java/com/alibaba/excel/context/AnalysisContext.java
index df17f55d..37039cbc 100644
--- a/src/main/java/com/alibaba/excel/context/AnalysisContext.java
+++ b/src/main/java/com/alibaba/excel/context/AnalysisContext.java
@@ -4,7 +4,6 @@ import java.io.InputStream;
import java.util.List;
import com.alibaba.excel.event.AnalysisEventListener;
-import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
@@ -92,15 +91,6 @@ public interface AnalysisContext {
*/
void readSheetList(List readSheetList);
- /**
- * get current sheet
- *
- * @return current analysis sheet
- * @deprecated please use {@link #readSheetHolder()}
- */
- @Deprecated
- Sheet getCurrentSheet();
-
/**
*
* get excel type
diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
index ab27e87e..4edaef37 100644
--- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
+++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
@@ -3,17 +3,15 @@ package com.alibaba.excel.context;
import java.io.InputStream;
import java.util.List;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.alibaba.excel.exception.ExcelAnalysisException;
-import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
+import com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder;
+import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
@@ -22,12 +20,13 @@ import com.alibaba.excel.read.processor.AnalysisEventProcessor;
import com.alibaba.excel.read.processor.DefaultAnalysisEventProcessor;
import com.alibaba.excel.support.ExcelTypeEnum;
+import lombok.extern.slf4j.Slf4j;
+
/**
- *
* @author jipengfei
*/
+@Slf4j
public class AnalysisContextImpl implements AnalysisContext {
- private static final Logger LOGGER = LoggerFactory.getLogger(AnalysisContextImpl.class);
/**
* The Workbook currently written
*/
@@ -60,13 +59,16 @@ public class AnalysisContextImpl implements AnalysisContext {
case XLSX:
readWorkbookHolder = new XlsxReadWorkbookHolder(readWorkbook);
break;
+ case CSV:
+ readWorkbookHolder = new CsvReadWorkbookHolder(readWorkbook);
+ break;
default:
break;
}
currentReadHolder = readWorkbookHolder;
analysisEventProcessor = new DefaultAnalysisEventProcessor();
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Initialization 'AnalysisContextImpl' complete");
+ if (log.isDebugEnabled()) {
+ log.debug("Initialization 'AnalysisContextImpl' complete");
}
}
@@ -79,6 +81,9 @@ public class AnalysisContextImpl implements AnalysisContext {
case XLSX:
readSheetHolder = new XlsxReadSheetHolder(readSheet, readWorkbookHolder);
break;
+ case CSV:
+ readSheetHolder = new CsvReadSheetHolder(readSheet, readWorkbookHolder);
+ break;
default:
break;
}
@@ -87,8 +92,8 @@ public class AnalysisContextImpl implements AnalysisContext {
throw new ExcelAnalysisException("Cannot read sheet repeatedly.");
}
readWorkbookHolder.getHasReadSheet().add(readSheetHolder.getSheetNo());
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Began to read:{}", readSheetHolder);
+ if (log.isDebugEnabled()) {
+ log.debug("Began to read:{}", readSheetHolder);
}
}
@@ -137,16 +142,6 @@ public class AnalysisContextImpl implements AnalysisContext {
}
- @Override
- public Sheet getCurrentSheet() {
- Sheet sheet = new Sheet(readSheetHolder.getSheetNo() + 1);
- sheet.setSheetName(readSheetHolder.getSheetName());
- sheet.setHead(readSheetHolder.getHead());
- sheet.setClazz(readSheetHolder.getClazz());
- sheet.setHeadLineMun(readSheetHolder.getHeadRowNumber());
- return sheet;
- }
-
@Override
public ExcelTypeEnum getExcelType() {
return readWorkbookHolder.getExcelType();
diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
index c1f89f02..c9be2233 100644
--- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
+++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
@@ -6,26 +6,10 @@ import java.io.OutputStream;
import java.util.Map;
import java.util.UUID;
-import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
-import org.apache.poi.openxml4j.opc.OPCPackage;
-import org.apache.poi.openxml4j.opc.PackageAccess;
-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.Cell;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.util.CellRangeAddress;
-import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.alibaba.excel.enums.WriteTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException;
-import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.FileUtils;
@@ -42,6 +26,22 @@ import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.property.ExcelWriteHeadProperty;
+import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackageAccess;
+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.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* A context is the main anchorage point of a excel writer.
*
@@ -50,6 +50,7 @@ import com.alibaba.excel.write.property.ExcelWriteHeadProperty;
public class WriteContextImpl implements WriteContext {
private static final Logger LOGGER = LoggerFactory.getLogger(WriteContextImpl.class);
+ private static final String NO_SHEETS = "no sheets";
/**
* The Workbook currently written
@@ -178,8 +179,12 @@ public class WriteContextImpl implements WriteContext {
writeSheetHolder
.setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheet(writeSheetHolder.getSheetName()));
}
- } catch (Exception e) {
- currentSheet = createSheet();
+ } catch (IllegalArgumentException e) {
+ if (e.getMessage() != null && e.getMessage().contains(NO_SHEETS)) {
+ currentSheet = createSheet();
+ } else {
+ throw e;
+ }
}
if (currentSheet == null) {
currentSheet = createSheet();
@@ -240,10 +245,16 @@ public class WriteContextImpl implements WriteContext {
Head head = entry.getValue();
int columnIndex = entry.getKey();
WriteHandlerUtils.beforeCellCreate(this, row, head, columnIndex, relativeRowIndex, Boolean.TRUE);
+
Cell cell = row.createCell(columnIndex);
+
WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE);
- cell.setCellValue(head.getHeadNameList().get(relativeRowIndex));
- WriteHandlerUtils.afterCellDispose(this, (CellData) null, cell, head, relativeRowIndex, Boolean.TRUE);
+
+ WriteCellData writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex));
+ cell.setCellValue(writeCellData.getStringValue());
+
+ WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, head, relativeRowIndex,
+ Boolean.TRUE);
}
}
@@ -280,7 +291,7 @@ public class WriteContextImpl implements WriteContext {
}
private void initCurrentTableHolder(WriteTable writeTable) {
- writeTableHolder = new WriteTableHolder(writeTable, writeSheetHolder, writeWorkbookHolder);
+ writeTableHolder = new WriteTableHolder(writeTable, writeSheetHolder);
writeSheetHolder.getHasBeenInitializedTable().put(writeTable.getTableNo(), writeTableHolder);
currentWriteHolder = writeTableHolder;
if (LOGGER.isDebugEnabled()) {
@@ -346,7 +357,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;
@@ -426,7 +437,7 @@ public class WriteContextImpl implements WriteContext {
if (writeWorkbookHolder.getFile() != null) {
return false;
}
- File tempXlsx = FileUtils.createTmpFile(UUID.randomUUID().toString() + ".xlsx");
+ File tempXlsx = FileUtils.createTmpFile(UUID.randomUUID() + ".xlsx");
FileOutputStream tempFileOutputStream = new FileOutputStream(tempXlsx);
try {
writeWorkbookHolder.getWorkbook().write(tempFileOutputStream);
@@ -441,14 +452,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!");
}
@@ -467,19 +473,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();
- }
}
}
@@ -487,15 +483,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/context/csv/CsvReadContext.java b/src/main/java/com/alibaba/excel/context/csv/CsvReadContext.java
new file mode 100644
index 00000000..00bcd54a
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/context/csv/CsvReadContext.java
@@ -0,0 +1,26 @@
+package com.alibaba.excel.context.csv;
+
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder;
+import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder;
+
+/**
+ * A context is the main anchorage point of a ls xls reader.
+ *
+ * @author Jiaju Zhuang
+ **/
+public interface CsvReadContext extends AnalysisContext {
+ /**
+ * All information about the workbook you are currently working on.
+ *
+ * @return Current workbook holder
+ */
+ CsvReadWorkbookHolder csvReadWorkbookHolder();
+
+ /**
+ * All information about the sheet you are currently working on.
+ *
+ * @return Current sheet holder
+ */
+ CsvReadSheetHolder csvReadSheetHolder();
+}
diff --git a/src/main/java/com/alibaba/excel/context/csv/DefaultCsvReadContext.java b/src/main/java/com/alibaba/excel/context/csv/DefaultCsvReadContext.java
new file mode 100644
index 00000000..d8653a88
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/context/csv/DefaultCsvReadContext.java
@@ -0,0 +1,29 @@
+package com.alibaba.excel.context.csv;
+
+import com.alibaba.excel.context.AnalysisContextImpl;
+import com.alibaba.excel.read.metadata.ReadWorkbook;
+import com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder;
+import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder;
+import com.alibaba.excel.support.ExcelTypeEnum;
+
+/**
+ * A context is the main anchorage point of a ls xls reader.
+ *
+ * @author Jiaju Zhuang
+ */
+public class DefaultCsvReadContext extends AnalysisContextImpl implements CsvReadContext {
+
+ public DefaultCsvReadContext(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) {
+ super(readWorkbook, actualExcelType);
+ }
+
+ @Override
+ public CsvReadWorkbookHolder csvReadWorkbookHolder() {
+ return (CsvReadWorkbookHolder)readWorkbookHolder();
+ }
+
+ @Override
+ public CsvReadSheetHolder csvReadSheetHolder() {
+ return (CsvReadSheetHolder)readSheetHolder();
+ }
+}
diff --git a/src/main/java/com/alibaba/excel/converters/AutoConverter.java b/src/main/java/com/alibaba/excel/converters/AutoConverter.java
index 800d2d85..5981e7ac 100644
--- a/src/main/java/com/alibaba/excel/converters/AutoConverter.java
+++ b/src/main/java/com/alibaba/excel/converters/AutoConverter.java
@@ -1,36 +1,9 @@
package com.alibaba.excel.converters;
-import com.alibaba.excel.enums.CellDataTypeEnum;
-import com.alibaba.excel.metadata.CellData;
-import com.alibaba.excel.metadata.GlobalConfiguration;
-import com.alibaba.excel.metadata.property.ExcelContentProperty;
-
/**
* An empty converter.It's automatically converted by type.
*
* @author Jiaju Zhuang
*/
-public class AutoConverter implements Converter {
-
- @Override
- public Class supportJavaTypeKey() {
- return null;
- }
-
- @Override
- public CellDataTypeEnum supportExcelTypeKey() {
- return null;
- }
-
- @Override
- public Object convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
- GlobalConfiguration globalConfiguration) {
- return null;
- }
-
- @Override
- public CellData convertToExcelData(Object value, ExcelContentProperty contentProperty,
- GlobalConfiguration globalConfiguration) {
- return null;
- }
+public class AutoConverter implements Converter