Browse Source

修改默认用String读取数字不使用科学计数法 通过`useScientificFormat`修改

pull/1391/head
Jiaju Zhuang 5 years ago
parent
commit
20454248cf
  1. 1
      src/main/java/com/alibaba/excel/metadata/AbstractHolder.java
  2. 15
      src/main/java/com/alibaba/excel/metadata/BasicParameter.java
  3. 14
      src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java
  4. 33
      src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java
  5. 81
      src/main/java/com/alibaba/excel/metadata/format/ExcelGeneralNumberFormat.java
  6. 13
      src/main/java/com/alibaba/excel/read/builder/AbstractExcelReaderParameterBuilder.java
  7. 11
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  8. 13
      src/main/java/com/alibaba/excel/util/NumberDataFormatterUtils.java
  9. 4
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  10. 4
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  11. 1
      update.md

1
src/main/java/com/alibaba/excel/metadata/AbstractHolder.java

@ -68,6 +68,7 @@ public abstract class AbstractHolder implements ConfigurationHolder {
} else {
globalConfiguration.setLocale(basicParameter.getLocale());
}
}
public Boolean getNewInitialization() {

15
src/main/java/com/alibaba/excel/metadata/BasicParameter.java

@ -41,6 +41,13 @@ public class BasicParameter {
*/
private Locale locale;
/**
* Whether to use scientific Format.
*
* default is false
*/
private Boolean useScientificFormat;
public List<List<String>> getHead() {
return head;
}
@ -88,4 +95,12 @@ public class BasicParameter {
public void setLocale(Locale locale) {
this.locale = locale;
}
public Boolean getUseScientificFormat() {
return useScientificFormat;
}
public void setUseScientificFormat(Boolean useScientificFormat) {
this.useScientificFormat = useScientificFormat;
}
}

14
src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java

@ -25,6 +25,12 @@ public class GlobalConfiguration {
* used when formatting dates and numbers.
*/
private Locale locale;
/**
* Whether to use scientific Format.
*
* default is false
*/
private Boolean useScientificFormat;
public Boolean getUse1904windowing() {
return use1904windowing;
@ -49,4 +55,12 @@ public class GlobalConfiguration {
public void setLocale(Locale locale) {
this.locale = locale;
}
public Boolean getUseScientificFormat() {
return useScientificFormat;
}
public void setUseScientificFormat(Boolean useScientificFormat) {
this.useScientificFormat = useScientificFormat;
}
}

33
src/main/java/com/alibaba/excel/metadata/DataFormatter.java → src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java

@ -15,7 +15,7 @@
* can be found in svn at location root/projects/3rd-party/src
* ====================================================================
*/
package com.alibaba.excel.metadata;
package com.alibaba.excel.metadata.format;
import java.math.BigDecimal;
import java.math.RoundingMode;
@ -35,12 +35,12 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.ExcelGeneralNumberFormat;
import org.apache.poi.ss.usermodel.ExcelStyleDateFormatter;
import org.apache.poi.ss.usermodel.FractionFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.util.DateUtils;
/**
@ -138,21 +138,32 @@ public class DataFormatter {
* @return
*/
private Boolean use1904windowing;
/**
* Creates a formatter using the {@link Locale#getDefault() default locale}.
* Whether to use scientific Format.
*
* default is false
*/
public DataFormatter() {
this(null, null);
}
private Boolean useScientificFormat;
/**
* Creates a formatter using the given locale.
*
*/
public DataFormatter(Locale locale, Boolean use1904windowing) {
this.use1904windowing = use1904windowing != null ? use1904windowing : Boolean.FALSE;
this.locale = locale != null ? locale : Locale.getDefault();
public DataFormatter(GlobalConfiguration globalConfiguration) {
if (globalConfiguration == null) {
this.use1904windowing = Boolean.FALSE;
this.locale = Locale.getDefault();
this.useScientificFormat = Boolean.FALSE;
} else {
this.use1904windowing =
globalConfiguration.getUse1904windowing() != null ? globalConfiguration.getUse1904windowing()
: Boolean.FALSE;
this.locale =
globalConfiguration.getLocale() != null ? globalConfiguration.getLocale() : Locale.getDefault();
this.useScientificFormat =
globalConfiguration.getUseScientificFormat() != null ? globalConfiguration.getUseScientificFormat()
: Boolean.FALSE;
}
this.dateSymbols = DateFormatSymbols.getInstance(this.locale);
this.decimalSymbols = DecimalFormatSymbols.getInstance(this.locale);
}
@ -532,7 +543,7 @@ public class DataFormatter {
return defaultNumFormat;
// otherwise use general format
}
defaultNumFormat = new ExcelGeneralNumberFormat(locale);
defaultNumFormat = new ExcelGeneralNumberFormat(locale, useScientificFormat);
return defaultNumFormat;
}

81
src/main/java/com/alibaba/excel/metadata/format/ExcelGeneralNumberFormat.java

@ -0,0 +1,81 @@
package com.alibaba.excel.metadata.format;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Locale;
import org.apache.poi.ss.usermodel.DataFormatter;
/**
* Written with reference to {@link org.apache.poi.ss.usermodel.ExcelGeneralNumberFormat }.
* <p>
* Supported Do not use scientific notation.
*
* @author JiaJu Zhuang
**/
public class ExcelGeneralNumberFormat extends Format {
private static final long serialVersionUID = 1L;
private static final MathContext TO_10_SF = new MathContext(10, RoundingMode.HALF_UP);
private final DecimalFormatSymbols decimalSymbols;
private final DecimalFormat integerFormat;
private final DecimalFormat decimalFormat;
private final DecimalFormat scientificFormat;
public ExcelGeneralNumberFormat(final Locale locale, final boolean useScientificFormat) {
decimalSymbols = DecimalFormatSymbols.getInstance(locale);
// Supported Do not use scientific notation.
if (useScientificFormat) {
scientificFormat = new DecimalFormat("0.#####E0", decimalSymbols);
} else {
scientificFormat = new DecimalFormat("#", decimalSymbols);
}
org.apache.poi.ss.usermodel.DataFormatter.setExcelStyleRoundingMode(scientificFormat);
integerFormat = new DecimalFormat("#", decimalSymbols);
org.apache.poi.ss.usermodel.DataFormatter.setExcelStyleRoundingMode(integerFormat);
decimalFormat = new DecimalFormat("#.##########", decimalSymbols);
DataFormatter.setExcelStyleRoundingMode(decimalFormat);
}
@Override
public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
final double value;
if (number instanceof Number) {
value = ((Number) number).doubleValue();
if (Double.isInfinite(value) || Double.isNaN(value)) {
return integerFormat.format(number, toAppendTo, pos);
}
} else {
// testBug54786 gets here with a date, so retain previous behaviour
return integerFormat.format(number, toAppendTo, pos);
}
final double abs = Math.abs(value);
if (abs >= 1E11 || (abs <= 1E-10 && abs > 0)) {
return scientificFormat.format(number, toAppendTo, pos);
} else if (Math.floor(value) == value || abs >= 1E10) {
// integer, or integer portion uses all 11 allowed digits
return integerFormat.format(number, toAppendTo, pos);
}
// Non-integers of non-scientific magnitude are formatted as "up to 11
// numeric characters, with the decimal point counting as a numeric
// character". We know there is a decimal point, so limit to 10 digits.
// https://support.microsoft.com/en-us/kb/65903
final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue();
return decimalFormat.format(rounded, toAppendTo, pos);
}
@Override
public Object parseObject(String source, ParsePosition pos) {
throw new UnsupportedOperationException();
}
}

13
src/main/java/com/alibaba/excel/read/builder/AbstractExcelReaderParameterBuilder.java

@ -31,6 +31,19 @@ public abstract class AbstractExcelReaderParameterBuilder<T extends AbstractExce
return self();
}
/**
* Whether to use scientific Format.
*
* default is false
*
* @param useScientificFormat
* @return
*/
public T useScientificFormat(Boolean useScientificFormat) {
parameter().setUseScientificFormat(useScientificFormat);
return self();
}
/**
* Custom type listener run after default
*

11
src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java

@ -56,6 +56,17 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
getGlobalConfiguration().setUse1904windowing(readBasicParameter.getUse1904windowing());
}
if (readBasicParameter.getUseScientificFormat() == null) {
if (parentAbstractReadHolder == null) {
getGlobalConfiguration().setUseScientificFormat(Boolean.FALSE);
} else {
getGlobalConfiguration()
.setUseScientificFormat(parentAbstractReadHolder.getGlobalConfiguration().getUseScientificFormat());
}
} else {
getGlobalConfiguration().setUseScientificFormat(readBasicParameter.getUseScientificFormat());
}
// Initialization property
this.excelReadHeadProperty = new ExcelReadHeadProperty(this, getClazz(), getHead(), convertAllFiled);
if (readBasicParameter.getHeadRowNumber() == null) {

13
src/main/java/com/alibaba/excel/util/NumberDataFormatterUtils.java

@ -1,6 +1,6 @@
package com.alibaba.excel.util;
import com.alibaba.excel.metadata.DataFormatter;
import com.alibaba.excel.metadata.format.DataFormatter;
import com.alibaba.excel.metadata.GlobalConfiguration;
/**
@ -9,6 +9,7 @@ import com.alibaba.excel.metadata.GlobalConfiguration;
* @author Jiaju Zhuang
**/
public class NumberDataFormatterUtils {
/**
* Cache DataFormatter.
*/
@ -18,8 +19,7 @@ public class NumberDataFormatterUtils {
* Format number data.
*
* @param data
* @param dataFormat
* Not null.
* @param dataFormat Not null.
* @param dataFormatString
* @param globalConfiguration
* @return
@ -28,12 +28,7 @@ public class NumberDataFormatterUtils {
GlobalConfiguration globalConfiguration) {
DataFormatter dataFormatter = DATA_FORMATTER_THREAD_LOCAL.get();
if (dataFormatter == null) {
if (globalConfiguration != null) {
dataFormatter =
new DataFormatter(globalConfiguration.getLocale(), globalConfiguration.getUse1904windowing());
} else {
dataFormatter = new DataFormatter();
}
dataFormatter = new DataFormatter(globalConfiguration);
DATA_FORMATTER_THREAD_LOCAL.set(dataFormatter);
}
return dataFormatter.format(data, dataFormat, dataFormatString);

4
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -111,6 +111,10 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
getGlobalConfiguration().setUse1904windowing(writeBasicParameter.getUse1904windowing());
}
if (writeBasicParameter.getUseScientificFormat() != null) {
throw new UnsupportedOperationException("Currently does not support setting useScientificFormat.");
}
if (writeBasicParameter.getNeedHead() == null) {
if (parentAbstractWriteHolder == null) {
this.needHead = Boolean.TRUE;

4
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -32,9 +32,9 @@ public class Lock2Test {
@Test
public void test() throws Exception {
File file = TestFileUtil.readUserHomeFile("test/t3.xls");
File file = TestFileUtil.readUserHomeFile("test/test.xlsx");
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync();
List<Object> list = EasyExcel.read(file).useScientificFormat(false).sheet().headRowNumber(0).doReadSync();
LOGGER.info("数据:{}", list.size());
for (Object data : list) {
LOGGER.info("返回数据:{}", JSON.toJSONString(data));

1
update.md

@ -1,5 +1,6 @@
# 2.2.4
* 撤销删除`AbstractMergeStrategy`
* 修改默认用String读取数字不使用科学计数法 通过`useScientificFormat`修改
# 2.2.3

Loading…
Cancel
Save