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));