@@ -176,6 +183,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
this.charset = writeWorkbook.getCharset();
}
+ if (writeWorkbook.getWithBom() == null) {
+ this.withBom = Boolean.TRUE;
+ } else {
+ this.withBom = writeWorkbook.getWithBom();
+ }
+
if (writeWorkbook.getAutoCloseStream() == null) {
this.autoCloseStream = Boolean.TRUE;
} else {
diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/bom/BomData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/bom/BomData.java
index 24d8ece8..55b58fcd 100644
--- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/bom/BomData.java
+++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/bom/BomData.java
@@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test.core.bom;
import com.alibaba.excel.annotation.ExcelProperty;
+
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@@ -12,5 +13,5 @@ public class BomData {
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年纪")
- private Integer age;
+ private Long age;
}
diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/bom/BomDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/bom/BomDataTest.java
index 0c4ceacc..693708a3 100644
--- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/bom/BomDataTest.java
+++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/bom/BomDataTest.java
@@ -1,10 +1,18 @@
package com.alibaba.easyexcel.test.core.bom;
+import com.alibaba.easyexcel.test.core.simple.SimpleData;
+import com.alibaba.easyexcel.test.demo.read.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData;
+import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.listener.ReadListener;
+import com.alibaba.excel.support.ExcelTypeEnum;
+import com.alibaba.excel.util.ListUtils;
+import com.alibaba.fastjson2.JSON;
+
+import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.junit.Assert;
import org.junit.FixMethodOrder;
@@ -12,17 +20,76 @@ import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.io.File;
+import java.io.FileOutputStream;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+/**
+ * bom test
+ *
+ * @author Jiaju Zhuang
+ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+@Slf4j
public class BomDataTest {
+
+ @Test
+ public void t01ReadCsv() {
+ readCsv(TestFileUtil.readFile("bom" + File.separator + "no_bom.csv"));
+ readCsv(TestFileUtil.readFile("bom" + File.separator + "office_bom.csv"));
+ }
+
@Test
- public void t01ReadAndWriteCsv() {
- readCsv(TestFileUtil.readFile("bom" + File.separator + "bom_none.csv"));
- readCsv(TestFileUtil.readFile("bom" + File.separator + "bom_utf8.csv"));
- readCsv(TestFileUtil.readFile("bom" + File.separator + "bom_utf16be.csv"));
- readCsv(TestFileUtil.readFile("bom" + File.separator + "bom_utf16le.csv"));
+ public void t02ReadAndWriteCsv() throws Exception {
+ readAndWriteCsv(TestFileUtil.createNewFile("bom" + File.separator + "bom_default.csv"), null, null);
+ readAndWriteCsv(TestFileUtil.createNewFile("bom" + File.separator + "bom_utf_8.csv"), "UTF-8", null);
+ readAndWriteCsv(TestFileUtil.createNewFile("bom" + File.separator + "bom_utf_8_lower_case.csv"), "utf-8", null);
+ readAndWriteCsv(TestFileUtil.createNewFile("bom" + File.separator + "bom_gbk.csv"), "GBK", null);
+ readAndWriteCsv(TestFileUtil.createNewFile("bom" + File.separator + "bom_gbk_lower_case.csv"), "gbk", null);
+ readAndWriteCsv(TestFileUtil.createNewFile("bom" + File.separator + "bom_utf_16be.csv"), "UTF-16BE", null);
+ readAndWriteCsv(TestFileUtil.createNewFile("bom" + File.separator + "bom_utf_8_not_with_bom.csv"), "UTF-8",
+ Boolean.FALSE);
+ }
+
+ private void readAndWriteCsv(File file, String charsetName, Boolean withBom) throws Exception {
+ Charset charset = null;
+ if (charsetName != null) {
+ charset = Charset.forName(charsetName);
+ }
+ EasyExcel.write(new FileOutputStream(file), BomData.class)
+ .charset(charset)
+ .withBom(withBom)
+ .excelType(ExcelTypeEnum.CSV)
+ .sheet()
+ .doWrite(data());
+
+ EasyExcel.read(file, BomData.class, new ReadListener