mirror of https://github.com/alibaba/easyexcel
15 changed files with 490 additions and 20 deletions
@ -0,0 +1,34 @@
|
||||
package com.alibaba.excel.converters.localdate; |
||||
|
||||
import java.time.LocalDate; |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* LocalDate converter |
||||
* |
||||
* @author ywzou |
||||
*/ |
||||
public class LocalDateConverter implements Converter<LocalDate> { |
||||
|
||||
@Override |
||||
public Class<LocalDate> supportJavaTypeKey() { |
||||
return LocalDate.class; |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(LocalDate 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.DATE_FORMAT_10); |
||||
return cellData; |
||||
} |
||||
} |
@ -0,0 +1,50 @@
|
||||
package com.alibaba.excel.converters.localdate; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.time.LocalDate; |
||||
|
||||
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; |
||||
|
||||
import org.apache.poi.ss.usermodel.DateUtil; |
||||
|
||||
/** |
||||
* LocalDate and number converter |
||||
* |
||||
* @author ywzou |
||||
*/ |
||||
public class LocalDateNumberConverter implements Converter<LocalDate> { |
||||
|
||||
@Override |
||||
public Class<LocalDate> supportJavaTypeKey() { |
||||
return LocalDate.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.NUMBER; |
||||
} |
||||
|
||||
@Override |
||||
public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { |
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { |
||||
return DateUtils.getLocalDate(cellData.getNumberValue().doubleValue(), globalConfiguration.getUse1904windowing()); |
||||
} else { |
||||
return DateUtils.getLocalDate(cellData.getNumberValue().doubleValue(), contentProperty.getDateTimeFormatProperty().getUse1904windowing()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(LocalDate 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,47 @@
|
||||
package com.alibaba.excel.converters.localdate; |
||||
|
||||
import java.text.ParseException; |
||||
import java.time.LocalDate; |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* LocalDate and string converter |
||||
* |
||||
* @author ywzou |
||||
*/ |
||||
public class LocalDateStringConverter implements Converter<LocalDate> { |
||||
@Override |
||||
public Class<LocalDate> supportJavaTypeKey() { |
||||
return LocalDate.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.STRING; |
||||
} |
||||
|
||||
@Override |
||||
public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws ParseException { |
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { |
||||
return DateUtils.parseLocalDate(cellData.getStringValue(), null, globalConfiguration.getLocale()); |
||||
} else { |
||||
return DateUtils.parseLocalDate(cellData.getStringValue(), contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public WriteCellData<?> convertToExcelData(LocalDate 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,114 @@
|
||||
package com.alibaba.easyexcel.test.core.localdate; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.time.LocalDate; |
||||
import java.time.LocalDateTime; |
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField; |
||||
|
||||
/** |
||||
* @author ywzou |
||||
*/ |
||||
public class DataEntry { |
||||
private String userName; |
||||
private int age; |
||||
private boolean hashChild; |
||||
private BigDecimal income; |
||||
private double height; |
||||
private Double weight; |
||||
|
||||
// @DateTimeFormat(value = "yyyy-MM-dd")
|
||||
@JSONField(format = "yyyy-MM-dd HH:mm:ss") |
||||
private LocalDateTime birthdayTime;// yyyy-MM-dd HH:mm:ss
|
||||
|
||||
@JSONField(format = "yyyy-MM-dd") |
||||
private LocalDate birthday;// yyyy-MM-dd
|
||||
|
||||
@JSONField(format = "yyyy-MM-dd HH:mm:ss") |
||||
private Date createDate;// yyyy-MM-dd HH:mm:ss
|
||||
|
||||
// @DateTimeFormat(value = "yyyy-MM-dd")
|
||||
@JSONField(format = "yyyy-MM-dd HH:mm:ss") |
||||
private Date modifyDate;// yyyy-MM-dd HH:mm:ss
|
||||
|
||||
public String getUserName() { |
||||
return userName; |
||||
} |
||||
|
||||
public void setUserName(String userName) { |
||||
this.userName = userName; |
||||
} |
||||
|
||||
public int getAge() { |
||||
return age; |
||||
} |
||||
|
||||
public void setAge(int age) { |
||||
this.age = age; |
||||
} |
||||
|
||||
public boolean isHashChild() { |
||||
return hashChild; |
||||
} |
||||
|
||||
public void setHashChild(boolean hashChild) { |
||||
this.hashChild = hashChild; |
||||
} |
||||
|
||||
public BigDecimal getIncome() { |
||||
return income; |
||||
} |
||||
|
||||
public void setIncome(BigDecimal income) { |
||||
this.income = income; |
||||
} |
||||
|
||||
public double getHeight() { |
||||
return height; |
||||
} |
||||
|
||||
public void setHeight(double height) { |
||||
this.height = height; |
||||
} |
||||
|
||||
public Double getWeight() { |
||||
return weight; |
||||
} |
||||
|
||||
public void setWeight(Double weight) { |
||||
this.weight = weight; |
||||
} |
||||
|
||||
public LocalDateTime getBirthdayTime() { |
||||
return birthdayTime; |
||||
} |
||||
|
||||
public void setBirthdayTime(LocalDateTime birthdayTime) { |
||||
this.birthdayTime = birthdayTime; |
||||
} |
||||
|
||||
public LocalDate getBirthday() { |
||||
return birthday; |
||||
} |
||||
|
||||
public void setBirthday(LocalDate birthday) { |
||||
this.birthday = birthday; |
||||
} |
||||
|
||||
public Date getCreateDate() { |
||||
return createDate; |
||||
} |
||||
|
||||
public void setCreateDate(Date createDate) { |
||||
this.createDate = createDate; |
||||
} |
||||
|
||||
public Date getModifyDate() { |
||||
return modifyDate; |
||||
} |
||||
|
||||
public void setModifyDate(Date modifyDate) { |
||||
this.modifyDate = modifyDate; |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.easyexcel.test.core.localdate; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.read.listener.ReadListener; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
|
||||
/** |
||||
* 读的监听器 |
||||
* |
||||
* @author ywzou |
||||
* |
||||
*/ |
||||
public class EntryListener implements ReadListener<DataEntry> { |
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(EntryListener.class); |
||||
|
||||
private List<DataEntry> entries = new ArrayList<>(); |
||||
|
||||
// 这个每一条数据解析都会来调用
|
||||
@Override |
||||
public void invoke(DataEntry data, AnalysisContext context) { |
||||
log.info("解析到一条数据:{}", JSONObject.toJSONString(data)); |
||||
entries.add(data); |
||||
} |
||||
|
||||
// 所有数据解析完成了 都会来调用
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
|
||||
} |
||||
|
||||
public List<DataEntry> getEntries() { |
||||
return entries; |
||||
} |
||||
} |
@ -0,0 +1,99 @@
|
||||
package com.alibaba.easyexcel.test.core.localdate; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.time.LocalDate; |
||||
import java.time.LocalDateTime; |
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.util.ListUtils; |
||||
import com.alibaba.fastjson2.JSON; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class LocalDateTest { |
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(LocalDateTest.class); |
||||
|
||||
// 读取
|
||||
@Test |
||||
public void read() throws Exception { |
||||
String fileName = TestFileUtil.getPath() + "localdate/local-date-time.xlsx"; |
||||
EntryListener entryListener = new EntryListener(); |
||||
EasyExcel.read(fileName, DataEntry.class, entryListener).sheet().doRead(); |
||||
log.info("读取到的数据==>{}", JSON.toJSONString(entryListener.getEntries())); |
||||
} |
||||
|
||||
// 读取
|
||||
@Test |
||||
public void read2() throws Exception { |
||||
String fileName = TestFileUtil.getPath() + "localdate/local-date-time2.xlsx"; |
||||
EntryListener entryListener = new EntryListener(); |
||||
EasyExcel.read(fileName, DataEntry.class, entryListener).sheet().doRead(); |
||||
log.info("读取到的数据==>{}", JSON.toJSONString(entryListener.getEntries())); |
||||
} |
||||
|
||||
// 写
|
||||
@Test |
||||
public void write() throws Exception { |
||||
|
||||
List<DataEntry> entries = buildData(); |
||||
|
||||
String fileName = TestFileUtil.getPath() + "local-date-time-" + System.currentTimeMillis() + ".xlsx"; |
||||
|
||||
EasyExcel.write(fileName, DataEntry.class).sheet("demo").doWrite(entries); |
||||
} |
||||
|
||||
// 填充
|
||||
@Test |
||||
public void fill() throws Exception { |
||||
List<DataEntry> entries = buildData(); |
||||
String templateFileName = TestFileUtil.getPath() + "localdate/local-date-time-fill.xlsx"; |
||||
|
||||
String fileName = TestFileUtil.getPath() + "local-date-time-fill-" + System.currentTimeMillis() + ".xlsx"; |
||||
|
||||
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(entries); |
||||
} |
||||
|
||||
private List<DataEntry> buildData() { |
||||
List<DataEntry> entries = ListUtils.newArrayList(); |
||||
BigDecimal hundred = new BigDecimal(1000); |
||||
LocalDateTime localDateTime = LocalDateTime.now(); |
||||
LocalDate localDate = LocalDate.now(); |
||||
Date date = new Date(); |
||||
Calendar calendar = Calendar.getInstance(); |
||||
for (int i = 1; i < 11; i++) { |
||||
calendar.setTime(date); |
||||
|
||||
DataEntry item = new DataEntry(); |
||||
item.setUserName("王麻子" + i); |
||||
item.setAge(i * 10); |
||||
item.setHashChild(i % 2 > 0); |
||||
item.setIncome(hundred.multiply(new BigDecimal(i))); |
||||
item.setHeight(i % 2 > 0 ? 1.72 : 1.68); |
||||
item.setWeight(i % 2 > 0 ? 50.00 : 40.00); |
||||
item.setBirthdayTime(localDateTime.plusDays(i)); |
||||
item.setBirthday(localDate.plusDays(i)); |
||||
|
||||
calendar.add(Calendar.DAY_OF_MONTH, i); |
||||
item.setCreateDate(calendar.getTime()); |
||||
|
||||
calendar.add(Calendar.HOUR_OF_DAY, i); |
||||
item.setModifyDate(calendar.getTime()); |
||||
entries.add(item); |
||||
} |
||||
return entries; |
||||
} |
||||
|
||||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue