featzhang commented on code in PR #7793:
URL: https://github.com/apache/inlong/pull/7793#discussion_r1160445273


##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelCellDataTransfer.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.common.tool.excel;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public enum ExcelCellDataTransfer {
+
+    DATE {
+
+        final SimpleDateFormat simpleDateFormat = new 
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+        String parse2Text(Object o) {
+            if (o == null) {
+                return null;
+            } else if (o instanceof Date) {
+                Date date = (Date) o;
+                return this.simpleDateFormat.format(date);
+            } else {
+                return String.valueOf(o);
+            }
+        }
+
+        Object parseFromText(String so) {
+            if (so != null && so.length() > 0) {
+                String s = so;

Review Comment:
   Fixed



##########
inlong-manager/manager-common/src/main/java/org/apache/inlong/manager/common/tool/excel/ExcelTool.java:
##########
@@ -0,0 +1,351 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.common.tool.excel;
+
+import lombok.Data;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.commons.lang3.tuple.Triple;
+import org.apache.inlong.manager.common.tool.excel.annotation.ExcelEntity;
+import org.apache.inlong.manager.common.tool.excel.annotation.ExcelField;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.apache.inlong.manager.common.util.Preconditions.expectTrue;
+
+/**
+ * Utility class for working with Excel files.
+ */
+public class ExcelTool {
+
+    private ExcelTool() {
+
+    }
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(ExcelTool.class);
+    private static final int DEFAULT_COLUMN_WIDTH = 10000;
+
+    /**
+     * Extracts the header row from a given class and returns it as a list of 
strings.
+     */
+    public static <E> List<String> extractHeader(Class<E> e1Class) {
+        List<String> list = new LinkedList<>();
+        Field[] fields = e1Class.getDeclaredFields();
+        if (fields.length > 0) {
+            for (Field field : fields) {
+                field.setAccessible(true);
+                ExcelField excel = field.getAnnotation(ExcelField.class);
+                if (excel != null) {
+                    String excelName = excel.name();
+                    list.add(excelName);
+                }
+            }
+            return !list.isEmpty() ? list : Collections.emptyList();
+        } else {
+            return Collections.emptyList();
+        }
+    }
+
+    /**
+     * Writes the given content to an Excel document.
+     *
+     * @param contents the list of contents to write
+     * @param out      the output stream to write to
+     * @throws IOException if there is an error writing to the output stream
+     */
+    public static <T> void write(List<T> contents, OutputStream out) throws 
IOException {
+        Preconditions.expectNotEmpty(contents, "Content can not be empty!");
+        Class<?> clazz = contents.get(0).getClass();
+        List<Map<String, String>> maps = write2List(contents);
+        doWrite(maps, clazz, out);
+    }
+
+    public static <T> void doWrite(List<Map<String, String>> maps, Class<T> 
clazz, OutputStream out)
+            throws IOException {
+        List<String> heads = extractHeader(clazz);
+        if (heads.isEmpty()) {
+            throw new IllegalArgumentException(
+                    "At least one field must be marked as Excel Field by 
annotation @ExcelField in class " + clazz);
+        }
+        XSSFWorkbook hwb = new XSSFWorkbook();
+        XSSFSheet sheet = hwb.createSheet("Sheet 1");
+
+        for (int index = 0; index < heads.size(); index++) {
+            sheet.setColumnWidth(index, DEFAULT_COLUMN_WIDTH);
+        }
+        fillSheetHeader(sheet.createRow(0), heads);
+        //

Review Comment:
   Fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to