Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
import org.apache.fesod.sheet.converters.floatconverter.FloatNumberConverter;
import org.apache.fesod.sheet.converters.floatconverter.FloatStringConverter;
import org.apache.fesod.sheet.converters.inputstream.InputStreamImageConverter;
import org.apache.fesod.sheet.converters.instant.InstantDateConverter;
import org.apache.fesod.sheet.converters.instant.InstantNumberConverter;
import org.apache.fesod.sheet.converters.instant.InstantStringConverter;
import org.apache.fesod.sheet.converters.integer.IntegerBooleanConverter;
import org.apache.fesod.sheet.converters.integer.IntegerNumberConverter;
import org.apache.fesod.sheet.converters.integer.IntegerStringConverter;
Expand All @@ -73,11 +76,20 @@
import org.apache.fesod.sheet.converters.shortconverter.ShortBooleanConverter;
import org.apache.fesod.sheet.converters.shortconverter.ShortNumberConverter;
import org.apache.fesod.sheet.converters.shortconverter.ShortStringConverter;
import org.apache.fesod.sheet.converters.sqldate.SqlDateDateConverter;
import org.apache.fesod.sheet.converters.sqldate.SqlDateNumberConverter;
import org.apache.fesod.sheet.converters.sqldate.SqlDateStringConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeDateConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeNumberConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeStringConverter;
import org.apache.fesod.sheet.converters.string.StringBooleanConverter;
import org.apache.fesod.sheet.converters.string.StringErrorConverter;
import org.apache.fesod.sheet.converters.string.StringNumberConverter;
import org.apache.fesod.sheet.converters.string.StringStringConverter;
import org.apache.fesod.sheet.converters.url.UrlImageConverter;
import org.apache.fesod.sheet.converters.yearmonth.YearMonthDateConverter;
import org.apache.fesod.sheet.converters.yearmonth.YearMonthNumberConverter;
import org.apache.fesod.sheet.converters.yearmonth.YearMonthStringConverter;

/**
* Load default handler
Expand Down Expand Up @@ -114,6 +126,18 @@ private static void initAllConverter() {
putAllConverter(new DateNumberConverter());
putAllConverter(new DateStringConverter());

putAllConverter(new SqlDateNumberConverter());
putAllConverter(new SqlDateStringConverter());

putAllConverter(new SqlTimeNumberConverter());
putAllConverter(new SqlTimeStringConverter());

putAllConverter(new InstantNumberConverter());
putAllConverter(new InstantStringConverter());

putAllConverter(new YearMonthNumberConverter());
putAllConverter(new YearMonthStringConverter());

putAllConverter(new LocalDateNumberConverter());
putAllConverter(new LocalDateStringConverter());

Expand Down Expand Up @@ -157,6 +181,10 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new BooleanBooleanConverter());
putWriteConverter(new ByteNumberConverter());
putWriteConverter(new DateDateConverter());
putWriteConverter(new SqlDateDateConverter());
putWriteConverter(new SqlTimeDateConverter());
putWriteConverter(new InstantDateConverter());
putWriteConverter(new YearMonthDateConverter());
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new LocalTimeDateConverter());
Expand All @@ -178,6 +206,10 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new BooleanStringConverter());
putWriteStringConverter(new ByteStringConverter());
putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new SqlDateStringConverter());
putWriteStringConverter(new SqlTimeStringConverter());
putWriteStringConverter(new InstantStringConverter());
putWriteStringConverter(new YearMonthStringConverter());
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new LocalTimeStringConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.fesod.sheet.converters.instant;

import java.time.Instant;
import java.util.Date;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;
import org.apache.fesod.sheet.util.WorkBookUtil;

/**
* Instant and date converter
*
*/
public class InstantDateConverter implements Converter<Instant> {

@Override
public Class<Instant> supportJavaTypeKey() {
return Instant.class;
}

@Override
public WriteCellData<?> convertToExcelData(
Instant value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
throws Exception {
WriteCellData<?> cellData = new WriteCellData<>(value == null ? null : Date.from(value));
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat);
return cellData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.fesod.sheet.converters.instant;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;

/**
* Instant and number converter
*
* <p>Excel stores a date as a number without a time zone, so the value is read and written with the default time
* zone of the JVM, the same way {@link java.util.Date} is handled.
*/
public class InstantNumberConverter implements Converter<Instant> {

@Override
public Class<?> supportJavaTypeKey() {
return Instant.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}

@Override
public Instant convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
LocalDateTime localDateTime = DateUtils.getLocalDateTime(
cellData.getNumberValue().doubleValue(), DateUtils.isDate1904(contentProperty, globalConfiguration));
return localDateTime.atZone(ZoneId.systemDefault()).toInstant();
}

@Override
public WriteCellData<?> convertToExcelData(
Instant value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(value, ZoneId.systemDefault());
return new WriteCellData<>(BigDecimal.valueOf(
DateUtil.getExcelDate(localDateTime, DateUtils.isDate1904(contentProperty, globalConfiguration))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.fesod.sheet.converters.instant;

import java.text.ParseException;
import java.time.Instant;
import java.util.Date;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;

/**
* Instant and string converter
*
*/
public class InstantStringConverter implements Converter<Instant> {

@Override
public Class<?> supportJavaTypeKey() {
return Instant.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

@Override
public Instant convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)
throws ParseException {
String stringValue = cellData.getStringValue();
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.parseDate(stringValue, null).toInstant();
}
return DateUtils.parseDate(
stringValue, contentProperty.getDateTimeFormatProperty().getFormat())
.toInstant();
}

@Override
public WriteCellData<?> convertToExcelData(
Instant value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
Date date = Date.from(value);
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(DateUtils.format(date, null));
}
return new WriteCellData<>(DateUtils.format(
date, contentProperty.getDateTimeFormatProperty().getFormat()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.fesod.sheet.converters.sqldate;

import java.sql.Date;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;
import org.apache.fesod.sheet.util.WorkBookUtil;

/**
* Sql date and date converter
*
*/
public class SqlDateDateConverter 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.defaultLocalDateFormat);
return cellData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.fesod.sheet.converters.sqldate;

import java.math.BigDecimal;
import java.sql.Date;
import java.time.LocalDate;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;

/**
* Sql date and number converter
*
*/
public class SqlDateNumberConverter implements Converter<Date> {

@Override
public Class<?> supportJavaTypeKey() {
return Date.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}

@Override
public Date convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
LocalDate localDate = DateUtils.getLocalDate(
cellData.getNumberValue().doubleValue(), DateUtils.isDate1904(contentProperty, globalConfiguration));
return Date.valueOf(localDate);
}

@Override
public WriteCellData<?> convertToExcelData(
Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(BigDecimal.valueOf(
DateUtil.getExcelDate(value, DateUtils.isDate1904(contentProperty, globalConfiguration))));
}
}
Loading