You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by do...@apache.org on 2023/04/13 03:25:31 UTC

[inlong] branch master updated: [INLONG-7841][Manager] Support header style and font when exporting Excel file (#7842)

This is an automated email from the ASF dual-hosted git repository.

dockerzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git


The following commit(s) were added to refs/heads/master by this push:
     new 0b13d3b33 [INLONG-7841][Manager] Support header style and font when exporting Excel file (#7842)
0b13d3b33 is described below

commit 0b13d3b33d176bb00aa811ac448bfad4ea8faee3
Author: feat <fe...@outlook.com>
AuthorDate: Thu Apr 13 11:25:25 2023 +0800

    [INLONG-7841][Manager] Support header style and font when exporting Excel file (#7842)
---
 .../inlong/manager/common/tool/excel/ExcelTool.java   | 19 +++++++++++++------
 .../common/tool/excel/annotation/ExcelField.java      |  4 ++++
 .../inlong/manager/pojo/stream/StreamField.java       | 10 +++-------
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java
index e8173488f..eb518fe7e 100644
--- a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java
+++ b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java
@@ -60,6 +60,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 import static org.apache.inlong.manager.common.util.Preconditions.expectTrue;
@@ -123,11 +124,14 @@ public class ExcelTool {
                 sheet.setColumnWidth(index, fieldMeta.getRight().style().width());
             }
             // Fill header with cellStyle
-            fillSheetHeader(sheet.createRow(0), headNames);
+            List<XSSFCellStyle> headerStyles =
+                    createContentCellStyle(hwb, fieldMetas, ExcelField::headerStyle, ExcelField::headerFont);
+            fillSheetHeader(sheet.createRow(0), headNames, headerStyles);
             // Fill validation
             fillSheetValidation(sheet, fieldMetas, clazz.getCanonicalName());
             // Fill content if data exist.
-            List<XSSFCellStyle> contentStyles = createContentCellStyle(hwb, fieldMetas);
+            List<XSSFCellStyle> contentStyles =
+                    createContentCellStyle(hwb, fieldMetas, ExcelField::style, ExcelField::font);
             if (CollectionUtils.isNotEmpty(maps)) {
                 fillSheetContent(sheet, headNames, maps, contentStyles);
             } else {
@@ -156,12 +160,14 @@ public class ExcelTool {
     }
 
     private static List<XSSFCellStyle> createContentCellStyle(XSSFWorkbook workbook,
-            List<Pair<Field, ExcelField>> fieldMetas) {
+            List<Pair<Field, ExcelField>> fieldMetas,
+            Function<ExcelField, Style> styleGenerator,
+            Function<ExcelField, Font> fontGenerator) {
         return fieldMetas.stream().map(fieldMeta -> {
             XSSFCellStyle style = workbook.createCellStyle();
             ExcelField excelField = fieldMeta.getRight();
-            Style excelStyle = excelField.style();
-            Font excelFont = excelField.font();
+            Style excelStyle = styleGenerator.apply(excelField);
+            Font excelFont = fontGenerator.apply(excelField);
             // Set foreground color
             style.setFillForegroundColor(excelStyle.bgColor().getIndex());
             style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
@@ -238,11 +244,12 @@ public class ExcelTool {
                 });
     }
 
-    private static void fillSheetHeader(XSSFRow row, List<String> heads) {
+    private static void fillSheetHeader(XSSFRow row, List<String> heads, List<XSSFCellStyle> headerStyles) {
         int headSize = heads.size();
         for (int index = 0; index < headSize; index++) {
             XSSFCell cell = row.createCell(index);
             cell.setCellValue(new XSSFRichTextString(heads.get(index)));
+            cell.setCellStyle(headerStyles.get(index));
         }
     }
 
diff --git a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/annotation/ExcelField.java b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/annotation/ExcelField.java
index 4a7f1375a..ae30251da 100644
--- a/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/annotation/ExcelField.java
+++ b/inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/annotation/ExcelField.java
@@ -49,4 +49,8 @@ public @interface ExcelField {
     Font font() default @Font;
 
     Style style() default @Style;
+
+    Font headerFont() default @Font;
+
+    Style headerStyle() default @Style;
 }
diff --git a/inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/stream/StreamField.java b/inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/stream/StreamField.java
index fffe2a213..3af7560ae 100644
--- a/inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/stream/StreamField.java
+++ b/inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/stream/StreamField.java
@@ -53,19 +53,15 @@ public class StreamField implements Serializable {
     @ApiModelProperty(value = "inlong stream id", required = true)
     private String inlongStreamId;
 
-    @ExcelField(name = "Field name", validator = NonEmptyCellValidator.class,
-
-            font = @Font(size = 16), style = @Style(bgColor = IndexedColors.LIGHT_TURQUOISE, width = 9000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN))
+    @ExcelField(name = "Field name", validator = NonEmptyCellValidator.class, font = @Font(size = 16), style = @Style(bgColor = IndexedColors.LIGHT_TURQUOISE, width = 9000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN), headerFont = @Font(size = 20, color = IndexedColors.WHITE), headerStyle = @Style(bgColor = IndexedColors.DARK_BLUE, width = 9000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN))
     @ApiModelProperty(value = "Field name", required = true)
     private String fieldName;
 
-    @ExcelField(name = "Field type", validator = StreamFieldTypeCellValidator.class,
-
-            font = @Font(size = 16), style = @Style(bgColor = IndexedColors.LIGHT_TURQUOISE, width = 6000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN))
+    @ExcelField(name = "Field type", validator = StreamFieldTypeCellValidator.class, font = @Font(size = 16), style = @Style(bgColor = IndexedColors.LIGHT_TURQUOISE, width = 6000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN), headerFont = @Font(size = 20, color = IndexedColors.WHITE), headerStyle = @Style(bgColor = IndexedColors.DARK_BLUE, width = 9000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN))
     @ApiModelProperty(value = "Field type", required = true)
     private String fieldType;
 
-    @ExcelField(name = "Field comment", font = @Font(size = 16), style = @Style(bgColor = IndexedColors.LIGHT_TURQUOISE, width = 10000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN))
+    @ExcelField(name = "Field comment", font = @Font(size = 16), style = @Style(bgColor = IndexedColors.LIGHT_TURQUOISE, width = 10000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN), headerFont = @Font(size = 20, color = IndexedColors.WHITE), headerStyle = @Style(bgColor = IndexedColors.DARK_BLUE, width = 9000, allBorderColor = IndexedColors.BLUE, allBorderStyle = BorderStyle.THIN))
     @ApiModelProperty("Field comment")
     private String fieldComment;