diff --git a/continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter/ExcelListConverter.java b/continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter/ExcelListConverter.java new file mode 100644 index 00000000..e1b9ab80 --- /dev/null +++ b/continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter/ExcelListConverter.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.file.excel.converter; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +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.springframework.stereotype.Component; +import top.continew.starter.core.constant.StringConstants; + +import java.util.List; + +/** + * Easy Excel List 集合转换器 + * + *

+ * 仅适合 List<基本类型> <=> xxx,xxx 转换 + *

+ * + * @author Charles7c + * @since 2.0.2 + */ +@Component +public class ExcelListConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return List.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public List convertToJavaData(ReadCellData cellData, + ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) { + String stringValue = cellData.getStringValue(); + return StrUtil.split(stringValue, StringConstants.COMMA); + } + + @Override + public WriteCellData convertToExcelData(List value, + ExcelContentProperty contentProperty, + GlobalConfiguration globalConfiguration) { + WriteCellData writeCellData = new WriteCellData<>(CollUtil.join(value, StringConstants.COMMA)); + writeCellData.setType(CellDataTypeEnum.STRING); + return writeCellData; + } +}