diff --git a/src/main/java/com/alibaba/excel/util/ClassUtils.java b/src/main/java/com/alibaba/excel/util/ClassUtils.java index 491d4b3d..9020d56c 100644 --- a/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/src/main/java/com/alibaba/excel/util/ClassUtils.java @@ -16,14 +16,18 @@ import java.util.concurrent.ConcurrentHashMap; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.format.DateTimeFormat; +import com.alibaba.excel.annotation.format.NumberFormat; import com.alibaba.excel.annotation.write.style.ContentFontStyle; import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.converters.AutoConverter; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.metadata.Holder; +import com.alibaba.excel.metadata.property.DateTimeFormatProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.FontProperty; +import com.alibaba.excel.metadata.property.NumberFormatProperty; import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.write.metadata.holder.WriteHolder; @@ -175,6 +179,12 @@ public class ClassUtils { contentFontStyle = parentContentFontStyle; } excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle)); + + excelContentProperty.setDateTimeFormatProperty( + DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class))); + excelContentProperty.setNumberFormatProperty( + NumberFormatProperty.build(field.getAnnotation(NumberFormat.class))); + fieldContentMap.put(field.getName(), excelContentProperty); } return fieldContentMap; diff --git a/src/main/java/com/alibaba/excel/util/StyleUtil.java b/src/main/java/com/alibaba/excel/util/StyleUtil.java index 93ae6577..1bafcdc8 100644 --- a/src/main/java/com/alibaba/excel/util/StyleUtil.java +++ b/src/main/java/com/alibaba/excel/util/StyleUtil.java @@ -142,7 +142,7 @@ public class StyleUtil { return null; } Font font = createFont(workbook, originFont, writeFont); - if (writeFont == null) { + if (writeFont == null || font == null) { return font; } if (writeFont.getFontName() != null) { diff --git a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java index 692a510a..7f52cd1d 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -54,7 +54,6 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class); for (Map.Entry entry : getHeadMap().entrySet()) { - Integer index = entry.getKey(); Head headData = entry.getValue(); if (headData == null) { throw new IllegalArgumentException( diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java index 6cd629f6..e85d63a6 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java @@ -91,7 +91,7 @@ public class FillDataTest { public void t03FillCsv() { ExcelGenerateException excelGenerateException = Assert.assertThrows(ExcelGenerateException.class, () -> fill(fileCsv, simpleTemplateCsv)); - Assert.assertEquals("Calling the 'fill' method must use a template.", excelGenerateException.getMessage()); + Assert.assertEquals("csv cannot use template.", excelGenerateException.getMessage()); } @Test diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java index 8e418549..210a5b9f 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.FileInputStream; import java.util.List; -import com.alibaba.easyexcel.test.core.fill.FillData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.metadata.Head; @@ -72,7 +71,7 @@ public class FillStyleDataTest { Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); XSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5.2", cell1.getStringCellValue()); + Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); Assert.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); Assert.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); @@ -126,7 +125,7 @@ public class FillStyleDataTest { Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); HSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5.2", cell1.getStringCellValue()); + Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); Assert.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); Assert.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) @@ -166,7 +165,7 @@ public class FillStyleDataTest { } private void fill(File file, File template) throws Exception { - EasyExcel.write(file, FillData.class).withTemplate(template).sheet().doFill(data()); + EasyExcel.write(file, FillStyleData.class).withTemplate(template).sheet().doFill(data()); } @Test diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index 957894e2..e1038f03 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -1,6 +1,7 @@ package com.alibaba.easyexcel.test.core.large; import java.io.File; +import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; @@ -9,6 +10,10 @@ import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; +import org.apache.poi.xssf.streaming.SXSSFCell; +import org.apache.poi.xssf.streaming.SXSSFRow; +import org.apache.poi.xssf.streaming.SXSSFSheet; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; @@ -25,11 +30,16 @@ public class LargeDataTest { private static File fileFill07; private static File template07; private static File fileCsv; + private static File fileWrite07; + private static File fileWritePoi07; + private int i = 0; @BeforeClass public static void init() { fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx"); + fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx"); + fileWritePoi07 = TestFileUtil.createNewFile("large" + File.separator + "fileWritePoi07.xlsx"); template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx"); fileCsv = TestFileUtil.createNewFile("largefileCsv.csv"); } @@ -72,6 +82,39 @@ public class LargeDataTest { LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start); } + @Test + public void t04Write() throws Exception { + long start = System.currentTimeMillis(); + ExcelWriter excelWriter = EasyExcel.write(fileWrite07).build(); + WriteSheet writeSheet = EasyExcel.writerSheet().build(); + for (int j = 0; j < 100; j++) { + excelWriter.write(data(), writeSheet); + LOGGER.info("{} write success.", j); + } + excelWriter.finish(); + LOGGER.info("write cost:{}", System.currentTimeMillis() - start); + start = System.currentTimeMillis(); + try (FileOutputStream fileOutputStream = new FileOutputStream(fileWritePoi07)) { + SXSSFWorkbook workbook = new SXSSFWorkbook(); + SXSSFSheet sheet = workbook.createSheet("sheet1"); + for (int i = 0; i < 100 * 5000; i++) { + SXSSFRow row = sheet.createRow(i); + for (int j = 0; j < 25; j++) { + SXSSFCell cell = row.createCell(j); + cell.setCellValue("str-" + j + "-" + i); + } + if (i % 5000 == 0) { + LOGGER.info("{} write success.", i); + } + } + workbook.write(fileOutputStream); + workbook.dispose(); + workbook.close(); + } + + LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start); + } + private List data() { List list = new ArrayList<>(); int size = i + 5000; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java index 2f36d665..b307ddb4 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java @@ -22,11 +22,12 @@ public class UnCamelDataListener extends AnalysisEventListener { public void invokeHeadMap(Map headMap, AnalysisContext context) { log.debug("Head is:{}", JSON.toJSONString(headMap)); Assert.assertEquals(headMap.get(0), "string1"); - Assert.assertEquals(headMap.get(1), "String2"); - Assert.assertEquals(headMap.get(2), "sTring3"); + Assert.assertEquals(headMap.get(1), "string2"); + Assert.assertEquals(headMap.get(2), "STring3"); Assert.assertEquals(headMap.get(3), "STring4"); Assert.assertEquals(headMap.get(4), "STRING5"); Assert.assertEquals(headMap.get(5), "STRing6"); + } @Override