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. 10
      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) { private boolean selectSheetFromCache(WriteSheet writeSheet) {
writeSheetHolder = null; writeSheetHolder = null;
Integer sheetNo = null; Integer sheetNo = writeSheet.getSheetNo();
if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) { if (sheetNo == null && StringUtils.isEmpty(writeSheet.getSheetName())) {
sheetNo = 0; sheetNo = 0;
} }
if (sheetNo != null) { 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(); RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
DecimalFormat decimalFormat = new DecimalFormat(format); DecimalFormat decimalFormat = new DecimalFormat(format);
decimalFormat.setRoundingMode(roundingMode); decimalFormat.setRoundingMode(roundingMode);
decimalFormat.setParseBigDecimal(true);
return decimalFormat.parse(string); 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.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; 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.WriteSheet;
/** /**
@ -33,16 +34,21 @@ public class FillTempTest {
public void complexFill() { public void complexFill() {
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替 // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
// {} 代表普通变量 {.} 代表是list的变量 // {} 代表普通变量 {.} 代表是list的变量
String templateFileName = "D:\\test\\simple.xlsx"; OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1);
String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "complexFill" + System.currentTimeMillis() + ".xlsx";
ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build(); ExcelWriter excelWriter = EasyExcel.write(fileName).registerWriteHandler(onceAbsoluteMergeStrategy).withTemplate(TestFileUtil.readUserHomeFile("test/simple.xlsx")).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet0 = EasyExcel.writerSheet(0).build();
excelWriter.fill(teamp(), writeSheet); WriteSheet writeSheet1 = EasyExcel.writerSheet(1).build();
excelWriter.fill(teamp(), writeSheet0);
excelWriter.fill(teamp(), writeSheet1);
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("date", "2019年10月9日13:28:28"); map.put("date", "2019年10月9日13:28:28");
map.put("total", 1000); map.put("total", 1000);
excelWriter.fill(map, writeSheet); excelWriter.fill(map, writeSheet0);
excelWriter.finish(); excelWriter.finish();
} }

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

@ -28,6 +28,7 @@ import com.alibaba.fastjson.JSON;
**/ **/
@Ignore @Ignore
public class Lock2Test { public class Lock2Test {
private static final Logger LOGGER = LoggerFactory.getLogger(Lock2Test.class); private static final Logger LOGGER = LoggerFactory.getLogger(Lock2Test.class);
@Test @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 @Test
public void write() throws Exception { public void write() throws Exception {
String fileName = TestFileUtil.getPath() + "styleWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "styleWrite" + System.currentTimeMillis() + ".xlsx";

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

@ -1,5 +1,7 @@
package com.alibaba.easyexcel.test.temp; package com.alibaba.easyexcel.test.temp;
import com.alibaba.excel.annotation.format.NumberFormat;
import lombok.Data; import lombok.Data;
/** /**
@ -9,7 +11,8 @@ import lombok.Data;
**/ **/
@Data @Data
public class LockData { public class LockData {
private String string0; @NumberFormat("#.##%")
private Double string0;
private String string1; private String string1;
private String string2; private String string2;
private String string3; 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 * @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); private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
/** /**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收 * 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/ */
private static final int BATCH_COUNT = 5; private static final int BATCH_COUNT = 5;
List<Map<Integer, CellData>> list = new ArrayList<Map<Integer, CellData>>(); List<LockData> list = new ArrayList<LockData>();
@Override @Override
public void invoke(Map<Integer, CellData> data, AnalysisContext context) { public void invoke(LockData data, AnalysisContext context) {
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
list.add(data); list.add(data);
if (list.size() >= BATCH_COUNT) { 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.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; 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.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.metadata.style.WriteFont;
@ -37,8 +38,10 @@ public class WriteV33Test {
public void test() throws Exception { public void test() throws Exception {
// 方法2 如果写到不同的sheet 同一个对象 // 方法2 如果写到不同的sheet 同一个对象
String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; 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 writeSheet1 = EasyExcel.writerSheet(1, "模板1").build();
WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "模板2").build(); WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "模板2").build();
WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "模板3").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++) { for (int i = 0; i < 10; i++) {
WriteData data = new WriteData(); WriteData data = new WriteData();
data.setF(300.35f); data.setF(300.35f);
data.setF1(33f);
list.add(data); list.add(data);
} }
return list; return list;

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

@ -11,6 +11,8 @@ import lombok.Data;
**/ **/
@Data @Data
public class WriteData { public class WriteData {
@ContentStyle(locked = true) // @ContentStyle(locked = true)
private float f; 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) * 修复xls直接读取第2页 `NPE` 的bug [Issue #1280](https://github.com/alibaba/easyexcel/issues/1280)
* 修复填充的时候,最后一行中间有空行会创建失败的bug * 修复填充的时候,最后一行中间有空行会创建失败的bug
* 修复`includeColumnIndexes`不包含第列 会无法导出数据的bug [Issue #1346](https://github.com/alibaba/easyexcel/issues/1346) * 修复`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 # 2.2.3
* 修改填充数据空数据的bug [Issue #1274](https://github.com/alibaba/easyexcel/issues/1274) * 修改填充数据空数据的bug [Issue #1274](https://github.com/alibaba/easyexcel/issues/1274)

Loading…
Cancel
Save