Browse Source

修复`@NumberFormat`注解转换double时可能会丢失精度 #1306

developing
Jiaju Zhuang 5 years ago
parent
commit
cf40b873bd
  1. 4
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  2. 1
      src/main/java/com/alibaba/excel/util/NumberUtils.java
  3. 16
      src/test/java/com/alibaba/easyexcel/test/temp/FillTempTest.java
  4. 14
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  5. 5
      src/test/java/com/alibaba/easyexcel/test/temp/LockData.java
  6. 6
      src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java
  7. 5
      src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java
  8. 1
      src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java
  9. 4
      src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java
  10. 1
      update.md

4
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -124,8 +124,8 @@ public class WriteContextImpl implements WriteContext {
private boolean selectSheetFromCache(WriteSheet writeSheet) {
writeSheetHolder = null;
Integer sheetNo = null;
if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) {
Integer sheetNo = writeSheet.getSheetNo();
if (sheetNo == null && StringUtils.isEmpty(writeSheet.getSheetName())) {
sheetNo = 0;
}
if (sheetNo != null) {

1
src/main/java/com/alibaba/excel/util/NumberUtils.java

@ -167,6 +167,7 @@ public class NumberUtils {
RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
DecimalFormat decimalFormat = new DecimalFormat(format);
decimalFormat.setRoundingMode(roundingMode);
decimalFormat.setParseBigDecimal(true);
return decimalFormat.parse(string);
}
}

16
src/test/java/com/alibaba/easyexcel/test/temp/FillTempTest.java

@ -13,6 +13,7 @@ import com.alibaba.easyexcel.test.temp.fill.FillData2;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
/**
@ -33,16 +34,21 @@ public class FillTempTest {
public void complexFill() {
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
// {} 代表普通变量 {.} 代表是list的变量
String templateFileName = "D:\\test\\simple.xlsx";
OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1);
String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
excelWriter.fill(teamp(), writeSheet);
ExcelWriter excelWriter = EasyExcel.write(fileName).registerWriteHandler(onceAbsoluteMergeStrategy).withTemplate(TestFileUtil.readUserHomeFile("test/simple.xlsx")).build();
WriteSheet writeSheet0 = EasyExcel.writerSheet(0).build();
WriteSheet writeSheet1 = EasyExcel.writerSheet(1).build();
excelWriter.fill(teamp(), writeSheet0);
excelWriter.fill(teamp(), writeSheet1);
Map<String, Object> map = new HashMap<String, Object>();
map.put("date", "2019年10月9日13:28:28");
map.put("total", 1000);
excelWriter.fill(map, writeSheet);
excelWriter.fill(map, writeSheet0);
excelWriter.finish();
}

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

@ -28,6 +28,7 @@ import com.alibaba.fastjson.JSON;
**/
@Ignore
public class Lock2Test {
private static final Logger LOGGER = LoggerFactory.getLogger(Lock2Test.class);
@Test
@ -41,6 +42,15 @@ public class Lock2Test {
}
}
@Test
public void test33() throws Exception {
File file = TestFileUtil.readUserHomeFile("test/test6.xlsx");
EasyExcel.read(file, LockData.class, new LockDataListener()).sheet(0).headRowNumber(0)
.doRead();
}
@Test
public void write() throws Exception {
String fileName = TestFileUtil.getPath() + "styleWrite" + System.currentTimeMillis() + ".xlsx";
@ -49,7 +59,7 @@ public class Lock2Test {
// 背景设置为红色
headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short)20);
headWriteFont.setFontHeightInPoints((short) 20);
headWriteCellStyle.setWriteFont(headWriteFont);
// 内容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
@ -59,7 +69,7 @@ public class Lock2Test {
contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
WriteFont contentWriteFont = new WriteFont();
// 字体大小
contentWriteFont.setFontHeightInPoints((short)20);
contentWriteFont.setFontHeightInPoints((short) 20);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy =

5
src/test/java/com/alibaba/easyexcel/test/temp/LockData.java

@ -1,5 +1,7 @@
package com.alibaba.easyexcel.test.temp;
import com.alibaba.excel.annotation.format.NumberFormat;
import lombok.Data;
/**
@ -9,7 +11,8 @@ import lombok.Data;
**/
@Data
public class LockData {
private String string0;
@NumberFormat("#.##%")
private Double string0;
private String string1;
private String string2;
private String string3;

6
src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java

@ -18,16 +18,16 @@ import com.alibaba.fastjson.JSON;
*
* @author Jiaju Zhuang
*/
public class LockDataListener extends AnalysisEventListener<Map<Integer, CellData>> {
public class LockDataListener extends AnalysisEventListener<LockData> {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
/**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<Map<Integer, CellData>> list = new ArrayList<Map<Integer, CellData>>();
List<LockData> list = new ArrayList<LockData>();
@Override
public void invoke(Map<Integer, CellData> data, AnalysisContext context) {
public void invoke(LockData data, AnalysisContext context) {
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
list.add(data);
if (list.size() >= BATCH_COUNT) {

5
src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java

@ -17,6 +17,7 @@ import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
@ -37,8 +38,10 @@ public class WriteV33Test {
public void test() throws Exception {
// 方法2 如果写到不同的sheet 同一个对象
String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx";
OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1);
// 这里 指定文件
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).registerWriteHandler(onceAbsoluteMergeStrategy).build();
WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "模板1").build();
WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "模板2").build();
WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "模板3").build();

1
src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java

@ -126,6 +126,7 @@ public class Wirte {
for (int i = 0; i < 10; i++) {
WriteData data = new WriteData();
data.setF(300.35f);
data.setF1(33f);
list.add(data);
}
return list;

4
src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java

@ -11,6 +11,8 @@ import lombok.Data;
**/
@Data
public class WriteData {
@ContentStyle(locked = true)
// @ContentStyle(locked = true)
private float f;
// @ContentStyle(locked = false)
private float f1;
}

1
update.md

@ -9,6 +9,7 @@
* 修复xls直接读取第2页 `NPE` 的bug [Issue #1280](https://github.com/alibaba/easyexcel/issues/1280)
* 修复填充的时候,最后一行中间有空行会创建失败的bug
* 修复`includeColumnIndexes`不包含第列 会无法导出数据的bug [Issue #1346](https://github.com/alibaba/easyexcel/issues/1346)
* 修复`@NumberFormat`注解转换double时可能会丢失精度 [Issue #1306](https://github.com/alibaba/easyexcel/issues/1306)
# 2.2.3
* 修改填充数据空数据的bug [Issue #1274](https://github.com/alibaba/easyexcel/issues/1274)

Loading…
Cancel
Save