mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
3 years ago
committed by
GitHub
318 changed files with 10182 additions and 6001 deletions
@ -0,0 +1,2 @@
|
||||
lombok.toString.callSuper = CALL |
||||
lombok.equalsAndHashCode.callSuper= CALL |
@ -0,0 +1,122 @@
|
||||
package com.alibaba.excel.analysis.csv; |
||||
|
||||
import java.io.FileReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.util.ArrayList; |
||||
import java.util.Iterator; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.analysis.ExcelReadExecutor; |
||||
import com.alibaba.excel.context.csv.CsvReadContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.enums.RowTypeEnum; |
||||
import com.alibaba.excel.exception.ExcelAnalysisException; |
||||
import com.alibaba.excel.metadata.Cell; |
||||
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
import com.alibaba.excel.read.metadata.ReadSheet; |
||||
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; |
||||
import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder; |
||||
import com.alibaba.excel.util.SheetUtils; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.collections4.MapUtils; |
||||
import org.apache.commons.csv.CSVFormat; |
||||
import org.apache.commons.csv.CSVRecord; |
||||
|
||||
/** |
||||
* read executor |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Slf4j |
||||
public class CsvExcelReadExecutor implements ExcelReadExecutor { |
||||
|
||||
private List<ReadSheet> sheetList; |
||||
private CsvReadContext csvReadContext; |
||||
|
||||
public CsvExcelReadExecutor(CsvReadContext csvReadContext) { |
||||
this.csvReadContext = csvReadContext; |
||||
sheetList = new ArrayList<>(); |
||||
ReadSheet readSheet = new ReadSheet(); |
||||
sheetList.add(readSheet); |
||||
readSheet.setSheetNo(0); |
||||
} |
||||
|
||||
@Override |
||||
public List<ReadSheet> sheetList() { |
||||
return sheetList; |
||||
} |
||||
|
||||
@Override |
||||
public void execute() { |
||||
Iterable<CSVRecord> parseRecords; |
||||
try { |
||||
parseRecords = parseRecords(); |
||||
} catch (IOException e) { |
||||
throw new ExcelAnalysisException(e); |
||||
} |
||||
for (ReadSheet readSheet : sheetList) { |
||||
readSheet = SheetUtils.match(readSheet, csvReadContext); |
||||
if (readSheet == null) { |
||||
continue; |
||||
} |
||||
csvReadContext.currentSheet(readSheet); |
||||
|
||||
int rowIndex = 0; |
||||
|
||||
for (CSVRecord record : parseRecords) { |
||||
dealRecord(record, rowIndex++); |
||||
} |
||||
|
||||
// The last sheet is read
|
||||
csvReadContext.analysisEventProcessor().endSheet(csvReadContext); |
||||
} |
||||
} |
||||
|
||||
private Iterable<CSVRecord> parseRecords() throws IOException { |
||||
CsvReadWorkbookHolder csvReadWorkbookHolder = csvReadContext.csvReadWorkbookHolder(); |
||||
CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat(); |
||||
|
||||
if (csvReadWorkbookHolder.getMandatoryUseInputStream()) { |
||||
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream())); |
||||
} |
||||
if (csvReadWorkbookHolder.getFile() != null) { |
||||
return csvFormat.parse(new FileReader(csvReadWorkbookHolder.getFile())); |
||||
} |
||||
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream())); |
||||
} |
||||
|
||||
private void dealRecord(CSVRecord record, int rowIndex) { |
||||
Map<Integer, Cell> cellMap = new LinkedHashMap<>(); |
||||
Iterator<String> cellIterator = record.iterator(); |
||||
int columnIndex = 0; |
||||
while (cellIterator.hasNext()) { |
||||
String cellString = cellIterator.next(); |
||||
ReadCellData<String> readCellData = new ReadCellData<>(); |
||||
readCellData.setRowIndex(rowIndex); |
||||
readCellData.setColumnIndex(columnIndex); |
||||
|
||||
// csv is an empty string of whether <code>,,</code> is read or <code>,"",</code>
|
||||
if (StringUtils.isNotBlank(cellString)) { |
||||
readCellData.setType(CellDataTypeEnum.STRING); |
||||
readCellData.setStringValue(cellString); |
||||
} else { |
||||
readCellData.setType(CellDataTypeEnum.EMPTY); |
||||
} |
||||
cellMap.put(columnIndex++, readCellData); |
||||
} |
||||
|
||||
RowTypeEnum rowType = MapUtils.isEmpty(cellMap) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA; |
||||
ReadRowHolder readRowHolder = new ReadRowHolder(rowIndex, rowType, |
||||
csvReadContext.readWorkbookHolder().getGlobalConfiguration(), cellMap); |
||||
csvReadContext.readRowHolder(readRowHolder); |
||||
|
||||
csvReadContext.csvReadSheetHolder().setCellMap(cellMap); |
||||
csvReadContext.csvReadSheetHolder().setRowIndex(rowIndex); |
||||
csvReadContext.analysisEventProcessor().endRow(csvReadContext); |
||||
} |
||||
} |
@ -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; |
||||
} |
@ -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(); |
||||
} |
@ -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(); |
||||
} |
||||
} |
@ -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<Object> { |
||||
} |
||||
|
@ -0,0 +1,10 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
/** |
||||
* When implementing <code>convertToExcelData</code> method, pay attention to the reference <code>value</code> may be |
||||
* null |
||||
* |
||||
* @author JiaJu Zhuang |
||||
**/ |
||||
public interface NullableObjectConverter<T> extends Converter<T> { |
||||
} |
@ -0,0 +1,30 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* read converter context |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class ReadConverterContext<T> { |
||||
/** |
||||
* Excel cell data.NotNull. |
||||
*/ |
||||
private ReadCellData<T> readCellData; |
||||
/** |
||||
* Content property.Nullable. |
||||
*/ |
||||
private ExcelContentProperty contentProperty; |
||||
/** |
||||
* context.NotNull. |
||||
*/ |
||||
private AnalysisContext analysisContext; |
||||
} |
@ -0,0 +1,32 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import com.alibaba.excel.context.WriteContext; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* write converter context |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class WriteConverterContext<T> { |
||||
|
||||
/** |
||||
* Java Data.NotNull. |
||||
*/ |
||||
private T value; |
||||
|
||||
/** |
||||
* Content property.Nullable. |
||||
*/ |
||||
private ExcelContentProperty contentProperty; |
||||
|
||||
/** |
||||
* write context |
||||
*/ |
||||
private WriteContext writeContext; |
||||
} |
@ -0,0 +1,47 @@
|
||||
package com.alibaba.excel.converters.biginteger; |
||||
|
||||
import java.math.BigInteger; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
||||
/** |
||||
* BigInteger and boolean converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class BigIntegerBooleanConverter implements Converter<BigInteger> { |
||||
|
||||
@Override |
||||
public Class<BigInteger> supportJavaTypeKey() { |
||||
return BigInteger.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.BOOLEAN; |
||||
} |
||||
|
||||
@Override |
||||
public BigInteger convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
if (cellData.getBooleanValue()) { |
||||
return BigInteger.ONE; |
||||
} |
||||
return BigInteger.ZERO; |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(BigInteger value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
if (BigInteger.ONE.equals(value)) { |
||||
return new WriteCellData<>(Boolean.TRUE); |
||||
} |
||||
return new WriteCellData<>(Boolean.FALSE); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.excel.converters.biginteger; |
||||
|
||||
import java.math.BigInteger; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.NumberUtils; |
||||
|
||||
/** |
||||
* BigInteger and number converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class BigIntegerNumberConverter implements Converter<BigInteger> { |
||||
|
||||
@Override |
||||
public Class<BigInteger> supportJavaTypeKey() { |
||||
return BigInteger.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.NUMBER; |
||||
} |
||||
|
||||
@Override |
||||
public BigInteger convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
return cellData.getNumberValue().toBigInteger(); |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(BigInteger value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
return NumberUtils.formatToCellData(value, contentProperty); |
||||
} |
||||
} |
@ -0,0 +1,42 @@
|
||||
package com.alibaba.excel.converters.biginteger; |
||||
|
||||
import java.math.BigInteger; |
||||
import java.text.ParseException; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.NumberUtils; |
||||
|
||||
/** |
||||
* BigDecimal and string converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class BigIntegerStringConverter implements Converter<BigInteger> { |
||||
|
||||
@Override |
||||
public Class<BigInteger> supportJavaTypeKey() { |
||||
return BigInteger.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.STRING; |
||||
} |
||||
|
||||
@Override |
||||
public BigInteger convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) throws ParseException { |
||||
return NumberUtils.parseBigDecimal(cellData.getStringValue(), contentProperty).toBigInteger(); |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(BigInteger value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
return NumberUtils.formatToCellData(value, contentProperty); |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.alibaba.excel.converters.date; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.DateUtils; |
||||
import com.alibaba.excel.util.WorkBookUtil; |
||||
|
||||
/** |
||||
* Date and date converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class DateDateConverter implements Converter<Date> { |
||||
@Override |
||||
public Class<Date> supportJavaTypeKey() { |
||||
return Date.class; |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(Date value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) throws Exception { |
||||
WriteCellData<?> cellData = new WriteCellData<>(value); |
||||
String format = null; |
||||
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) { |
||||
format = contentProperty.getDateTimeFormatProperty().getFormat(); |
||||
} |
||||
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat); |
||||
return cellData; |
||||
} |
||||
} |
@ -0,0 +1,55 @@
|
||||
package com.alibaba.excel.converters.localdatetime; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.time.LocalDateTime; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
||||
import org.apache.poi.ss.usermodel.DateUtil; |
||||
|
||||
/** |
||||
* LocalDateTime and number converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class LocalDateNumberConverter implements Converter<LocalDateTime> { |
||||
|
||||
@Override |
||||
public Class<?> supportJavaTypeKey() { |
||||
return LocalDateTime.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.NUMBER; |
||||
} |
||||
|
||||
@Override |
||||
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { |
||||
return DateUtil.getLocalDateTime(cellData.getNumberValue().doubleValue(), |
||||
globalConfiguration.getUse1904windowing()); |
||||
} else { |
||||
return DateUtil.getLocalDateTime(cellData.getNumberValue().doubleValue(), |
||||
contentProperty.getDateTimeFormatProperty().getUse1904windowing()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { |
||||
return new WriteCellData<>( |
||||
BigDecimal.valueOf(DateUtil.getExcelDate(value, globalConfiguration.getUse1904windowing()))); |
||||
} else { |
||||
return new WriteCellData<>(BigDecimal.valueOf( |
||||
DateUtil.getExcelDate(value, contentProperty.getDateTimeFormatProperty().getUse1904windowing()))); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.alibaba.excel.converters.localdatetime; |
||||
|
||||
import java.time.LocalDateTime; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.DateUtils; |
||||
import com.alibaba.excel.util.WorkBookUtil; |
||||
|
||||
/** |
||||
* Date and date converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class LocalDateTimeDateConverter implements Converter<LocalDateTime> { |
||||
@Override |
||||
public Class<LocalDateTime> supportJavaTypeKey() { |
||||
return LocalDateTime.class; |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) throws Exception { |
||||
WriteCellData<?> cellData = new WriteCellData<>(value); |
||||
String format = null; |
||||
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) { |
||||
format = contentProperty.getDateTimeFormatProperty().getFormat(); |
||||
} |
||||
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat); |
||||
return cellData; |
||||
} |
||||
} |
@ -0,0 +1,52 @@
|
||||
package com.alibaba.excel.converters.localdatetime; |
||||
|
||||
import java.text.ParseException; |
||||
import java.time.LocalDateTime; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.data.ReadCellData; |
||||
import com.alibaba.excel.metadata.data.WriteCellData; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.DateUtils; |
||||
|
||||
/** |
||||
* LocalDateTime and string converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class LocalDateTimeStringConverter implements Converter<LocalDateTime> { |
||||
@Override |
||||
public Class<?> supportJavaTypeKey() { |
||||
return LocalDateTime.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.STRING; |
||||
} |
||||
|
||||
@Override |
||||
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) throws ParseException { |
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { |
||||
return DateUtils.parseLocalDateTime(cellData.getStringValue(), null, globalConfiguration.getLocale()); |
||||
} else { |
||||
return DateUtils.parseLocalDateTime(cellData.getStringValue(), |
||||
contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { |
||||
return new WriteCellData<>(DateUtils.format(value, null, globalConfiguration.getLocale())); |
||||
} else { |
||||
return new WriteCellData<>( |
||||
DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat(), |
||||
globalConfiguration.getLocale())); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.alibaba.excel.enums; |
||||
|
||||
import org.apache.poi.ss.usermodel.CellType; |
||||
|
||||
/** |
||||
* Used to supplement {@link CellType}. |
||||
* |
||||
* Cannot distinguish between date and number in write case. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public enum NumericCellTypeEnum { |
||||
/** |
||||
* number |
||||
*/ |
||||
NUMBER, |
||||
/** |
||||
* date. Support only when writing. |
||||
*/ |
||||
DATE, |
||||
; |
||||
} |
@ -1,8 +1,21 @@
|
||||
package com.alibaba.excel.event; |
||||
|
||||
import com.alibaba.excel.constant.OrderConstant; |
||||
|
||||
/** |
||||
* Intercepts handle some business logic |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public interface Handler {} |
||||
public interface Handler extends Order { |
||||
|
||||
/** |
||||
* handler order |
||||
* |
||||
* @return order |
||||
*/ |
||||
@Override |
||||
default int order() { |
||||
return OrderConstant.DEFAULT_ORDER; |
||||
} |
||||
} |
||||
|
@ -1,38 +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; |
||||
|
||||
/** |
||||
* |
||||
* @author jipengfei |
||||
* @deprecated please use {@link com.alibaba.excel.write.handler.WriteHandler} |
||||
*/ |
||||
@Deprecated |
||||
public interface WriteHandler { |
||||
|
||||
/** |
||||
* 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); |
||||
} |
@ -1,40 +0,0 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.usermodel.CellStyle; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnore; |
||||
|
||||
/** |
||||
* Excel基础模型 |
||||
* |
||||
* @author jipengfei |
||||
* @deprecated Now you don't need to extend any classes |
||||
*/ |
||||
@Deprecated |
||||
public class BaseRowModel { |
||||
|
||||
/** |
||||
* 每列样式 |
||||
*/ |
||||
@ExcelIgnore |
||||
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; |
||||
} |
||||
} |
@ -1,286 +0,0 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
/** |
||||
* Excel internal cell data. |
||||
* |
||||
* <p> |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class CellData<T> extends AbstractCell { |
||||
private CellDataTypeEnum type; |
||||
/** |
||||
* {@link CellDataTypeEnum#NUMBER} |
||||
*/ |
||||
private BigDecimal numberValue; |
||||
/** |
||||
* {@link CellDataTypeEnum#STRING} and{@link CellDataTypeEnum#ERROR} |
||||
*/ |
||||
private String stringValue; |
||||
/** |
||||
* {@link CellDataTypeEnum#BOOLEAN} |
||||
*/ |
||||
private Boolean booleanValue; |
||||
private Boolean formula; |
||||
private String formulaValue; |
||||
private byte[] imageValue; |
||||
/** |
||||
* The number formatting.Currently only supported when reading |
||||
*/ |
||||
private Integer dataFormat; |
||||
/** |
||||
* The string of number formatting.Currently only supported when reading |
||||
*/ |
||||
private String dataFormatString; |
||||
/** |
||||
* The resulting converted data. |
||||
*/ |
||||
private T data; |
||||
|
||||
public CellData(CellData<T> other) { |
||||
this.type = other.type; |
||||
this.numberValue = other.numberValue; |
||||
this.stringValue = other.stringValue; |
||||
this.booleanValue = other.booleanValue; |
||||
this.formula = other.formula; |
||||
this.formulaValue = other.formulaValue; |
||||
this.imageValue = other.imageValue; |
||||
this.dataFormat = other.dataFormat; |
||||
this.dataFormatString = other.dataFormatString; |
||||
this.data = other.data; |
||||
} |
||||
|
||||
public CellData() {} |
||||
|
||||
public CellData(T data) { |
||||
this.data = data; |
||||
} |
||||
|
||||
public CellData(T data, String formulaValue) { |
||||
this.data = data; |
||||
this.formula = Boolean.TRUE; |
||||
this.formulaValue = formulaValue; |
||||
} |
||||
|
||||
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.formula = Boolean.FALSE; |
||||
} |
||||
|
||||
public CellData(BigDecimal numberValue) { |
||||
if (numberValue == null) { |
||||
throw new IllegalArgumentException("DoubleValue can not be null"); |
||||
} |
||||
this.type = CellDataTypeEnum.NUMBER; |
||||
this.numberValue = numberValue; |
||||
this.formula = Boolean.FALSE; |
||||
} |
||||
|
||||
public CellData(byte[] imageValue) { |
||||
if (imageValue == null) { |
||||
throw new IllegalArgumentException("ImageValue can not be null"); |
||||
} |
||||
this.type = CellDataTypeEnum.IMAGE; |
||||
this.imageValue = imageValue; |
||||
this.formula = 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.formula = Boolean.FALSE; |
||||
} |
||||
|
||||
public CellData(CellDataTypeEnum type) { |
||||
if (type == null) { |
||||
throw new IllegalArgumentException("Type can not be null"); |
||||
} |
||||
this.type = type; |
||||
this.formula = Boolean.FALSE; |
||||
} |
||||
|
||||
public CellDataTypeEnum getType() { |
||||
return type; |
||||
} |
||||
|
||||
public void setType(CellDataTypeEnum type) { |
||||
this.type = type; |
||||
} |
||||
|
||||
public BigDecimal getNumberValue() { |
||||
return numberValue; |
||||
} |
||||
|
||||
public void setNumberValue(BigDecimal numberValue) { |
||||
this.numberValue = numberValue; |
||||
} |
||||
|
||||
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 getFormula() { |
||||
return formula; |
||||
} |
||||
|
||||
public void setFormula(Boolean formula) { |
||||
this.formula = formula; |
||||
} |
||||
|
||||
public String getFormulaValue() { |
||||
return formulaValue; |
||||
} |
||||
|
||||
public void setFormulaValue(String formulaValue) { |
||||
this.formulaValue = formulaValue; |
||||
} |
||||
|
||||
public byte[] getImageValue() { |
||||
return imageValue; |
||||
} |
||||
|
||||
public void setImageValue(byte[] imageValue) { |
||||
this.imageValue = imageValue; |
||||
} |
||||
|
||||
public Integer getDataFormat() { |
||||
return dataFormat; |
||||
} |
||||
|
||||
public void setDataFormat(Integer dataFormat) { |
||||
this.dataFormat = dataFormat; |
||||
} |
||||
|
||||
public String getDataFormatString() { |
||||
return dataFormatString; |
||||
} |
||||
|
||||
public void setDataFormatString(String dataFormatString) { |
||||
this.dataFormatString = dataFormatString; |
||||
} |
||||
|
||||
public T getData() { |
||||
return data; |
||||
} |
||||
|
||||
public void setData(T data) { |
||||
this.data = data; |
||||
} |
||||
|
||||
/** |
||||
* Ensure that the object does not appear null |
||||
*/ |
||||
public void checkEmpty() { |
||||
if (type == null) { |
||||
type = CellDataTypeEnum.EMPTY; |
||||
} |
||||
switch (type) { |
||||
case STRING: |
||||
case ERROR: |
||||
if (StringUtils.isEmpty(stringValue)) { |
||||
type = CellDataTypeEnum.EMPTY; |
||||
} |
||||
return; |
||||
case NUMBER: |
||||
if (numberValue == null) { |
||||
type = CellDataTypeEnum.EMPTY; |
||||
} |
||||
return; |
||||
case BOOLEAN: |
||||
if (booleanValue == null) { |
||||
type = CellDataTypeEnum.EMPTY; |
||||
} |
||||
return; |
||||
default: |
||||
} |
||||
} |
||||
|
||||
public static CellData newEmptyInstance() { |
||||
return newEmptyInstance(null, null); |
||||
} |
||||
|
||||
public static CellData newEmptyInstance(Integer rowIndex, Integer columnIndex) { |
||||
CellData cellData = new CellData(CellDataTypeEnum.EMPTY); |
||||
cellData.setRowIndex(rowIndex); |
||||
cellData.setColumnIndex(columnIndex); |
||||
return cellData; |
||||
} |
||||
|
||||
public static CellData newInstance(Boolean booleanValue) { |
||||
return newInstance(booleanValue, null, null); |
||||
} |
||||
|
||||
public static CellData newInstance(Boolean booleanValue, Integer rowIndex, Integer columnIndex) { |
||||
CellData cellData = new CellData(booleanValue); |
||||
cellData.setRowIndex(rowIndex); |
||||
cellData.setColumnIndex(columnIndex); |
||||
return cellData; |
||||
} |
||||
|
||||
public static CellData newInstance(String stringValue, Integer rowIndex, Integer columnIndex) { |
||||
CellData cellData = new CellData(stringValue); |
||||
cellData.setRowIndex(rowIndex); |
||||
cellData.setColumnIndex(columnIndex); |
||||
return cellData; |
||||
} |
||||
|
||||
public static CellData newInstance(BigDecimal numberValue, Integer rowIndex, Integer columnIndex) { |
||||
CellData cellData = new CellData(numberValue); |
||||
cellData.setRowIndex(rowIndex); |
||||
cellData.setColumnIndex(columnIndex); |
||||
return cellData; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
if (type == null) { |
||||
return StringUtils.EMPTY; |
||||
} |
||||
switch (type) { |
||||
case NUMBER: |
||||
return numberValue.toString(); |
||||
case BOOLEAN: |
||||
return booleanValue.toString(); |
||||
case DIRECT_STRING: |
||||
case STRING: |
||||
case ERROR: |
||||
return stringValue; |
||||
case IMAGE: |
||||
return "image[" + imageValue.length + "]"; |
||||
default: |
||||
return StringUtils.EMPTY; |
||||
} |
||||
} |
||||
|
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue