Commit 43d8eca7 by 钟明宏

增加一半公式单元格解析

parent 64f5caa3
......@@ -46,8 +46,13 @@ public class ExcelRowDataModel<T> {
if (modelPropertyDescriptor.getExcelFieldRowByExcelTitle(excelTitle).require())
throw new ExcelToolsException("数据为空", row.getRowNum(), i + 1, excelTitle);
} else {
Object value = ValueSelector.select(cell, field);
propertyDescriptor.getWriteMethod().invoke(entity, value);
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);
}
}
}
......
package cn.yunmaozj.tools.excel.utils;
import cn.yunmaozj.tools.excel.model.ModelPropertyDescriptor;
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.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Date;
import static org.apache.poi.ss.usermodel.CellType.BOOLEAN;
import static org.apache.poi.ss.usermodel.CellType.NUMERIC;
import static org.apache.poi.ss.usermodel.CellType.STRING;
import static org.apache.poi.ss.usermodel.CellType.*;
/**
* @author zhongminghong
......@@ -25,6 +29,7 @@ public class ValueSelector<T> {
}
public static Object select(Cell cell, Field field) {
if (field.getType().equals(String.class)) {
if (cell.getCellTypeEnum() == STRING) {
return cell.getStringCellValue();
......@@ -35,6 +40,9 @@ public class ValueSelector<T> {
if (cell.getCellTypeEnum() == NUMERIC) {
return new BigDecimal(cell.getNumericCellValue()).toString();
}
if (cell.getCellTypeEnum() == FORMULA) {
return new StringBuilder().append(cell.getCellFormula()).toString();
}
return null;
}
if (field.getType().equals(Integer.class)) {
......@@ -48,9 +56,12 @@ public class ValueSelector<T> {
if (cell.getCellTypeEnum() == NUMERIC) {
return new BigDecimal(cell.getNumericCellValue()).intValue();
}
return false;
if (cell.getCellTypeEnum() == FORMULA) {
return Double.valueOf(cell.getNumericCellValue()).intValue();
}
return 0;
}
if(field.getType().equals(int.class)){
if (field.getType().equals(int.class)) {
if (cell.getCellTypeEnum() == STRING) {
String value = cell.getStringCellValue();
return StringUtils.hasLength(value) ? Integer.parseInt(cell.getStringCellValue()) : 0;
......@@ -61,8 +72,11 @@ public class ValueSelector<T> {
if (cell.getCellTypeEnum() == NUMERIC) {
return new BigDecimal(cell.getNumericCellValue()).intValue();
}
if (cell.getCellTypeEnum() == FORMULA) {
return Double.valueOf(cell.getNumericCellValue()).intValue();
}
}
if(field.getType().equals(Double.class)){
if (field.getType().equals(Double.class)) {
if (cell.getCellTypeEnum() == STRING) {
String value = cell.getStringCellValue();
return StringUtils.hasLength(value) ? Double.parseDouble(cell.getStringCellValue()) : null;
......@@ -73,8 +87,11 @@ public class ValueSelector<T> {
if (cell.getCellTypeEnum() == NUMERIC) {
return new BigDecimal(cell.getNumericCellValue()).doubleValue();
}
if (cell.getCellTypeEnum() == FORMULA) {
return cell.getNumericCellValue();
}
}
if(field.getType().equals(double.class)){
if (field.getType().equals(double.class)) {
if (cell.getCellTypeEnum() == STRING) {
String value = cell.getStringCellValue();
return StringUtils.hasLength(value) ? Double.parseDouble(cell.getStringCellValue()) : 0;
......@@ -85,8 +102,20 @@ public class ValueSelector<T> {
if (cell.getCellTypeEnum() == NUMERIC) {
return new BigDecimal(cell.getNumericCellValue()).doubleValue();
}
if (cell.getCellTypeEnum() == FORMULA) {
return cell.getNumericCellValue();
}
}
if (field.getType().equals(Date.class)) {
if (cell.getCellTypeEnum() == NUMERIC) {
return HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
}
if (cell.getCellTypeEnum() == FORMULA) {
return HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
}
HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
}
return cell;
return null;
}
public static void selectAndSet(Row row, int index, Field field, Object value) {
......@@ -128,5 +157,11 @@ public class ValueSelector<T> {
cell.setCellValue((Double) value);
}
}
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));
}
}
}
}
......@@ -7,6 +7,8 @@ import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Date;
/**
* @author zhongminghong
* @email zhongmh@yunmaozj.com
......@@ -49,6 +51,9 @@ public class OpusModel {
@ExcelFieldRow(title = "sys weight")
private double sysWeight;
@ExcelFieldRow(title = "发布时间")
private Date createDate;
public String getTitle() {
return title;
}
......
......@@ -11,7 +11,9 @@ import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
......@@ -36,6 +38,7 @@ public class DefaultExcelImportTest {
model.setOpusType("video");
model.setTopics("test");
model.setLikeNumber(null);
model.setCreateDate(new Date());
DefaultExcelExport excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, Arrays.asList(model)));
......@@ -67,4 +70,9 @@ public class DefaultExcelImportTest {
opusModel = list.get(0);
Assert.assertEquals("test",opusModel.getTopics());
}
@Test
public void demo(){
}
}
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