From 6366552137eaf5bba270508b30c22920860e68af Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 29 Oct 2021 20:03:59 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E4=BF=AE=E5=A4=8D=E5=88=97=E5=AE=BD?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E6=B2=A1=E7=94=A8=E7=9A=84bug=20[Issue=20#21?= =?UTF-8?q?51]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metadata/holder/AbstractWriteHolder.java | 7 ------- .../property/ExcelWriteHeadProperty.java | 11 +++++++++++ .../test/core/annotation/AnnotationData.java | 8 +++++--- .../core/annotation/AnnotationDataTest.java | 19 +++++++++++++++++++ .../test/core/style/StyleDataTest.java | 4 ++++ 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index cc4b2f21..ca14886c 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java @@ -243,15 +243,11 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ } Map headMap = getExcelWriteHeadProperty().getHeadMap(); boolean hasColumnWidth = false; - boolean hasStyle = false; for (Head head : headMap.values()) { if (head.getColumnWidthProperty() != null) { hasColumnWidth = true; } - if (head.getHeadStyleProperty() != null || head.getHeadFontProperty() != null) { - hasStyle = true; - } dealLoopMerge(handlerList, head); } @@ -259,10 +255,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ dealColumnWidth(handlerList); } - //if (hasStyle) { dealStyle(handlerList); - //} - dealRowHigh(handlerList); dealOnceAbsoluteMerge(handlerList); } 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 7f52cd1d..84ba05a5 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentLoopMerge; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadFontStyle; @@ -17,6 +18,7 @@ import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.CellRange; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Holder; +import com.alibaba.excel.metadata.property.ColumnWidthProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty; @@ -50,9 +52,11 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { this.onceAbsoluteMergeProperty = OnceAbsoluteMergeProperty.build(headClazz.getAnnotation(OnceAbsoluteMerge.class)); + ColumnWidth parentColumnWidth = headClazz.getAnnotation(ColumnWidth.class); HeadStyle parentHeadStyle = headClazz.getAnnotation(HeadStyle.class); HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class); + for (Map.Entry entry : getHeadMap().entrySet()) { Head headData = entry.getValue(); if (headData == null) { @@ -61,6 +65,13 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { } Field field = headData.getField(); + ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class); + if (columnWidth == null) { + columnWidth = parentColumnWidth; + } + headData.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth)); + + HeadStyle headStyle = field.getAnnotation(HeadStyle.class); if (headStyle == null) { headStyle = parentHeadStyle; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java index d1c2a4f7..40bc4b0b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java @@ -16,16 +16,18 @@ import lombok.Data; * @author Jiaju Zhuang */ @Data -@ColumnWidth(30) -@HeadRowHeight(15) -@ContentRowHeight(20) +@ColumnWidth(50) +@HeadRowHeight(50) +@ContentRowHeight(100) public class AnnotationData { @ExcelProperty("日期") @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") private Date date; + @ExcelProperty(value = "数字") @NumberFormat("#.##%") private Double number; + @ExcelIgnore private String ignore; private static final String staticFinal = "test"; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java index 7a4dfe21..02bd3159 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java @@ -8,6 +8,11 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.util.DateUtils; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -46,6 +51,20 @@ public class AnnotationDataTest { EasyExcel.write().file(file).head(AnnotationData.class).sheet().doWrite(data()); EasyExcel.read().file(file).head(AnnotationData.class).registerReadListener(new AnnotationDataListener()) .sheet().doRead(); + + if (file == fileCsv) { + return; + } + + Workbook workbook = WorkbookFactory.create(file); + Sheet sheet = workbook.getSheetAt(0); + Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0); + + Row row0 = sheet.getRow(0); + Assert.assertEquals(1000, row0.getHeight(), 0); + + Row row1 = sheet.getRow(1); + Assert.assertEquals(2000, row1.getHeight(), 0); } private List data() throws Exception { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java index a48c3238..e9323761 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java @@ -209,7 +209,10 @@ public class StyleDataTest { Workbook workbook = WorkbookFactory.create(file); Sheet sheet = workbook.getSheetAt(0); + Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0); + Row row0 = sheet.getRow(0); + Assert.assertEquals(800, row0.getHeight(), 0); Cell cell00 = row0.getCell(0); Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell00)); Assert.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell00, workbook)); @@ -221,6 +224,7 @@ public class StyleDataTest { Assert.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell01, workbook)); Row row1 = sheet.getRow(1); + Assert.assertEquals(1000, row1.getHeight(), 0); Cell cell10 = row1.getCell(0); Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell10)); Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell10, workbook));