Repository: wangguanquan/eec
Pull request #288 (2022-08-28 00:12:38)

Fix#287


Patch:
diff --git a/src/main/java/org/ttzero/excel/annotation/HeaderComment.java b/src/main/java/org/ttzero/excel/annotation/HeaderComment.java
index dcaebf73..d38f3a79 100644
--- a/src/main/java/org/ttzero/excel/annotation/HeaderComment.java
+++ b/src/main/java/org/ttzero/excel/annotation/HeaderComment.java
@@ -44,5 +44,17 @@ public @interface HeaderComment {
      */
     String title() default "";
 
+    /**
+     * Specify the comment width
+     *
+     * @return comment width
+     */
+    double width() default 100.8D;
 
+    /**
+     * Specify the comment height
+     *
+     * @return comment height
+     */
+    double height() default 60.6D;
 }
diff --git a/src/main/java/org/ttzero/excel/entity/Comment.java b/src/main/java/org/ttzero/excel/entity/Comment.java
index 39d40ad6..2d78dd7a 100644
--- a/src/main/java/org/ttzero/excel/entity/Comment.java
+++ b/src/main/java/org/ttzero/excel/entity/Comment.java
@@ -23,12 +23,21 @@ package org.ttzero.excel.entity;
 public class Comment {
     private String title;
     private String value;
+    private Double width, height;
 
-    public Comment (String title, String value) {
+    public Comment() { }
+    public Comment(String title, String value) {
         this.title = title;
         this.value = value;
     }
 
+    public Comment(String title, String value, Double width, Double height) {
+        this.title = title;
+        this.value = value;
+        this.width = width;
+        this.height = height;
+    }
+
     public String getTitle() {
         return title;
     }
@@ -44,4 +53,20 @@ public class Comment {
     public void setValue(String value) {
         this.value = value;
     }
+
+    public Double getWidth() {
+        return width;
+    }
+
+    public void setWidth(Double width) {
+        this.width = width;
+    }
+
+    public Double getHeight() {
+        return height;
+    }
+
+    public void setHeight(Double height) {
+        this.height = height;
+    }
 }
diff --git a/src/main/java/org/ttzero/excel/entity/Comments.java b/src/main/java/org/ttzero/excel/entity/Comments.java
index faf097a0..ad8235c7 100644
--- a/src/main/java/org/ttzero/excel/entity/Comments.java
+++ b/src/main/java/org/ttzero/excel/entity/Comments.java
@@ -42,18 +42,18 @@ import static org.ttzero.excel.util.StringUtil.isNotEmpty;
 public class Comments implements Storable, Closeable {
 
     /** Comments Cache*/
-    private final List<C> commentList;
-    private final int id;
-    private final String author;
-//    private final static int CACHE_SIZE = 20;
+    public List<C> commentList = new ArrayList<>();
+    public int id;
+    public String author;
 
-    Comments(int id, String author) {
+    public Comments() { }
+
+    public Comments(int id, String author) {
         this.id = id;
         this.author = author;
-        commentList = new ArrayList<>();
     }
 
-    public void addComment(String ref, String title, String value) {
+    public C addComment(String ref, String title, String value) {
         C c = new C();
         c.ref = ref;
         c.text = new ArrayList<>();
@@ -66,12 +66,28 @@ public class Comments implements Storable, Closeable {
 
         commentList.add(c);
 
-//        if (commentList.size() >= CACHE_SIZE) {
-//            flush();
-//        }
+        return c;
+    }
+
+    public C addComment(String ref, Comment comment) {
+        C c = new C();
+        c.ref = ref;
+        c.text = new ArrayList<>();
+        c.width = comment.getWidth();
+        c.height = comment.getHeight();
+        if (isNotEmpty(comment.getTitle())) {
+            parse(comment.getTitle(), true, c.text);
+        }
+        if (isNotEmpty(comment.getValue())) {
+            parse(comment.getValue(), false, c.text);
+        }
+
+        commentList.add(c);
+
+        return c;
     }
 
-    private void parse(String val, boolean bold, List<R> list) {
+    protected void parse(String val, boolean bold, List<R> list) {
         // a simple implement
         R r = new R();
         r.rPr = new Pr("宋体", 9);
@@ -132,7 +148,7 @@ public class Comments implements Storable, Closeable {
         vml(root);
     }
 
-    private void vml(Path root) throws IOException {
+    protected void vml(Path root) throws IOException {
         Path parent = root.resolve("drawings");
         if (!Files.exists(parent)) {
             FileUtil.mkdir(parent);
@@ -155,7 +171,7 @@ public class Comments implements Storable, Closeable {
             for (C c : commentList) {
                 long cr = ExcelReader.cellRangeToLong(c.ref);
                 writer.write(" <v:shape id=\"_x0000_s");writer.write(100 + i);
-                writer.write("\" type=\"#_x0000_t202\" style='width:100.8pt;height:60.6pt;z-index:");
+                writer.write("\" type=\"#_x0000_t202\" style='width:" + (c.width != null ? c.width : 100.8D) + "pt;height:" + (c.height != null ? c.height : 60.6D) + " pt;z-index:");
                 writer.writeInt(i++);
                 writer.write(";  visibility:hidden' fillcolor=\"#ffffe1\" o:insetmode=\"auto\">");
                 writer.write("  <v:fill color2=\"#ffffe1\"/>");
@@ -178,9 +194,10 @@ public class Comments implements Storable, Closeable {
         }
     }
 
-    private static class C {
-        private String ref;
-        private List<R> text;
+    public static class C {
+        public String ref;
+        public List<R> text;
+        public Double width, height;
 
         @Override
         public String toString() {
@@ -193,9 +210,9 @@ public class Comments implements Storable, Closeable {
         }
     }
 
-    private static class R {
-        private Pr rPr;
-        private String t;
+    public static class R {
+        public Pr rPr;
+        public String t;
 
         @Override
         public String toString() {
@@ -204,9 +221,9 @@ public class Comments implements Storable, Closeable {
         }
     }
 
-    private static class Pr extends Font {
-        private static final String[] STYLE = {"", "<u/>", "<b/>", "<u/><b/>", "<i/>", "<i/><u/>", "<b/><i/>", "<i/><b/><u/>"};
-        Pr(String name, int size) {
+    public static class Pr extends Font {
+        public static final String[] STYLE = {"", "<u/>", "<b/>", "<u/><b/>", "<i/>", "<i/><u/>", "<b/><i/>", "<i/><b/><u/>"};
+        public Pr(String name, int size) {
             super(name, size);
         }
 
diff --git a/src/main/java/org/ttzero/excel/entity/ListSheet.java b/src/main/java/org/ttzero/excel/entity/ListSheet.java
index 2c295955..da74d099 100644
--- a/src/main/java/org/ttzero/excel/entity/ListSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListSheet.java
@@ -611,8 +611,6 @@ public class ListSheet<T> extends Sheet {
             if (sp != null) {
                 column.styleProcessor = sp;
             }
-            // Cell max width
-            column.width = ec.maxWidth();
             return column;
         }
         return null;
@@ -637,11 +635,8 @@ public class ListSheet<T> extends Sheet {
         if (ec.colIndex() > -1) {
             column.colIndex = ec.colIndex();
         }
-        // Comment
-        HeaderComment comment = ec.comment();
-        if ((isNotEmpty(comment.value()) || isNotEmpty(comment.title()))) {
-            column.headerComment = new Comment(comment.title(), comment.value());
-        }
+        // Cell max width
+        column.width = ec.maxWidth();
         return column;
     }
 
@@ -689,7 +684,7 @@ public class ListSheet<T> extends Sheet {
             }
         }
         if (comment != null && (isNotEmpty(comment.value()) || isNotEmpty(comment.title()))) {
-            column.headerComment = new Comment(comment.title(), comment.value());
+            column.headerComment = new Comment(comment.title(), comment.value(), comment.width(), comment.height());
         }
     }
 
diff --git a/src/main/java/org/ttzero/excel/entity/Sheet.java b/src/main/java/org/ttzero/excel/entity/Sheet.java
index 9cbee1c8..d642fb04 100644
--- a/src/main/java/org/ttzero/excel/entity/Sheet.java
+++ b/src/main/java/org/ttzero/excel/entity/Sheet.java
@@ -533,6 +533,9 @@ public abstract class Sheet implements Cloneable, Storable {
      * @return Columns instance if exists
      */
     public Comments getComments() {
+        if (comments != null && comments.id == 0) {
+            comments.id = this.id;
+        }
         return comments;
     }
 
@@ -543,11 +546,13 @@ public abstract class Sheet implements Cloneable, Storable {
      */
     public Comments createComments() {
         if (comments == null) {
-            comments = new Comments(id, workbook.getCreator());
+            comments = workbook != null ? new Comments(id, workbook.getCreator()) : new Comments();
             // FIXME Removed at excel version 2013
-            addRel(new Relationship("../drawings/vmlDrawing" + id + Const.Suffix.VML, Const.Relationship.VMLDRAWING));
+            if (id > 0) {
+                addRel(new Relationship("../drawings/vmlDrawing" + id + Const.Suffix.VML, Const.Relationship.VMLDRAWING));
 
-            addRel(new Relationship("../comments" + id + Const.Suffix.XML, Const.Relationship.COMMENTS));
+                addRel(new Relationship("../comments" + id + Const.Suffix.XML, Const.Relationship.COMMENTS));
+            }
         }
         return comments;
     }
diff --git a/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java b/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java
index 35abad51..98ab93b3 100644
--- a/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java
+++ b/src/main/java/org/ttzero/excel/entity/e7/XMLWorksheetWriter.java
@@ -369,8 +369,7 @@ public class XMLWorksheetWriter implements IWorksheetWriter {
                 Column hc = columnsArray[j][i];
                 if (hc.headerComment != null) {
                     if (comments == null) comments = sheet.createComments();
-                    comments.addComment(new String(int2Col(hc.getRealColIndex())) + row
-                        , hc.headerComment.getTitle(), hc.headerComment.getValue());
+                    comments.addComment(new String(int2Col(hc.getRealColIndex())) + row, hc.headerComment);
                 }
             }
             bw.write("</row>");
@@ -1100,6 +1099,15 @@ public class XMLWorksheetWriter implements IWorksheetWriter {
             bw.write(r.getId());
             bw.write("\"/>");
         }
+        // Compatible processing
+        else if (comments != null) {
+            sheet.addRel(r = new Relationship("../drawings/vmlDrawing" + sheet.getId() + Const.Suffix.VML, Const.Relationship.VMLDRAWING));
+            sheet.addRel(new Relationship("../comments" + sheet.getId() + Const.Suffix.XML, Const.Relationship.COMMENTS));
+
+            bw.write("<legacyDrawing r:id=\"");
+            bw.write(r.getId());
+            bw.write("\"/>");
+        }
 
         // background image
         if (sheet.getWaterMark() != null) {
diff --git a/src/main/java/org/ttzero/excel/manager/RelManager.java b/src/main/java/org/ttzero/excel/manager/RelManager.java
index 8ae6c4df..b6e77b52 100644
--- a/src/main/java/org/ttzero/excel/manager/RelManager.java
+++ b/src/main/java/org/ttzero/excel/manager/RelManager.java
@@ -109,6 +109,15 @@ public class RelManager implements Serializable {
         return null;
     }
 
+    public Relationship getByType(String type) {
+        if (relationships == null || relationships.isEmpty() || StringUtil.isEmpty(type))
+            return null;
+        for (Relationship rel : relationships) {
+            if (type.equals(rel.getType())) return rel;
+        }
+        return null;
+    }
+
     public void write(Path parent, String name) throws IOException {
         if (relationships == null || relationships.isEmpty()) {
             return;


Test patch:
diff --git a/src/test/java/org/ttzero/excel/entity/CommentTest.java b/src/test/java/org/ttzero/excel/entity/CommentTest.java
index 5a5024ad..50614024 100644
--- a/src/test/java/org/ttzero/excel/entity/CommentTest.java
+++ b/src/test/java/org/ttzero/excel/entity/CommentTest.java
@@ -36,6 +36,19 @@ public class CommentTest extends WorkbookTest {
             .writeTo(defaultTestPath);
     }
 
+    @Test public void testCommentLongText() throws IOException {
+        Sheet sheet = new ListSheet<>(Student.randomTestData());
+        Comments comments = sheet.getComments();
+        if (comments == null) comments = sheet.createComments();
+        comments.addComment("C5", new Comment("提示：", "1、第一行批注内容较多时无法完全显示内容，增加弹出框大小设置\n" +
+            "2、第二行批注内容较多时无法完全显示内容\n" +
+            "3、第三行批注内容较多时无法完全显示内容\n" +
+            "4、第四行批注内容较多时无法完全显示内容", 180D, 80D));
+        new Workbook("long text comment test")
+            .addSheet(sheet)
+            .writeTo(defaultTestPath);
+    }
+
     /**
      * Annotation Object
      */