| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package cn.iocoder.yudao.framework.excel.core.convert; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.idev.excel.converters.Converter; |
| | | import cn.idev.excel.enums.CellDataTypeEnum; |
| | | import cn.idev.excel.metadata.GlobalConfiguration; |
| | | import cn.idev.excel.metadata.data.ReadCellData; |
| | | import cn.idev.excel.metadata.data.WriteCellData; |
| | | import cn.idev.excel.metadata.property.ExcelContentProperty; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | |
| | | /** |
| | | * LocalTime 转æ¢å¨ |
| | | * |
| | | * FastExcelï¼cn.idev.excelï¼å
ç½®ä¸æ¯æ LocalTimeï¼æªæå® converter æ¶è¯»åé½ä¼æåº |
| | | * `Can not find 'Converter' support class LocalTime`ãè¿éè¡¥ä¸ãHH:mm:ss å符串ãä¸ |
| | | * ãExcel æ¶é´æ°å¼ï¼ä¸å¤©çå æ¯ï¼ã两ç§åæ³çè¯»åæ¯æã |
| | | * |
| | | * @author è¶
级管çå |
| | | */ |
| | | public class LocalTimeConvert implements Converter<LocalTime> { |
| | | |
| | | private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); |
| | | |
| | | @Override |
| | | public Class<LocalTime> supportJavaTypeKey() { |
| | | return LocalTime.class; |
| | | } |
| | | |
| | | @Override |
| | | public CellDataTypeEnum supportExcelTypeKey() { |
| | | return CellDataTypeEnum.STRING; |
| | | } |
| | | |
| | | @Override |
| | | public LocalTime convertToJavaData(ReadCellData readCellData, ExcelContentProperty contentProperty, |
| | | GlobalConfiguration globalConfiguration) { |
| | | // æ
åµä¸ï¼Excel éå¡«çæ¯æ¶é´æ°å¼ï¼ä¸å¤©çå æ¯ï¼ |
| | | if (CellDataTypeEnum.NUMBER.equals(readCellData.getType())) { |
| | | BigDecimal numberValue = readCellData.getNumberValue(); |
| | | if (numberValue == null) { |
| | | return null; |
| | | } |
| | | long secondOfDay = Math.round(numberValue.doubleValue() * 24 * 60 * 60) % (24 * 60 * 60); |
| | | return LocalTime.ofSecondOfDay(secondOfDay); |
| | | } |
| | | // æ
åµäºï¼Excel éå¡«çæ¯ææ¬ï¼å¦ 8:30 / 08:30:00 |
| | | String text = readCellData.getStringValue(); |
| | | if (StrUtil.isBlank(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | String[] parts = text.trim().replace('ï¼', ':').split(":"); |
| | | int hour = Integer.parseInt(parts[0].trim()); |
| | | int minute = parts.length > 1 ? Integer.parseInt(parts[1].trim()) : 0; |
| | | int second = parts.length > 2 ? Integer.parseInt(parts[2].trim()) : 0; |
| | | return LocalTime.of(hour, minute, second); |
| | | } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public WriteCellData<String> convertToExcelData(LocalTime value, ExcelContentProperty contentProperty, |
| | | GlobalConfiguration globalConfiguration) { |
| | | return new WriteCellData<>(value == null ? "" : value.format(TIME_FORMATTER)); |
| | | } |
| | | |
| | | } |