zhuangjiaju
5 years ago
67 changed files with 1896 additions and 1324 deletions
@ -1,11 +1,13 @@
|
||||
package com.alibaba.excel.analysis.v07; |
||||
|
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
public interface XlsxRowResultHolder { |
||||
void clearResult(); |
||||
|
||||
void appendCurrentCellValue(String currentCellValue); |
||||
|
||||
String[] getCurRowContent(); |
||||
CellData[] getCurRowContent(); |
||||
|
||||
int getColumnSize(); |
||||
} |
||||
|
@ -1,37 +1,47 @@
|
||||
package com.alibaba.excel.context; |
||||
|
||||
import java.io.OutputStream; |
||||
|
||||
import org.apache.poi.ss.usermodel.CellStyle; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
|
||||
import com.alibaba.excel.converters.ConverterRegistryCenter; |
||||
import com.alibaba.excel.event.WriteHandler; |
||||
import com.alibaba.excel.metadata.ExcelHeadProperty; |
||||
import com.alibaba.excel.metadata.SheetHolder; |
||||
import com.alibaba.excel.metadata.Table; |
||||
import com.alibaba.excel.metadata.holder.ConfigurationSelector; |
||||
|
||||
/** |
||||
* Write context |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public interface WriteContext { |
||||
|
||||
Sheet getCurrentSheet(); |
||||
|
||||
boolean needHead(); |
||||
|
||||
ExcelHeadProperty getExcelHeadProperty(); |
||||
|
||||
/** |
||||
* If the current sheet already exists, select it; if not, create it |
||||
* |
||||
* @param sheet |
||||
*/ |
||||
void currentSheet(com.alibaba.excel.metadata.Sheet sheet); |
||||
|
||||
/** |
||||
* If the current table already exists, select it; if not, create it |
||||
* |
||||
* @param table |
||||
*/ |
||||
void currentTable(Table table); |
||||
|
||||
OutputStream getOutputStream(); |
||||
/** |
||||
* Configuration of currently operated cell |
||||
* |
||||
* @return |
||||
*/ |
||||
ConfigurationSelector configurationSelector(); |
||||
|
||||
Workbook getWorkbook(); |
||||
Sheet getCurrentSheet(); |
||||
|
||||
@Deprecated |
||||
WriteHandler getWriteHandler(); |
||||
ExcelHeadProperty getExcelHeadProperty(); |
||||
|
||||
CellStyle getCurrentContentStyle(); |
||||
Workbook getWorkbook(); |
||||
|
||||
ConverterRegistryCenter getConverterRegistryCenter(); |
||||
/** |
||||
* close |
||||
*/ |
||||
void finish(); |
||||
} |
||||
|
@ -1,33 +0,0 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
|
||||
public class BigDecimalConverter implements Converter { |
||||
@Override |
||||
public String getName() { |
||||
return "big-decimal-converter"; |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(ExcelColumnProperty columnProperty) { |
||||
return BigDecimal.class.equals(columnProperty.getField().getType()); |
||||
} |
||||
@Override |
||||
public Object convert(String value, ExcelColumnProperty columnProperty) { |
||||
return new BigDecimal(value); |
||||
} |
||||
@Override |
||||
public Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty) { |
||||
cell.setCellValue(Double.parseDouble(value.toString())); |
||||
return cell; |
||||
} |
||||
@Override |
||||
public boolean support(Object cellValue) { |
||||
return cellValue instanceof BigDecimal; |
||||
} |
||||
|
||||
} |
@ -1,42 +0,0 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import java.lang.reflect.Field; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
|
||||
public class BooleanConverter implements Converter { |
||||
@Override |
||||
public String getName() { |
||||
return "boolean-converter"; |
||||
} |
||||
@Override |
||||
public boolean support(ExcelColumnProperty columnProperty) { |
||||
Field field = columnProperty.getField(); |
||||
return Boolean.class.equals(field.getType()) || boolean.class.equals(field.getType()); |
||||
} |
||||
@Override |
||||
public Object convert(String value, ExcelColumnProperty columnProperty) { |
||||
String valueLower = value.toLowerCase(); |
||||
if (valueLower.equals("true") || valueLower.equals("false")) { |
||||
return Boolean.parseBoolean(value.toLowerCase()); |
||||
} |
||||
Integer integer = Integer.parseInt(value); |
||||
if (integer == 0) { |
||||
return false; |
||||
} else { |
||||
return true; |
||||
} |
||||
} |
||||
@Override |
||||
public Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty) { |
||||
cell.setCellValue(String.valueOf(value)); |
||||
return cell; |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Object cellValue) { |
||||
return cellValue instanceof Boolean || boolean.class.equals(cellValue.getClass()); |
||||
} |
||||
} |
@ -1,13 +1,17 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
import com.sun.istack.internal.NotNull; |
||||
|
||||
public interface Converter<T> { |
||||
|
||||
Class supportJavaTypeKey(); |
||||
|
||||
CellDataTypeEnum supportExcelTypeKey(); |
||||
|
||||
T convertToJavaData(@NotNull CellData cellData, ExcelColumnProperty columnProperty) throws Exception; |
||||
|
||||
public interface Converter { |
||||
String getName(); |
||||
boolean support(ExcelColumnProperty columnProperty); |
||||
boolean support(Object cellValue); |
||||
Object convert(String value, ExcelColumnProperty columnProperty); |
||||
Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty); |
||||
CellData convertToExcelData(@NotNull T value, ExcelColumnProperty columnProperty) throws Exception; |
||||
} |
||||
|
@ -0,0 +1,48 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
|
||||
/** |
||||
* Converter unique key |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ConverterKey { |
||||
|
||||
private Class javaTypeKey; |
||||
private CellDataTypeEnum excelTypeKey; |
||||
|
||||
public ConverterKey(Class javaTypeKey, CellDataTypeEnum excelTypeKey) { |
||||
if (javaTypeKey == null || excelTypeKey == null) { |
||||
throw new IllegalArgumentException("All parameters can not be null"); |
||||
} |
||||
this.javaTypeKey = javaTypeKey; |
||||
this.excelTypeKey = excelTypeKey; |
||||
} |
||||
|
||||
public static ConverterKey buildConverterKey(Class javaTypeKey, CellDataTypeEnum excelTypeKey) { |
||||
return new ConverterKey(javaTypeKey, excelTypeKey); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object o) { |
||||
if (this == o) { |
||||
return true; |
||||
} |
||||
if (o == null || getClass() != o.getClass()) { |
||||
return false; |
||||
} |
||||
ConverterKey that = (ConverterKey)o; |
||||
if (javaTypeKey != null ? !javaTypeKey.equals(that.javaTypeKey) : that.javaTypeKey != null) { |
||||
return false; |
||||
} |
||||
return excelTypeKey == that.excelTypeKey; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int result = javaTypeKey != null ? javaTypeKey.hashCode() : 0; |
||||
result = 31 * result + (excelTypeKey != null ? excelTypeKey.hashCode() : 0); |
||||
return result; |
||||
} |
||||
} |
@ -1,55 +0,0 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFDateUtil; |
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
import com.alibaba.excel.util.TypeUtil; |
||||
|
||||
public class DateConverter implements Converter { |
||||
private final AnalysisContext context; |
||||
|
||||
public DateConverter(AnalysisContext context) { |
||||
this.context = context; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "date-converter"; |
||||
} |
||||
@Override |
||||
public boolean support(ExcelColumnProperty columnProperty) { |
||||
return Date.class.equals(columnProperty.getField().getType()); |
||||
} |
||||
|
||||
@Override |
||||
public Object convert(String value, ExcelColumnProperty columnProperty) { |
||||
if (value.contains("-") || value.contains("/") || value.contains(":")) { |
||||
return TypeUtil.getSimpleDateFormatDate(value, columnProperty.getFormat()); |
||||
} else { |
||||
Double d = Double.parseDouble(value); |
||||
return HSSFDateUtil.getJavaDate(d, context != null ? context.use1904WindowDate() : false); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty) { |
||||
Date d = (Date)value; |
||||
if (columnProperty != null && StringUtils.isEmpty(columnProperty.getFormat()) == false) { |
||||
cell.setCellValue(TypeUtil.formatDate(d, columnProperty.getFormat())); |
||||
} else { |
||||
cell.setCellValue(d); |
||||
} |
||||
|
||||
return cell; |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Object cellValue) { |
||||
return cellValue instanceof Date; |
||||
} |
||||
} |
@ -1,33 +0,0 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import java.lang.reflect.Field; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
import com.alibaba.excel.util.TypeUtil; |
||||
|
||||
public class DoubleConverter implements Converter { |
||||
@Override |
||||
public String getName() { |
||||
return "double-converter"; |
||||
} |
||||
@Override |
||||
public boolean support(ExcelColumnProperty columnProperty) { |
||||
Field field = columnProperty.getField(); |
||||
return Double.class.equals(field.getType()) || double.class.equals(field.getType()); |
||||
} |
||||
@Override |
||||
public Object convert(String value, ExcelColumnProperty columnProperty) { |
||||
return TypeUtil.formatFloat(value); |
||||
} |
||||
@Override |
||||
public Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty) { |
||||
cell.setCellValue(Double.parseDouble(value.toString())); |
||||
return cell; |
||||
} |
||||
@Override |
||||
public boolean support(Object cellValue) { |
||||
return cellValue instanceof Double || double.class.equals(cellValue.getClass()); |
||||
} |
||||
} |
@ -1,31 +0,0 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
import com.alibaba.excel.util.TypeUtil; |
||||
|
||||
public class FloatConverter implements Converter { |
||||
@Override |
||||
public String getName() { |
||||
return "float-converter"; |
||||
} |
||||
@Override |
||||
public boolean support(ExcelColumnProperty columnProperty) { |
||||
return Float.class.equals(columnProperty.getField().getType()); |
||||
} |
||||
@Override |
||||
public Object convert(String value, ExcelColumnProperty columnProperty) { |
||||
return TypeUtil.formatFloat(value); |
||||
} |
||||
@Override |
||||
public Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty) { |
||||
cell.setCellValue(Double.parseDouble(value.toString())); |
||||
return cell; |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Object cellValue) { |
||||
return cellValue instanceof Float || float.class.equals(cellValue.getClass()); |
||||
} |
||||
} |
@ -1,33 +0,0 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import java.lang.reflect.Field; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
|
||||
public class IntegerConverter implements Converter { |
||||
@Override |
||||
public String getName() { |
||||
return "integer-converter"; |
||||
} |
||||
@Override |
||||
public boolean support(ExcelColumnProperty columnProperty) { |
||||
Field field = columnProperty.getField(); |
||||
return Integer.class.equals(field.getType()) || int.class.equals(field.getType()); |
||||
} |
||||
@Override |
||||
public Object convert(String value, ExcelColumnProperty columnProperty) { |
||||
return Integer.parseInt(value); |
||||
} |
||||
@Override |
||||
public Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty) { |
||||
cell.setCellValue(Double.parseDouble(value.toString())); |
||||
return cell; |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Object cellValue) { |
||||
return cellValue instanceof Integer || int.class.equals(cellValue.getClass()); |
||||
} |
||||
} |
@ -1,33 +0,0 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import java.lang.reflect.Field; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
|
||||
public class LongConverter implements Converter { |
||||
@Override |
||||
public String getName() { |
||||
return "long-converter"; |
||||
} |
||||
@Override |
||||
public boolean support(ExcelColumnProperty columnProperty) { |
||||
Field field = columnProperty.getField(); |
||||
return Long.class.equals(field.getType()) || long.class.equals(field.getType()); |
||||
} |
||||
@Override |
||||
public Object convert(String value, ExcelColumnProperty columnProperty) { |
||||
return Long.parseLong(value); |
||||
} |
||||
@Override |
||||
public Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty) { |
||||
cell.setCellValue(Double.parseDouble(value.toString())); |
||||
return cell; |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Object cellValue) { |
||||
return cellValue instanceof Long || long.class.equals(cellValue.getClass()); |
||||
} |
||||
} |
@ -1,31 +0,0 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
|
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
import com.alibaba.excel.util.TypeUtil; |
||||
|
||||
public class StringConverter implements Converter { |
||||
@Override |
||||
public String getName() { |
||||
return "string-converter"; |
||||
} |
||||
@Override |
||||
public boolean support(ExcelColumnProperty columnProperty) { |
||||
return String.class.equals(columnProperty.getField().getType()); |
||||
} |
||||
@Override |
||||
public Object convert(String value, ExcelColumnProperty columnProperty) { |
||||
return TypeUtil.formatFloat(value); |
||||
} |
||||
@Override |
||||
public Cell convert(Cell cell, Object value, ExcelColumnProperty columnProperty) { |
||||
cell.setCellValue(String.valueOf(value)); |
||||
return cell; |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Object cellValue) { |
||||
return cellValue instanceof String; |
||||
} |
||||
} |
@ -0,0 +1,42 @@
|
||||
package com.alibaba.excel.converters.bigdecimal; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
|
||||
/** |
||||
* BigDecimal and boolean converter |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class BigDecimalBooleanConverter implements Converter<BigDecimal> { |
||||
|
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return BigDecimal.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.BOOLEAN; |
||||
} |
||||
|
||||
@Override |
||||
public BigDecimal convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { |
||||
if (cellData.getBooleanValue()) { |
||||
return BigDecimal.ONE; |
||||
} |
||||
return BigDecimal.ZERO; |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(BigDecimal value, ExcelColumnProperty columnProperty) { |
||||
if (BigDecimal.ONE.equals(value)) { |
||||
return new CellData(Boolean.TRUE); |
||||
} |
||||
return new CellData(Boolean.FALSE); |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
package com.alibaba.excel.converters.bigdecimal; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
|
||||
/** |
||||
* BigDecimal and number converter |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class BigDecimalNumberConverter implements Converter<BigDecimal> { |
||||
|
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return BigDecimal.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.NUMBER; |
||||
} |
||||
|
||||
@Override |
||||
public BigDecimal convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { |
||||
return new BigDecimal(cellData.getDoubleValue()); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(BigDecimal value, ExcelColumnProperty columnProperty) { |
||||
return new CellData(value.doubleValue()); |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.excel.converters.bigdecimal; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.text.DecimalFormat; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
/** |
||||
* BigDecimal and string converter |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class BigDecimalStringConverter implements Converter<BigDecimal> { |
||||
|
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return BigDecimal.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.STRING; |
||||
} |
||||
|
||||
@Override |
||||
public BigDecimal convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { |
||||
return new BigDecimal(cellData.getStringValue()); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(BigDecimal value, ExcelColumnProperty columnProperty) { |
||||
if (StringUtils.isEmpty(columnProperty.getFormat())) { |
||||
return new CellData(value.toString()); |
||||
} |
||||
return new CellData(new DecimalFormat(columnProperty.getFormat()).format(value)); |
||||
} |
||||
} |
@ -0,0 +1,39 @@
|
||||
package com.alibaba.excel.converters.date; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFDateUtil; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
|
||||
/** |
||||
* Date and number converter |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class DateNumberConverter implements Converter<Date> { |
||||
|
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return Date.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.NUMBER; |
||||
} |
||||
|
||||
@Override |
||||
public Date convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) { |
||||
return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), columnProperty.getUse1904windowing(), |
||||
columnProperty.getTimeZone()); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(Date value, ExcelColumnProperty columnProperty) { |
||||
return new CellData((double)(value.getTime())); |
||||
} |
||||
} |
@ -0,0 +1,37 @@
|
||||
package com.alibaba.excel.converters.date; |
||||
|
||||
import java.text.ParseException; |
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
import com.alibaba.excel.util.DateUtils; |
||||
|
||||
/** |
||||
* Date and string converter |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class DateStringConverter implements Converter<Date> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return Date.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.STRING; |
||||
} |
||||
|
||||
@Override |
||||
public Date convertToJavaData(CellData cellData, ExcelColumnProperty columnProperty) throws ParseException { |
||||
return DateUtils.parseDate(cellData.getStringValue(), columnProperty.getFormat()); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(Date value, ExcelColumnProperty columnProperty) { |
||||
return new CellData(DateUtils.format(value, columnProperty.getFormat())); |
||||
} |
||||
} |
@ -0,0 +1,54 @@
|
||||
package com.alibaba.excel.enums; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
/** |
||||
* excel internal data type |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public enum CellDataTypeEnum { |
||||
/** |
||||
* string |
||||
*/ |
||||
STRING, |
||||
/** |
||||
* number |
||||
*/ |
||||
NUMBER, |
||||
/** |
||||
* boolean |
||||
*/ |
||||
BOOLEAN, |
||||
/** |
||||
* empty |
||||
*/ |
||||
EMPTY, |
||||
/** |
||||
* error |
||||
*/ |
||||
ERROR; |
||||
|
||||
private static final Map<String, CellDataTypeEnum> TYPE_ROUTING_MAP = new HashMap<String, CellDataTypeEnum>(8); |
||||
static { |
||||
TYPE_ROUTING_MAP.put("s", STRING); |
||||
TYPE_ROUTING_MAP.put("e", ERROR); |
||||
TYPE_ROUTING_MAP.put("b", BOOLEAN); |
||||
} |
||||
|
||||
/** |
||||
* Build data types |
||||
* |
||||
* @param cellType |
||||
* @return |
||||
*/ |
||||
public static CellDataTypeEnum buildFromCellType(String cellType) { |
||||
if (StringUtils.isEmpty(cellType)) { |
||||
return EMPTY; |
||||
} |
||||
return TYPE_ROUTING_MAP.get(cellType); |
||||
} |
||||
} |
@ -0,0 +1,16 @@
|
||||
package com.alibaba.excel.event; |
||||
|
||||
/** |
||||
* There are multiple interceptors that execute only one of them when fired once.If you want to control which one to |
||||
* execute please use {@link Order} |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
public interface NotRepeatExecutor { |
||||
/** |
||||
* To see if it's the same executor |
||||
* |
||||
* @return |
||||
*/ |
||||
String uniqueValue(); |
||||
} |
@ -0,0 +1,15 @@
|
||||
package com.alibaba.excel.event; |
||||
|
||||
/** |
||||
* Implement this interface when sorting |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public interface Order { |
||||
/** |
||||
* The smaller the first implementation |
||||
* |
||||
* @return |
||||
*/ |
||||
int order(); |
||||
} |
@ -1,42 +0,0 @@
|
||||
package com.alibaba.excel.event; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Row; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.write.handler.ExcelWriteHandler; |
||||
|
||||
/** |
||||
* |
||||
* @deprecated Separate implementations |
||||
* @see com.alibaba.excel.write.handler.SheetExcelWriteHandler |
||||
* @see com.alibaba.excel.write.handler.RowExcelWriteHandler |
||||
* @see com.alibaba.excel.write.handler.CellExcelWriteHandler |
||||
* @author jipengfei |
||||
*/ |
||||
public interface WriteHandler extends ExcelWriteHandler { |
||||
|
||||
/** |
||||
* Custom operation after creating each sheet |
||||
* |
||||
* @param sheetNo |
||||
* @param sheet |
||||
*/ |
||||
void sheet(int sheetNo, Sheet sheet); |
||||
|
||||
/** |
||||
* Custom operation after creating each row |
||||
* |
||||
* @param rowNum |
||||
* @param row |
||||
*/ |
||||
void row(int rowNum, Row row); |
||||
|
||||
/** |
||||
* Custom operation after creating each cell |
||||
* |
||||
* @param cellNum |
||||
* @param cell |
||||
*/ |
||||
void cell(int cellNum, Cell cell); |
||||
} |
@ -0,0 +1,21 @@
|
||||
package com.alibaba.excel.exception; |
||||
|
||||
/** |
||||
* Data convert exception |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ExcelDataConvertException extends RuntimeException { |
||||
|
||||
public ExcelDataConvertException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public ExcelDataConvertException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
public ExcelDataConvertException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
} |
@ -1,34 +0,0 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.usermodel.CellStyle; |
||||
|
||||
/** |
||||
* Excel基础模型 |
||||
* @author jipengfei |
||||
*/ |
||||
public class BaseRowModel { |
||||
|
||||
/** |
||||
* 每列样式 |
||||
*/ |
||||
private Map<Integer,CellStyle> cellStyleMap = new HashMap<Integer,CellStyle>(); |
||||
|
||||
public void addStyle(Integer row, CellStyle cellStyle){ |
||||
cellStyleMap.put(row,cellStyle); |
||||
} |
||||
|
||||
public CellStyle getStyle(Integer row){ |
||||
return cellStyleMap.get(row); |
||||
} |
||||
|
||||
public Map<Integer, CellStyle> getCellStyleMap() { |
||||
return cellStyleMap; |
||||
} |
||||
|
||||
public void setCellStyleMap(Map<Integer, CellStyle> cellStyleMap) { |
||||
this.cellStyleMap = cellStyleMap; |
||||
} |
||||
} |
@ -0,0 +1,138 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
|
||||
/** |
||||
* excel internal cell data |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class CellData { |
||||
private CellDataTypeEnum type; |
||||
/** |
||||
* {@link CellDataTypeEnum#NUMBER} |
||||
*/ |
||||
private Double doubleValue; |
||||
/** |
||||
* {@link CellDataTypeEnum#STRING} and{@link CellDataTypeEnum#ERROR} |
||||
*/ |
||||
private String stringValue; |
||||
/** |
||||
* {@link CellDataTypeEnum#BOOLEAN} |
||||
*/ |
||||
private Boolean booleanValue; |
||||
|
||||
/** |
||||
* Support only when reading |
||||
*/ |
||||
private Boolean readIsFormula; |
||||
/** |
||||
* Support only when reading |
||||
*/ |
||||
private String readFormula; |
||||
|
||||
public CellData(String stringValue) { |
||||
this(CellDataTypeEnum.STRING, stringValue); |
||||
} |
||||
|
||||
public CellData(CellDataTypeEnum type, String stringValue) { |
||||
if (type != CellDataTypeEnum.STRING && type != CellDataTypeEnum.ERROR) { |
||||
throw new IllegalArgumentException("Only support CellDataTypeEnum.STRING and CellDataTypeEnum.ERROR"); |
||||
} |
||||
if (stringValue == null) { |
||||
throw new IllegalArgumentException("StringValue can not be null"); |
||||
} |
||||
this.type = type; |
||||
this.stringValue = stringValue; |
||||
this.readIsFormula = Boolean.FALSE; |
||||
} |
||||
|
||||
public CellData(Double doubleValue) { |
||||
if (doubleValue == null) { |
||||
throw new IllegalArgumentException("DoubleValue can not be null"); |
||||
} |
||||
this.type = CellDataTypeEnum.NUMBER; |
||||
this.doubleValue = doubleValue; |
||||
this.readIsFormula = Boolean.FALSE; |
||||
} |
||||
|
||||
public CellData(Boolean booleanValue) { |
||||
if (booleanValue == null) { |
||||
throw new IllegalArgumentException("BooleanValue can not be null"); |
||||
} |
||||
this.type = CellDataTypeEnum.BOOLEAN; |
||||
this.booleanValue = booleanValue; |
||||
this.readIsFormula = Boolean.FALSE; |
||||
} |
||||
|
||||
public CellData(CellDataTypeEnum type) { |
||||
if (type == null) { |
||||
throw new IllegalArgumentException("Type can not be null"); |
||||
} |
||||
this.type = type; |
||||
this.readIsFormula = Boolean.FALSE; |
||||
} |
||||
|
||||
public CellDataTypeEnum getType() { |
||||
return type; |
||||
} |
||||
|
||||
public void setType(CellDataTypeEnum type) { |
||||
this.type = type; |
||||
} |
||||
|
||||
public Double getDoubleValue() { |
||||
return doubleValue; |
||||
} |
||||
|
||||
public void setDoubleValue(Double doubleValue) { |
||||
this.doubleValue = doubleValue; |
||||
} |
||||
|
||||
public String getStringValue() { |
||||
return stringValue; |
||||
} |
||||
|
||||
public void setStringValue(String stringValue) { |
||||
this.stringValue = stringValue; |
||||
} |
||||
|
||||
public Boolean getBooleanValue() { |
||||
return booleanValue; |
||||
} |
||||
|
||||
public void setBooleanValue(Boolean booleanValue) { |
||||
this.booleanValue = booleanValue; |
||||
} |
||||
|
||||
public Boolean getReadIsFormula() { |
||||
return readIsFormula; |
||||
} |
||||
|
||||
public void setReadIsFormula(Boolean readIsFormula) { |
||||
this.readIsFormula = readIsFormula; |
||||
} |
||||
|
||||
public String getReadFormula() { |
||||
return readFormula; |
||||
} |
||||
|
||||
public void setReadFormula(String readFormula) { |
||||
this.readFormula = readFormula; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
switch (type) { |
||||
case NUMBER: |
||||
return doubleValue.toString(); |
||||
case BOOLEAN: |
||||
return booleanValue.toString(); |
||||
case STRING: |
||||
case ERROR: |
||||
return stringValue; |
||||
default: |
||||
return "empty"; |
||||
} |
||||
} |
||||
} |
@ -1,104 +0,0 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.write.style.CellStyleStrategy; |
||||
import com.alibaba.excel.write.style.column.ColumnWidthStyleStrategy; |
||||
import com.alibaba.excel.write.style.row.RowHighStyleStrategy; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SheetHolder { |
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private Sheet sheet; |
||||
/*** |
||||
* has been initialized table |
||||
*/ |
||||
private Map<Integer, TableHolder> hasBeenInitializedTable; |
||||
/** |
||||
* the header attribute of excel |
||||
*/ |
||||
private ExcelHeadProperty excelHeadProperty; |
||||
|
||||
private CellStyleStrategy cellStyleStrategy; |
||||
|
||||
private ColumnWidthStyleStrategy columnWidthStyleStrategy; |
||||
private RowHighStyleStrategy rowHighStyleStrategy; |
||||
/** |
||||
* current param |
||||
*/ |
||||
private com.alibaba.excel.metadata.Sheet currentSheetParam; |
||||
|
||||
private boolean needHead = true; |
||||
|
||||
public com.alibaba.excel.metadata.Sheet getCurrentSheetParam() { |
||||
return currentSheetParam; |
||||
} |
||||
|
||||
public void setCurrentSheetParam(com.alibaba.excel.metadata.Sheet currentSheetParam) { |
||||
this.currentSheetParam = currentSheetParam; |
||||
} |
||||
|
||||
public RowHighStyleStrategy getRowHighStyleStrategy() { |
||||
return rowHighStyleStrategy; |
||||
} |
||||
|
||||
public void setRowHighStyleStrategy(RowHighStyleStrategy rowHighStyleStrategy) { |
||||
this.rowHighStyleStrategy = rowHighStyleStrategy; |
||||
} |
||||
|
||||
public ColumnWidthStyleStrategy getColumnWidthStyleStrategy() { |
||||
return columnWidthStyleStrategy; |
||||
} |
||||
|
||||
public void setColumnWidthStyleStrategy(ColumnWidthStyleStrategy columnWidthStyleStrategy) { |
||||
this.columnWidthStyleStrategy = columnWidthStyleStrategy; |
||||
} |
||||
|
||||
public boolean isNeedHead() { |
||||
return needHead; |
||||
} |
||||
|
||||
public void setNeedHead(boolean needHead) { |
||||
this.needHead = needHead; |
||||
} |
||||
|
||||
public ExcelHeadProperty getExcelHeadProperty() { |
||||
return excelHeadProperty; |
||||
} |
||||
|
||||
public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) { |
||||
this.excelHeadProperty = excelHeadProperty; |
||||
} |
||||
|
||||
public Sheet getSheet() { |
||||
return sheet; |
||||
} |
||||
|
||||
public void setSheet(Sheet sheet) { |
||||
this.sheet = sheet; |
||||
} |
||||
|
||||
public Map<Integer, TableHolder> getHasBeenInitializedTable() { |
||||
return hasBeenInitializedTable; |
||||
} |
||||
|
||||
public void setHasBeenInitializedTable(Map<Integer, TableHolder> hasBeenInitializedTable) { |
||||
this.hasBeenInitializedTable = hasBeenInitializedTable; |
||||
} |
||||
|
||||
public CellStyleStrategy getCellStyleStrategy() { |
||||
return cellStyleStrategy; |
||||
} |
||||
|
||||
public void setCellStyleStrategy(CellStyleStrategy cellStyleStrategy) { |
||||
this.cellStyleStrategy = cellStyleStrategy; |
||||
} |
||||
} |
@ -0,0 +1,43 @@
|
||||
package com.alibaba.excel.metadata.holder; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
|
||||
/** |
||||
* |
||||
* Get the corresponding configuration |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
public interface ConfigurationSelector { |
||||
|
||||
/** |
||||
* What handler does the currently operated cell need to execute |
||||
* |
||||
* @return |
||||
*/ |
||||
List<WriteHandler> writeHandlerList(); |
||||
|
||||
/** |
||||
* What converter does the currently operated cell need to execute |
||||
* |
||||
* @return |
||||
*/ |
||||
Map<Class, Converter> converterMap(); |
||||
|
||||
/** |
||||
* Whether a header is required for the currently operated cell |
||||
* |
||||
* @return |
||||
*/ |
||||
boolean needHead(); |
||||
|
||||
/** |
||||
* Writes the head relative to the existing contents of the sheet. Indexes are zero-based. |
||||
*/ |
||||
int writeRelativeHeadRowIndex(); |
||||
|
||||
} |
@ -0,0 +1,131 @@
|
||||
package com.alibaba.excel.metadata.holder; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.metadata.ExcelHeadProperty; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SheetHolder implements ConfigurationSelector { |
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private Sheet sheet; |
||||
|
||||
/*** |
||||
* has been initialized table |
||||
*/ |
||||
private Map<Integer, TableHolder> hasBeenInitializedTable; |
||||
/** |
||||
* Need Head |
||||
*/ |
||||
private Boolean needHead; |
||||
/** |
||||
* Write handler for workbook |
||||
*/ |
||||
private List<WriteHandler> writeHandlerList; |
||||
/** |
||||
* Converter for workbook |
||||
*/ |
||||
private Map<Class, Converter> converterMap; |
||||
/** |
||||
* current param |
||||
*/ |
||||
private com.alibaba.excel.metadata.Sheet sheetParam; |
||||
|
||||
/** |
||||
* Excel head property |
||||
*/ |
||||
private ExcelHeadProperty excelHeadProperty; |
||||
/** |
||||
* Writes the head relative to the existing contents of the sheet. Indexes are zero-based. |
||||
*/ |
||||
private Integer writeRelativeHeadRowIndex; |
||||
|
||||
public Sheet getSheet() { |
||||
return sheet; |
||||
} |
||||
|
||||
public void setSheet(Sheet sheet) { |
||||
this.sheet = sheet; |
||||
} |
||||
|
||||
public Map<Integer, TableHolder> getHasBeenInitializedTable() { |
||||
return hasBeenInitializedTable; |
||||
} |
||||
|
||||
public void setHasBeenInitializedTable(Map<Integer, TableHolder> hasBeenInitializedTable) { |
||||
this.hasBeenInitializedTable = hasBeenInitializedTable; |
||||
} |
||||
|
||||
public Boolean getNeedHead() { |
||||
return needHead; |
||||
} |
||||
|
||||
public void setNeedHead(Boolean needHead) { |
||||
this.needHead = needHead; |
||||
} |
||||
|
||||
public List<WriteHandler> getWriteHandlerList() { |
||||
return writeHandlerList; |
||||
} |
||||
|
||||
public void setWriteHandlerList(List<WriteHandler> writeHandlerList) { |
||||
this.writeHandlerList = writeHandlerList; |
||||
} |
||||
|
||||
public Map<Class, Converter> getConverterMap() { |
||||
return converterMap; |
||||
} |
||||
|
||||
public void setConverterMap(Map<Class, Converter> converterMap) { |
||||
this.converterMap = converterMap; |
||||
} |
||||
|
||||
public com.alibaba.excel.metadata.Sheet getSheetParam() { |
||||
return sheetParam; |
||||
} |
||||
|
||||
public void setSheetParam(com.alibaba.excel.metadata.Sheet sheetParam) { |
||||
this.sheetParam = sheetParam; |
||||
} |
||||
|
||||
public ExcelHeadProperty getExcelHeadProperty() { |
||||
return excelHeadProperty; |
||||
} |
||||
|
||||
public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) { |
||||
this.excelHeadProperty = excelHeadProperty; |
||||
} |
||||
|
||||
public Integer getWriteRelativeHeadRowIndex() { |
||||
return writeRelativeHeadRowIndex; |
||||
} |
||||
|
||||
public void setWriteRelativeHeadRowIndex(Integer writeRelativeHeadRowIndex) { |
||||
this.writeRelativeHeadRowIndex = writeRelativeHeadRowIndex; |
||||
} |
||||
|
||||
@Override |
||||
public List<WriteHandler> writeHandlerList() { |
||||
return getWriteHandlerList(); |
||||
} |
||||
|
||||
@Override |
||||
public Map<Class, Converter> converterMap() { |
||||
return getConverterMap(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean needHead() { |
||||
return getNeedHead(); |
||||
} |
||||
} |
@ -0,0 +1,92 @@
|
||||
package com.alibaba.excel.metadata.holder; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
|
||||
/** |
||||
* Workbook holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class WorkbookHolder implements ConfigurationSelector { |
||||
/*** |
||||
* poi Workbook |
||||
*/ |
||||
private Workbook workbook; |
||||
/** |
||||
* Need Head |
||||
*/ |
||||
private Boolean needHead; |
||||
/** |
||||
* Write handler for workbook |
||||
*/ |
||||
private List<WriteHandler> writeHandlerList; |
||||
/** |
||||
* Converter for workbook |
||||
*/ |
||||
private Map<Class, Converter> converterMap; |
||||
/** |
||||
* prevent duplicate creation of sheet objects |
||||
*/ |
||||
private Map<Integer, SheetHolder> hasBeenInitializedSheet; |
||||
|
||||
public Workbook getWorkbook() { |
||||
return workbook; |
||||
} |
||||
|
||||
public void setWorkbook(Workbook workbook) { |
||||
this.workbook = workbook; |
||||
} |
||||
|
||||
public Boolean getNeedHead() { |
||||
return needHead; |
||||
} |
||||
|
||||
public void setNeedHead(Boolean needHead) { |
||||
this.needHead = needHead; |
||||
} |
||||
|
||||
public List<WriteHandler> getWriteHandlerList() { |
||||
return writeHandlerList; |
||||
} |
||||
|
||||
public void setWriteHandlerList(List<WriteHandler> writeHandlerList) { |
||||
this.writeHandlerList = writeHandlerList; |
||||
} |
||||
|
||||
public Map<Class, Converter> getConverterMap() { |
||||
return converterMap; |
||||
} |
||||
|
||||
public void setConverterMap(Map<Class, Converter> converterMap) { |
||||
this.converterMap = converterMap; |
||||
} |
||||
|
||||
public Map<Integer, SheetHolder> getHasBeenInitializedSheet() { |
||||
return hasBeenInitializedSheet; |
||||
} |
||||
|
||||
public void setHasBeenInitializedSheet(Map<Integer, SheetHolder> hasBeenInitializedSheet) { |
||||
this.hasBeenInitializedSheet = hasBeenInitializedSheet; |
||||
} |
||||
|
||||
@Override |
||||
public List<WriteHandler> writeHandlerList() { |
||||
return getWriteHandlerList(); |
||||
} |
||||
|
||||
@Override |
||||
public Map<Class, Converter> converterMap() { |
||||
return getConverterMap(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean needHead() { |
||||
return getNeedHead(); |
||||
} |
||||
} |
@ -0,0 +1,29 @@
|
||||
package com.alibaba.excel.util; |
||||
|
||||
/** |
||||
* boolean util |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class BooleanUtils { |
||||
|
||||
private static final String TRUE_NUMBER = "1"; |
||||
private static final String FALSE_NUMBER = "0"; |
||||
|
||||
/** |
||||
* String to boolean |
||||
* |
||||
* <li> |
||||
* |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static Boolean valueOf(String str) { |
||||
if (TRUE_NUMBER.equals(str)) { |
||||
return Boolean.TRUE; |
||||
} else { |
||||
return Boolean.FALSE; |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,101 @@
|
||||
package com.alibaba.excel.util; |
||||
|
||||
import java.text.ParseException; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.exception.ExcelDataConvertException; |
||||
|
||||
/** |
||||
* Date utils |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
public class DateUtils { |
||||
public static final String DATE_FORMAT_14 = "yyyyMMddHHmmss"; |
||||
public static final String DATE_FORMAT_17 = "yyyyMMdd HH:mm:ss"; |
||||
public static final String DATE_FORMAT_19 = "yyyy-MM-dd HH:mm:ss"; |
||||
public static final String DATE_FORMAT_19_FORWARD_SLASH = "yyyy/MM/dd HH:mm:ss"; |
||||
private static final String MINUS = "-"; |
||||
|
||||
private DateUtils() {} |
||||
|
||||
/** |
||||
* convert string to date |
||||
* |
||||
* @param dateString |
||||
* @param dateFormat |
||||
* @return |
||||
* @throws ParseException |
||||
*/ |
||||
public static Date parseDate(String dateString, String dateFormat) throws ParseException { |
||||
if (StringUtils.isEmpty(dateFormat)) { |
||||
dateFormat = switchDateFormat(dateString); |
||||
} |
||||
return new SimpleDateFormat(dateFormat).parse(dateString); |
||||
} |
||||
|
||||
/** |
||||
* convert string to date |
||||
* |
||||
* @param dateString |
||||
* @return |
||||
* @throws ParseException |
||||
*/ |
||||
public static Date parseDate(String dateString) throws ParseException { |
||||
return parseDate(switchDateFormat(dateString), null); |
||||
} |
||||
|
||||
/** |
||||
* switch date format |
||||
* |
||||
* @param dateString |
||||
* @return |
||||
*/ |
||||
private static String switchDateFormat(String dateString) { |
||||
int length = dateString.length(); |
||||
switch (length) { |
||||
case 19: |
||||
if (dateString.contains(MINUS)) { |
||||
return DATE_FORMAT_19; |
||||
} else { |
||||
return DATE_FORMAT_19_FORWARD_SLASH; |
||||
} |
||||
case 17: |
||||
return DATE_FORMAT_17; |
||||
case 14: |
||||
return DATE_FORMAT_14; |
||||
default: |
||||
throw new ExcelDataConvertException("can not find date format for:" + dateString); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Format date |
||||
* <p> |
||||
* yyyy-MM-dd HH:mm:ss |
||||
* |
||||
* @param date |
||||
* @return |
||||
*/ |
||||
public static String format(Date date) { |
||||
return format(date, null); |
||||
} |
||||
|
||||
/** |
||||
* Format date |
||||
* |
||||
* @param date |
||||
* @param dateFormat |
||||
* @return |
||||
*/ |
||||
public static String format(Date date, String dateFormat) { |
||||
if (date == null) { |
||||
return ""; |
||||
} |
||||
if (StringUtils.isEmpty(dateFormat)) { |
||||
dateFormat = DATE_FORMAT_19; |
||||
} |
||||
return new SimpleDateFormat(dateFormat).format(date); |
||||
} |
||||
} |
@ -1,145 +0,0 @@
|
||||
package com.alibaba.excel.util; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.math.BigDecimal; |
||||
import java.math.RoundingMode; |
||||
import java.text.ParseException; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.ArrayList; |
||||
import java.util.Date; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.metadata.ExcelColumnProperty; |
||||
import com.alibaba.excel.metadata.ExcelHeadProperty; |
||||
|
||||
/** |
||||
* @author jipengfei |
||||
*/ |
||||
public class TypeUtil { |
||||
|
||||
private static List<String> DATE_FORMAT_LIST = new ArrayList<String>(4); |
||||
|
||||
static { |
||||
DATE_FORMAT_LIST.add("yyyy/MM/dd HH:mm:ss"); |
||||
DATE_FORMAT_LIST.add("yyyy-MM-dd HH:mm:ss"); |
||||
DATE_FORMAT_LIST.add("yyyyMMdd HH:mm:ss"); |
||||
} |
||||
|
||||
public static Boolean isNum(Field field) { |
||||
if (field == null) { |
||||
return false; |
||||
} |
||||
if (Integer.class.equals(field.getType()) || int.class.equals(field.getType())) { |
||||
return true; |
||||
} |
||||
if (Double.class.equals(field.getType()) || double.class.equals(field.getType())) { |
||||
return true; |
||||
} |
||||
|
||||
if (Long.class.equals(field.getType()) || long.class.equals(field.getType())) { |
||||
return true; |
||||
} |
||||
|
||||
if (BigDecimal.class.equals(field.getType())) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static Boolean isNum(Object cellValue) { |
||||
if (cellValue instanceof Integer || cellValue instanceof Double || cellValue instanceof Short |
||||
|| cellValue instanceof Long || cellValue instanceof Float || cellValue instanceof BigDecimal) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public static String getDefaultDateString(Date date) { |
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
||||
return simpleDateFormat.format(date); |
||||
|
||||
} |
||||
|
||||
public static Date getSimpleDateFormatDate(String value, String format) { |
||||
if (!StringUtils.isEmpty(value)) { |
||||
Date date = null; |
||||
if (!StringUtils.isEmpty(format)) { |
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); |
||||
try { |
||||
date = simpleDateFormat.parse(value); |
||||
return date; |
||||
} catch (ParseException e) { |
||||
} |
||||
} |
||||
for (String dateFormat : DATE_FORMAT_LIST) { |
||||
try { |
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat); |
||||
date = simpleDateFormat.parse(value); |
||||
} catch (ParseException e) { |
||||
} |
||||
if (date != null) { |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return date; |
||||
|
||||
} |
||||
return null; |
||||
|
||||
} |
||||
|
||||
|
||||
public static String formatFloat(String value) { |
||||
if (null != value && value.contains(".")) { |
||||
if (isNumeric(value)) { |
||||
try { |
||||
BigDecimal decimal = new BigDecimal(value); |
||||
BigDecimal setScale = decimal.setScale(10, RoundingMode.HALF_DOWN).stripTrailingZeros(); |
||||
return setScale.toPlainString(); |
||||
} catch (Exception e) { |
||||
} |
||||
} |
||||
} |
||||
return value; |
||||
} |
||||
|
||||
public static String formatFloat0(String value, int n) { |
||||
if (null != value && value.contains(".")) { |
||||
if (isNumeric(value)) { |
||||
try { |
||||
BigDecimal decimal = new BigDecimal(value); |
||||
BigDecimal setScale = decimal.setScale(n, RoundingMode.HALF_DOWN); |
||||
return setScale.toPlainString(); |
||||
} catch (Exception e) { |
||||
} |
||||
} |
||||
} |
||||
return value; |
||||
} |
||||
|
||||
public static final Pattern pattern = Pattern.compile("[\\+\\-]?[\\d]+([\\.][\\d]*)?([Ee][+-]?[\\d]+)?$"); |
||||
|
||||
private static boolean isNumeric(String str) { |
||||
Matcher isNum = pattern.matcher(str); |
||||
if (!isNum.matches()) { |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
public static String formatDate(Date cellValue, String format) { |
||||
SimpleDateFormat simpleDateFormat; |
||||
if (!StringUtils.isEmpty(format)) { |
||||
simpleDateFormat = new SimpleDateFormat(format); |
||||
} else { |
||||
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
||||
} |
||||
return simpleDateFormat.format(cellValue); |
||||
} |
||||
} |
@ -1,30 +0,0 @@
|
||||
package com.alibaba.excel.write; |
||||
|
||||
import java.io.OutputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
|
||||
public class ExcelWriterBuilder { |
||||
/** |
||||
* Excel type |
||||
*/ |
||||
private ExcelTypeEnum excelType; |
||||
|
||||
/** |
||||
* Final output stream |
||||
*/ |
||||
private OutputStream outputStream; |
||||
|
||||
/** |
||||
* |
||||
*/ |
||||
private List<Converter> customConverter = new ArrayList<Converter>(); |
||||
|
||||
public ExcelWriter build() { |
||||
new ExcelBuilderImpl(templateInputStream, outputStream, typeEnum, needHead, writeHandler, converters); |
||||
} |
||||
} |
@ -0,0 +1,120 @@
|
||||
package com.alibaba.excel.write.builder; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.net.URI; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
|
||||
/** |
||||
* Build ExcelBuilder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ExcelWriterBuilder { |
||||
/** |
||||
* Excel type |
||||
*/ |
||||
private ExcelTypeEnum excelType; |
||||
/** |
||||
* Final output stream |
||||
*/ |
||||
private OutputStream outputStream; |
||||
/** |
||||
* Template input stream |
||||
*/ |
||||
private InputStream templateInputStream; |
||||
/** |
||||
* Custom type conversions override the default |
||||
*/ |
||||
private Map<Class, Converter> customConverterMap = new HashMap<Class, Converter>(); |
||||
/** |
||||
* Need Head |
||||
*/ |
||||
private Boolean needHead; |
||||
/** |
||||
* Custom type handler override the default |
||||
*/ |
||||
private List<WriteHandler> customWriteHandlerList = new ArrayList<WriteHandler>(); |
||||
|
||||
public ExcelWriterBuilder excelType(ExcelTypeEnum excelType) { |
||||
this.excelType = excelType; |
||||
return this; |
||||
} |
||||
|
||||
public ExcelWriterBuilder outputFile(OutputStream outputStream) { |
||||
this.outputStream = outputStream; |
||||
return this; |
||||
} |
||||
|
||||
public ExcelWriterBuilder outputFile(File outputFile) throws FileNotFoundException { |
||||
return outputFile(new FileOutputStream(outputFile)); |
||||
} |
||||
|
||||
public ExcelWriterBuilder outputFile(String outputPathName) throws FileNotFoundException { |
||||
return outputFile(new File(outputPathName)); |
||||
} |
||||
|
||||
public ExcelWriterBuilder outputFile(URI outputUri) throws FileNotFoundException { |
||||
return outputFile(new File(outputUri)); |
||||
} |
||||
|
||||
public ExcelWriterBuilder withTemplate(InputStream templateInputStream) { |
||||
this.templateInputStream = templateInputStream; |
||||
return this; |
||||
} |
||||
|
||||
public ExcelWriterBuilder withTemplate(File templateFile) throws FileNotFoundException { |
||||
return withTemplate(new FileInputStream(templateFile)); |
||||
} |
||||
|
||||
public ExcelWriterBuilder withTemplate(String templatePathName) throws FileNotFoundException { |
||||
return withTemplate(new File(templatePathName)); |
||||
} |
||||
|
||||
public ExcelWriterBuilder withTemplate(URI templateUri) throws FileNotFoundException { |
||||
return withTemplate(new File(templateUri)); |
||||
} |
||||
|
||||
/** |
||||
* Custom type conversions override the default. |
||||
* |
||||
* @param converter |
||||
* @return |
||||
*/ |
||||
public ExcelWriterBuilder registerConverter(Converter converter) { |
||||
this.customConverterMap.put(converter.supportJavaTypeKey(), converter); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Default required header |
||||
* |
||||
* @return |
||||
*/ |
||||
public ExcelWriterBuilder doNotNeedHead() { |
||||
this.needHead = Boolean.FALSE; |
||||
return this; |
||||
} |
||||
|
||||
public ExcelWriterBuilder registerWriteHandler(WriteHandler excelWriteHandler) { |
||||
this.customWriteHandlerList.add(excelWriteHandler); |
||||
return this; |
||||
} |
||||
|
||||
public ExcelWriter build() { |
||||
return new ExcelWriter(templateInputStream, outputStream, excelType, needHead, customConverterMap, |
||||
customWriteHandlerList); |
||||
} |
||||
} |
@ -0,0 +1,25 @@
|
||||
package com.alibaba.excel.write.handler; |
||||
|
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
|
||||
/** |
||||
* intercepts handle Workbook creation |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public interface WorkbookWriteHandler extends WriteHandler { |
||||
|
||||
/** |
||||
* called before create the sheet |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void beforeWorkbookCreate(); |
||||
|
||||
/** |
||||
* called after the sheet is created |
||||
* |
||||
* @param writeContext |
||||
*/ |
||||
void afterWorkbookCreate(Workbook workbook); |
||||
} |
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<configuration> |
||||
<property name="LOG_PATTERN" |
||||
value="%d{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread] %class:%line - %msg%n"/> |
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> |
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> |
||||
<pattern>${LOG_PATTERN}</pattern> |
||||
</encoder> |
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter"> |
||||
<level>INFO</level> |
||||
</filter> |
||||
</appender> |
||||
<root level="INFO"> |
||||
<appender-ref ref="STDOUT"/> |
||||
</root> |
||||
</configuration> |
Loading…
Reference in new issue