浏览代码

加了一个numformat

4.1.3.A
jueyue 8 年前
父节点
当前提交
04a9422567
共有 5 个文件被更改,包括 50 次插入10 次删除
  1. +4
    -0
      easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java
  2. +10
    -5
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ImportParams.java
  3. +10
    -0
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelExportEntity.java
  4. +24
    -3
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportBase.java
  5. +2
    -2
      gradle/wrapper/gradle-wrapper.properties

+ 4
- 0
easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java 查看文件

@@ -43,6 +43,10 @@ public @interface Excel {
* 时间格式,相当于同时设置了exportFormat 和 importFormat * 时间格式,相当于同时设置了exportFormat 和 importFormat
*/ */
public String format() default ""; public String format() default "";
/**
* 数字格式化,参数是Pattern,使用的对象是DecimalFormat
*/
public String numFormat() default "";
/** /**
* 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符


+ 10
- 5
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ImportParams.java 查看文件

@@ -38,10 +38,13 @@ public class ImportParams extends ExcelBaseParams {
* 字段真正值和列标题之间的距离 默认0 * 字段真正值和列标题之间的距离 默认0
*/ */
private int startRows = 0; private int startRows = 0;
/** /**
* 主键设置,如何这个cell没有值,就跳过 或者认为这个是list的下面的值 * 主键设置,如何这个cell没有值,就跳过 或者认为这个是list的下面的值
* 大家不理解,去掉这个
*/ */
private Integer keyIndex = 0;
private Integer keyIndex = null;
/** /**
* 开始读取的sheet位置,默认为0 * 开始读取的sheet位置,默认为0
*/ */
@@ -84,10 +87,6 @@ public class ImportParams extends ExcelBaseParams {
return headRows; return headRows;
} }
public Integer getKeyIndex() {
return keyIndex;
}
public String getSaveUrl() { public String getSaveUrl() {
return saveUrl; return saveUrl;
} }
@@ -116,6 +115,7 @@ public class ImportParams extends ExcelBaseParams {
this.headRows = headRows; this.headRows = headRows;
} }
@Deprecated
public void setKeyIndex(Integer keyIndex) { public void setKeyIndex(Integer keyIndex) {
this.keyIndex = keyIndex; this.keyIndex = keyIndex;
} }
@@ -184,4 +184,9 @@ public class ImportParams extends ExcelBaseParams {
this.readRows = readRows; this.readRows = readRows;
} }
public Integer getKeyIndex() {
return keyIndex;
}
} }

+ 10
- 0
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelExportEntity.java 查看文件

@@ -70,6 +70,8 @@ public class ExcelExportEntity extends ExcelBaseEntity implements Comparable<Exc
*/ */
private boolean isStatistics; private boolean isStatistics;
private String numFormat;
private List<ExcelExportEntity> list; private List<ExcelExportEntity> list;
public ExcelExportEntity() { public ExcelExportEntity() {
@@ -187,6 +189,14 @@ public class ExcelExportEntity extends ExcelBaseEntity implements Comparable<Exc
this.isStatistics = isStatistics; this.isStatistics = isStatistics;
} }
public String getNumFormat() {
return numFormat;
}
public void setNumFormat(String numFormat) {
this.numFormat = numFormat;
}
@Override @Override
public int compareTo(ExcelExportEntity prev) { public int compareTo(ExcelExportEntity prev) {
return this.getOrderNum() - prev.getOrderNum(); return this.getOrderNum() - prev.getOrderNum();


+ 24
- 3
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportBase.java 查看文件

@@ -18,6 +18,7 @@ package cn.afterturn.easypoi.excel.export.base;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -28,6 +29,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -42,7 +44,7 @@ import cn.afterturn.easypoi.util.PoiPublicUtil;
import cn.afterturn.easypoi.util.PoiReflectorUtil; import cn.afterturn.easypoi.util.PoiReflectorUtil;
/** /**
* 导出基础处理,不设计POI,只设计对象,保证复用性
* 导出基础处理,不涉及POI,只涉及对象,保证复用性
* *
* @author JueYue * @author JueYue
* 2014年8月9日 下午11:01:32 * 2014年8月9日 下午11:01:32
@@ -83,7 +85,7 @@ public class ExportBase {
return excelEntity; return excelEntity;
} }
private Object formatValue(Object value, ExcelExportEntity entity) throws Exception {
private Object dateFormatValue(Object value, ExcelExportEntity entity) throws Exception {
Date temp = null; Date temp = null;
if (value instanceof String) { if (value instanceof String) {
SimpleDateFormat format = new SimpleDateFormat(entity.getDatabaseFormat()); SimpleDateFormat format = new SimpleDateFormat(entity.getDatabaseFormat());
@@ -98,6 +100,20 @@ public class ExportBase {
return value; return value;
} }
private Object numFormatValue(Object value, ExcelExportEntity entity) {
if(value == null){
return null;
}
if(!NumberUtils.isNumber(value.toString())){
LOGGER.error("data want num format ,but is not num, value is:"+value);
return null;
}
Double d = Double.parseDouble(value.toString());
DecimalFormat df = new DecimalFormat(entity.getNumFormat());
return df.format(d);
}
/** /**
* 获取需要导出的全部字段 * 获取需要导出的全部字段
* *
@@ -176,11 +192,14 @@ public class ExportBase {
: entity.getMethod().invoke(obj, new Object[] {}); : entity.getMethod().invoke(obj, new Object[] {});
} }
if (StringUtils.isNotEmpty(entity.getFormat())) { if (StringUtils.isNotEmpty(entity.getFormat())) {
value = formatValue(value, entity);
value = dateFormatValue(value, entity);
} }
if (entity.getReplace() != null && entity.getReplace().length > 0) { if (entity.getReplace() != null && entity.getReplace().length > 0) {
value = replaceValue(entity.getReplace(), String.valueOf(value)); value = replaceValue(entity.getReplace(), String.valueOf(value));
} }
if (StringUtils.isNotEmpty(entity.getNumFormat())) {
value = numFormatValue(value, entity);
}
if (needHanlderList != null && needHanlderList.contains(entity.getName())) { if (needHanlderList != null && needHanlderList.contains(entity.getName())) {
value = dataHanlder.exportHandler(obj, entity.getName(), value); value = dataHanlder.exportHandler(obj, entity.getName(), value);
} }
@@ -190,6 +209,7 @@ public class ExportBase {
return value == null ? "" : value.toString(); return value == null ? "" : value.toString();
} }
/** /**
* 获取集合的值 * 获取集合的值
* @param entity * @param entity
@@ -237,6 +257,7 @@ public class ExportBase {
excelEntity.setStatistics(excel.isStatistics()); excelEntity.setStatistics(excel.isStatistics());
excelEntity.setHyperlink(excel.isHyperlink()); excelEntity.setHyperlink(excel.isHyperlink());
excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName()));
excelEntity.setNumFormat(excel.numFormat());
} }
/** /**


+ 2
- 2
gradle/wrapper/gradle-wrapper.properties 查看文件

@@ -1,6 +1,6 @@
#Tue Dec 15 22:20:53 CST 2015
#Mon May 15 20:18:13 CST 2017
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.7-all.zip

正在加载...
取消
保存