From f1078395b091372f3446d39dab18484177ee6355 Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Fri, 10 May 2024 10:07:10 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8A=A4=ED=83=80=EC=9D=BC=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/cokr/xit/base/docs/xls/CellDef.java | 7 +++++++ src/main/java/cokr/xit/base/docs/xls/Style.java | 8 ++++++++ .../java/cokr/xit/base/docs/xls/XLSWriter.java | 15 ++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/cokr/xit/base/docs/xls/CellDef.java b/src/main/java/cokr/xit/base/docs/xls/CellDef.java index 3955fc3..31c502f 100644 --- a/src/main/java/cokr/xit/base/docs/xls/CellDef.java +++ b/src/main/java/cokr/xit/base/docs/xls/CellDef.java @@ -134,6 +134,7 @@ public class CellDef { */ public CellDef setWidth(int width) { this.width = width; + setWidth(); return this; } @@ -150,6 +151,12 @@ public class CellDef { */ public CellDef setValue(Object value) { this.value = value; + setWidth(); return this; } + + private void setWidth() { + if (value instanceof Style style) + style.width(width); + } } \ No newline at end of file diff --git a/src/main/java/cokr/xit/base/docs/xls/Style.java b/src/main/java/cokr/xit/base/docs/xls/Style.java index ab4e740..c31d773 100644 --- a/src/main/java/cokr/xit/base/docs/xls/Style.java +++ b/src/main/java/cokr/xit/base/docs/xls/Style.java @@ -51,6 +51,10 @@ public class Style { private Font font; private String dataFormat; + public int width() { + return width != null ? width : 0; + } + /**열의 폭을 설정한다. * @param chars 열의 폭. 한번에 보여지는 문자수 * @return 현재 Style @@ -60,6 +64,10 @@ public class Style { return this; } + public short height() { + return height != null ? height : 0; + } + /**행의 높이를 설정한다. * @param height 행의 높이 * @return 현재 Style diff --git a/src/main/java/cokr/xit/base/docs/xls/XLSWriter.java b/src/main/java/cokr/xit/base/docs/xls/XLSWriter.java index b4f5016..c223b4f 100644 --- a/src/main/java/cokr/xit/base/docs/xls/XLSWriter.java +++ b/src/main/java/cokr/xit/base/docs/xls/XLSWriter.java @@ -5,6 +5,7 @@ import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Collections; import java.util.Date; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -275,6 +276,16 @@ public class XLSWriter extends XLS { if (style.dataFormat() != null) { cellStyle.setDataFormat(workbook.createDataFormat().getFormat(style.dataFormat())); } + + int width = style.width(); + if (width > 0) { + int column = cell.getColumnIndex(); + worksheet.setColumnWidth(column, width * 256); + } + + short height = style.height(); + if (height > 0) + row.setHeight(height); } return cellStyle; } @@ -298,7 +309,7 @@ public class XLSWriter extends XLS { */ public XLSWriter trackWidth(int... cols) { if (!isEmpty(cols)) - worksheet.trackColumnsForAutoSizing(trackWidths = IntStream.of(cols).boxed().collect(Collectors.toSet())); + worksheet.trackColumnsForAutoSizing(trackWidths = IntStream.of(cols).boxed().collect(Collectors.toCollection(LinkedHashSet::new))); return this; } @@ -342,6 +353,8 @@ public class XLSWriter extends XLS { @Override protected void clear(boolean all) { + if (trackWidths != null) + worksheet.untrackColumnsForAutoSizing(trackWidths); super.clear(all); trackWidths = null; }