Browse Source

* 修复列宽注解没用的bug [Issue #2151]

pull/2159/head
Jiaju Zhuang 3 years ago
parent
commit
6366552137
  1. 7
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  2. 11
      src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java
  3. 8
      src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java
  4. 19
      src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java
  5. 4
      src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java

7
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -243,15 +243,11 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
} }
Map<Integer, Head> headMap = getExcelWriteHeadProperty().getHeadMap(); Map<Integer, Head> headMap = getExcelWriteHeadProperty().getHeadMap();
boolean hasColumnWidth = false; boolean hasColumnWidth = false;
boolean hasStyle = false;
for (Head head : headMap.values()) { for (Head head : headMap.values()) {
if (head.getColumnWidthProperty() != null) { if (head.getColumnWidthProperty() != null) {
hasColumnWidth = true; hasColumnWidth = true;
} }
if (head.getHeadStyleProperty() != null || head.getHeadFontProperty() != null) {
hasStyle = true;
}
dealLoopMerge(handlerList, head); dealLoopMerge(handlerList, head);
} }
@ -259,10 +255,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
dealColumnWidth(handlerList); dealColumnWidth(handlerList);
} }
//if (hasStyle) {
dealStyle(handlerList); dealStyle(handlerList);
//}
dealRowHigh(handlerList); dealRowHigh(handlerList);
dealOnceAbsoluteMerge(handlerList); dealOnceAbsoluteMerge(handlerList);
} }

11
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.Map;
import java.util.Set; 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.ContentLoopMerge;
import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadFontStyle; 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.CellRange;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.Holder; 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.ExcelHeadProperty;
import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.FontProperty;
import com.alibaba.excel.metadata.property.LoopMergeProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty;
@ -50,9 +52,11 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty {
this.onceAbsoluteMergeProperty = this.onceAbsoluteMergeProperty =
OnceAbsoluteMergeProperty.build(headClazz.getAnnotation(OnceAbsoluteMerge.class)); OnceAbsoluteMergeProperty.build(headClazz.getAnnotation(OnceAbsoluteMerge.class));
ColumnWidth parentColumnWidth = headClazz.getAnnotation(ColumnWidth.class);
HeadStyle parentHeadStyle = headClazz.getAnnotation(HeadStyle.class); HeadStyle parentHeadStyle = headClazz.getAnnotation(HeadStyle.class);
HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class); HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class);
for (Map.Entry<Integer, Head> entry : getHeadMap().entrySet()) { for (Map.Entry<Integer, Head> entry : getHeadMap().entrySet()) {
Head headData = entry.getValue(); Head headData = entry.getValue();
if (headData == null) { if (headData == null) {
@ -61,6 +65,13 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty {
} }
Field field = headData.getField(); 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); HeadStyle headStyle = field.getAnnotation(HeadStyle.class);
if (headStyle == null) { if (headStyle == null) {
headStyle = parentHeadStyle; headStyle = parentHeadStyle;

8
src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java

@ -16,16 +16,18 @@ import lombok.Data;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@Data @Data
@ColumnWidth(30) @ColumnWidth(50)
@HeadRowHeight(15) @HeadRowHeight(50)
@ContentRowHeight(20) @ContentRowHeight(100)
public class AnnotationData { public class AnnotationData {
@ExcelProperty("日期") @ExcelProperty("日期")
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
private Date date; private Date date;
@ExcelProperty(value = "数字") @ExcelProperty(value = "数字")
@NumberFormat("#.##%") @NumberFormat("#.##%")
private Double number; private Double number;
@ExcelIgnore @ExcelIgnore
private String ignore; private String ignore;
private static final String staticFinal = "test"; private static final String staticFinal = "test";

19
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.EasyExcel;
import com.alibaba.excel.util.DateUtils; 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.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -46,6 +51,20 @@ public class AnnotationDataTest {
EasyExcel.write().file(file).head(AnnotationData.class).sheet().doWrite(data()); EasyExcel.write().file(file).head(AnnotationData.class).sheet().doWrite(data());
EasyExcel.read().file(file).head(AnnotationData.class).registerReadListener(new AnnotationDataListener()) EasyExcel.read().file(file).head(AnnotationData.class).registerReadListener(new AnnotationDataListener())
.sheet().doRead(); .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<AnnotationData> data() throws Exception { private List<AnnotationData> data() throws Exception {

4
src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java

@ -209,7 +209,10 @@ public class StyleDataTest {
Workbook workbook = WorkbookFactory.create(file); Workbook workbook = WorkbookFactory.create(file);
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0);
Row row0 = sheet.getRow(0); Row row0 = sheet.getRow(0);
Assert.assertEquals(800, row0.getHeight(), 0);
Cell cell00 = row0.getCell(0); Cell cell00 = row0.getCell(0);
Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell00)); Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell00));
Assert.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell00, workbook)); 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)); Assert.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell01, workbook));
Row row1 = sheet.getRow(1); Row row1 = sheet.getRow(1);
Assert.assertEquals(1000, row1.getHeight(), 0);
Cell cell10 = row1.getCell(0); Cell cell10 = row1.getCell(0);
Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell10)); Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell10));
Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell10, workbook)); Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell10, workbook));

Loading…
Cancel
Save