diff --git a/README.md b/README.md index c1e263e..03bb015 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目 | 版本 | 支持 | |-------|--| -| 3.2.0 | 适配 SpringBoot3.x | +| 3.2.1 | 适配 SpringBoot3.x | | 1.2.7 | 适配 SpringBoot2.x | ```xml diff --git a/pom.xml b/pom.xml index 863dd49..ad1fab5 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.pig4cloud.excel excel-spring-boot-starter - 3.2.0 + 3.2.1 excel-spring-boot-starter easy and high performance excel https://pig4cloud.com diff --git a/src/main/java/com/pig4cloud/plugin/excel/aop/RequestExcelArgumentResolver.java b/src/main/java/com/pig4cloud/plugin/excel/aop/RequestExcelArgumentResolver.java index 4693d45..7b799ff 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/aop/RequestExcelArgumentResolver.java +++ b/src/main/java/com/pig4cloud/plugin/excel/aop/RequestExcelArgumentResolver.java @@ -2,8 +2,7 @@ import com.alibaba.excel.EasyExcel; import com.pig4cloud.plugin.excel.annotation.RequestExcel; -import com.pig4cloud.plugin.excel.converters.LocalDateStringConverter; -import com.pig4cloud.plugin.excel.converters.LocalDateTimeStringConverter; +import com.pig4cloud.plugin.excel.converters.*; import com.pig4cloud.plugin.excel.handler.ListAnalysisEventListener; import jakarta.servlet.http.HttpServletRequest; import lombok.SneakyThrows; @@ -74,6 +73,9 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m EasyExcel.read(inputStream, excelModelClass, readListener) .registerConverter(LocalDateStringConverter.INSTANCE) .registerConverter(LocalDateTimeStringConverter.INSTANCE) + .registerConverter(LocalTimeStringConverter.INSTANCE) + .registerConverter(LongStringConverter.INSTANCE) + .registerConverter(StringArrayConverter.INSTANCE) .ignoreEmptyRow(requestExcel.ignoreEmptyRow()) .sheet() .headRowNumber(requestExcel.headRowNumber()) diff --git a/src/main/java/com/pig4cloud/plugin/excel/converters/LocalTimeStringConverter.java b/src/main/java/com/pig4cloud/plugin/excel/converters/LocalTimeStringConverter.java new file mode 100644 index 0000000..5f1f8b2 --- /dev/null +++ b/src/main/java/com/pig4cloud/plugin/excel/converters/LocalTimeStringConverter.java @@ -0,0 +1,62 @@ +package com.pig4cloud.plugin.excel.converters; + +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 java.text.ParseException; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + +/** + * LocalDate and string converter + * + * @author L.cm + */ +public enum LocalTimeStringConverter implements Converter { + + /** + * 实例 + */ + INSTANCE; + + @Override + public Class supportJavaTypeKey() { + return LocalTime.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public LocalTime convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) throws ParseException { + if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { + return LocalTime.parse(cellData.getStringValue()); + } + else { + DateTimeFormatter formatter = DateTimeFormatter + .ofPattern(contentProperty.getDateTimeFormatProperty().getFormat()); + return LocalTime.parse(cellData.getStringValue(), formatter); + } + } + + @Override + public WriteCellData convertToExcelData(LocalTime value, ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) { + DateTimeFormatter formatter; + if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { + formatter = DateTimeFormatter.ISO_LOCAL_TIME; + } + else { + formatter = DateTimeFormatter.ofPattern(contentProperty.getDateTimeFormatProperty().getFormat()); + } + return new WriteCellData<>(value.format(formatter)); + } + +} diff --git a/src/main/java/com/pig4cloud/plugin/excel/converters/LongStringConverter.java b/src/main/java/com/pig4cloud/plugin/excel/converters/LongStringConverter.java new file mode 100644 index 0000000..82e625c --- /dev/null +++ b/src/main/java/com/pig4cloud/plugin/excel/converters/LongStringConverter.java @@ -0,0 +1,46 @@ +package com.pig4cloud.plugin.excel.converters; + +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 java.text.ParseException; + +/** + * Long and string converter + * + * @author L.cm + */ +public enum LongStringConverter implements Converter { + + /** + * 实例 + */ + INSTANCE; + + @Override + public Class supportJavaTypeKey() { + return Long.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public Long convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) throws ParseException { + return Long.parseLong(cellData.getStringValue()); + } + + @Override + public WriteCellData convertToExcelData(Long value, ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) { + return new WriteCellData<>(String.valueOf(value)); + } + +} diff --git a/src/main/java/com/pig4cloud/plugin/excel/converters/StringArrayConverter.java b/src/main/java/com/pig4cloud/plugin/excel/converters/StringArrayConverter.java new file mode 100644 index 0000000..df6589e --- /dev/null +++ b/src/main/java/com/pig4cloud/plugin/excel/converters/StringArrayConverter.java @@ -0,0 +1,48 @@ +package com.pig4cloud.plugin.excel.converters; + +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 java.text.ParseException; +import java.util.Arrays; +import java.util.stream.Collectors; + +/** + * LocalDate and string converter + * + * @author L.cm + */ +public enum StringArrayConverter implements Converter { + + /** + * 实例 + */ + INSTANCE; + + @Override + public Class supportJavaTypeKey() { + return String[].class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public String[] convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) throws ParseException { + return cellData.getStringValue().split(","); + } + + @Override + public WriteCellData convertToExcelData(String[] value, ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) { + return new WriteCellData<>(Arrays.stream(value).collect(Collectors.joining())); + } + +} diff --git a/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java b/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java index 9124dbb..d6adf0d 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java +++ b/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java @@ -11,8 +11,7 @@ import com.pig4cloud.plugin.excel.annotation.Sheet; import com.pig4cloud.plugin.excel.aop.DynamicNameAspect; import com.pig4cloud.plugin.excel.config.ExcelConfigProperties; -import com.pig4cloud.plugin.excel.converters.LocalDateStringConverter; -import com.pig4cloud.plugin.excel.converters.LocalDateTimeStringConverter; +import com.pig4cloud.plugin.excel.converters.*; import com.pig4cloud.plugin.excel.enhance.WriterBuilderEnhancer; import com.pig4cloud.plugin.excel.head.HeadGenerator; import com.pig4cloud.plugin.excel.head.HeadMeta; @@ -110,6 +109,9 @@ public ExcelWriter getExcelWriter(HttpServletResponse response, ResponseExcel re ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream()) .registerConverter(LocalDateStringConverter.INSTANCE) .registerConverter(LocalDateTimeStringConverter.INSTANCE) + .registerConverter(LocalTimeStringConverter.INSTANCE) + .registerConverter(LongStringConverter.INSTANCE) + .registerConverter(StringArrayConverter.INSTANCE) .autoCloseStream(true) .excelType(responseExcel.suffix()) .inMemory(responseExcel.inMemory());