Commit f6a94ae1 by 钟明宏

增加对日期类型的时间格式解析

parent 43d8eca7
......@@ -44,13 +44,13 @@ public class ExcelRowDataModel<T> {
//判断数据是否为空,这里应该放到selector里面
if (cell == null) {
if (modelPropertyDescriptor.getExcelFieldRowByExcelTitle(excelTitle).require())
throw new ExcelToolsException("数据为空", row.getRowNum(), i + 1, excelTitle);
throw new ExcelToolsException("数据为空", row.getRowNum(), i + 1, excelTitle, new Exception());
} else {
try{
try {
Object value = ValueSelector.select(cell, field);
propertyDescriptor.getWriteMethod().invoke(entity, value);
}catch (Exception e){
throw new ExcelToolsException(e.getMessage(), row.getRowNum(), i + 1, excelTitle);
} catch (Exception e) {
throw new ExcelToolsException(e.getMessage(), row.getRowNum(), i + 1, excelTitle, e);
}
}
......
......@@ -11,8 +11,8 @@ public class ExcelToolsException extends RuntimeException {
private int cellNumber;
private String title;
public ExcelToolsException(String message, int rowNumber, int cellNumber, String title) {
super(message);
public ExcelToolsException(String message, int rowNumber, int cellNumber, String title, Throwable cause) {
super(message,cause);
this.rowNumber = rowNumber;
this.cellNumber = cellNumber;
this.title = title;
......
......@@ -18,9 +18,11 @@ public @interface ExcelFieldRow {
/**
* excel 表头,如果没有默认采用字段名
*
* @return
*/
String title();
/**
* 是否必填
*
......@@ -62,6 +64,14 @@ public @interface ExcelFieldRow {
* @return
*/
int maxLength() default 0;
/**
* 格式化所需要的模板
*
* @return
*/
String format() default "";
}
package cn.yunmaozj.tools.excel.utils;
import cn.yunmaozj.tools.excel.model.ModelPropertyDescriptor;
import cn.yunmaozj.tools.excel.model.annotations.ExcelFieldRow;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Date;
import static org.apache.poi.ss.usermodel.CellType.*;
......@@ -113,7 +112,21 @@ public class ValueSelector<T> {
if (cell.getCellTypeEnum() == FORMULA) {
return HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
}
HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
if (cell.getCellTypeEnum() == STRING) {
if (StringUtils.isEmpty(cell.getStringCellValue())) {
return null;
}
String stringDate = cell.getStringCellValue();
ExcelFieldRow excelFieldRow = field.getAnnotation(ExcelFieldRow.class);
if (excelFieldRow == null || StringUtils.isEmpty(excelFieldRow.format())) {
return new Date(Long.parseLong(stringDate));
}
try {
return new SimpleDateFormat(excelFieldRow.format()).parse(stringDate);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
}
return null;
}
......@@ -159,8 +172,12 @@ public class ValueSelector<T> {
}
if (field.getType().equals(Date.class)) {
Cell cell = row.createCell(index, STRING);
if(value != null){
cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value));
if (value != null) {
ExcelFieldRow excelFieldRow = field.getAnnotation(ExcelFieldRow.class);
if (excelFieldRow == null || StringUtils.isEmpty(excelFieldRow.format())) {
cell.setCellValue(((Date) value).getTime());
}
cell.setCellValue(new SimpleDateFormat(excelFieldRow.format()).format(value));
}
}
}
......
......@@ -51,86 +51,15 @@ public class OpusModel {
@ExcelFieldRow(title = "sys weight")
private double sysWeight;
@ExcelFieldRow(title = "发布时间")
@ExcelFieldRow(title = "发布时间", format = "yyyy-MM-dd HH:mm:ss")
private Date createDate;
public String getTitle() {
return title;
}
@ExcelFieldRow(title = "地区")
private String location;
public void setTitle(String title) {
this.title = title;
}
@ExcelFieldRow(title = "经度")
private double lat;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getResource() {
return resource;
}
public void setResource(String resource) {
this.resource = resource;
}
public String getOpusType() {
return opusType;
}
public void setOpusType(String opusType) {
this.opusType = opusType;
}
public String getTopics() {
return topics;
}
public void setTopics(String topics) {
this.topics = topics;
}
public Integer getBrowseNumber() {
return browseNumber;
}
public void setBrowseNumber(Integer browseNumber) {
this.browseNumber = browseNumber;
}
public Integer getLikeNumber() {
return likeNumber;
}
public void setLikeNumber(Integer likeNumber) {
this.likeNumber = likeNumber;
}
public Double getOpusWeight() {
return opusWeight;
}
public void setOpusWeight(Double opusWeight) {
this.opusWeight = opusWeight;
}
public double getSysWeight() {
return sysWeight;
}
public void setSysWeight(double sysWeight) {
this.sysWeight = sysWeight;
}
@ExcelFieldRow(title = "维度")
private double lng;
}
package com.yunmaozj.excel.model;
import cn.yunmaozj.tools.excel.model.annotations.ExcelFieldRow;
import cn.yunmaozj.tools.excel.model.annotations.Sheet;
import lombok.Getter;
import lombok.Setter;
/**
* @author zhongminghong
* @email zhongmh@yunmaozj.com
* @create 2020-07-24 9:10
*/
@Getter
@Setter
@Sheet("用户")
public class UserModel {
@ExcelFieldRow(title = "昵称")
private String nickname;
@ExcelFieldRow(title = "手机号")
private String mobile;
@ExcelFieldRow(title = "头像")
private String avatar;
@ExcelFieldRow(title = "性别", allowableValues = {"男", "女"})
private String gender;
@ExcelFieldRow(title = "生日")
private String birthday;
@ExcelFieldRow(title = "个性签名")
private String signature;
}
......@@ -8,8 +8,12 @@ import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class DefaultExcelExportTest {
private String excelFile;
......@@ -26,7 +30,7 @@ public class DefaultExcelExportTest {
public void writeFileTest() throws IOException {
OpusModel model = new OpusModel();
model.setContent("I wang ride bicycle");
model.setContent("I want ride bicycle");
model.setTitle("English");
model.setMobile("1521521234");
model.setResource("/2020/22/2/2/2.jpg");
......@@ -38,6 +42,12 @@ public class DefaultExcelExportTest {
model.setOpusWeight(10.34324324);
model.setSysWeight(89.42343243243);
model.setLocation("成都市");
model.setLat(1.45454354354);
model.setLng(343.434343243);
model.setCreateDate(new Date());
DefaultExcelExport excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, Arrays.asList(model)));
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "sheet 2", Arrays.asList(model)));
......@@ -46,6 +56,20 @@ public class DefaultExcelExportTest {
DefaultExcelImport excelImport = new DefaultExcelImport(excelFile);
List<OpusModel> list = excelImport.getData(new DefaultExcelImport.ImportSheet<>(OpusModel.class));
System.out.println(list.get(0));
assertTrue(list.size() == 1);
assertEquals(model.getContent(), list.get(0).getContent());
assertEquals(model.getTitle(), list.get(0).getTitle());
assertEquals(model.getMobile(), list.get(0).getMobile());
assertEquals(model.getResource(), list.get(0).getResource());
assertEquals(model.getOpusType(), list.get(0).getOpusType());
assertEquals(model.getTopics(), list.get(0).getTopics());
assertEquals(model.getBrowseNumber(), list.get(0).getBrowseNumber());
assertEquals(model.getSysWeight(), list.get(0).getSysWeight(), 1);
assertEquals(model.getOpusWeight(), list.get(0).getOpusWeight());
assertEquals(model.getLocation(), list.get(0).getLocation());
assertEquals(model.getLat(), list.get(0).getLat(), 1);
assertEquals(model.getLng(), list.get(0).getLng(), 1);
assertEquals(model.getCreateDate().getTime(), list.get(0).getCreateDate().getTime());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment