스타일 관련 수정

master
mjkhan21 6 months ago
parent a6f2689cc1
commit f1078395b0

@ -134,6 +134,7 @@ public class CellDef {
*/ */
public CellDef setWidth(int width) { public CellDef setWidth(int width) {
this.width = width; this.width = width;
setWidth();
return this; return this;
} }
@ -150,6 +151,12 @@ public class CellDef {
*/ */
public CellDef setValue(Object value) { public CellDef setValue(Object value) {
this.value = value; this.value = value;
setWidth();
return this; return this;
} }
private void setWidth() {
if (value instanceof Style style)
style.width(width);
}
} }

@ -51,6 +51,10 @@ public class Style {
private Font font; private Font font;
private String dataFormat; private String dataFormat;
public int width() {
return width != null ? width : 0;
}
/** . /** .
* @param chars . * @param chars .
* @return Style * @return Style
@ -60,6 +64,10 @@ public class Style {
return this; return this;
} }
public short height() {
return height != null ? height : 0;
}
/** . /** .
* @param height * @param height
* @return Style * @return Style

@ -5,6 +5,7 @@ import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -275,6 +276,16 @@ public class XLSWriter extends XLS {
if (style.dataFormat() != null) { if (style.dataFormat() != null) {
cellStyle.setDataFormat(workbook.createDataFormat().getFormat(style.dataFormat())); 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; return cellStyle;
} }
@ -298,7 +309,7 @@ public class XLSWriter extends XLS {
*/ */
public XLSWriter trackWidth(int... cols) { public XLSWriter trackWidth(int... cols) {
if (!isEmpty(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<Integer>::new)));
return this; return this;
} }
@ -342,6 +353,8 @@ public class XLSWriter extends XLS {
@Override @Override
protected void clear(boolean all) { protected void clear(boolean all) {
if (trackWidths != null)
worksheet.untrackColumnsForAutoSizing(trackWidths);
super.clear(all); super.clear(all);
trackWidths = null; trackWidths = null;
} }

Loading…
Cancel
Save