XLSWriter 스타일링, 포맷팅 수정

master
mjkhan21 11 months ago
parent 815ac2676e
commit 92cbb0f8b3

@ -4,6 +4,8 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Map;
import java.util.function.Function;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
@ -64,4 +66,62 @@ public abstract class XLS extends AbstractComponent {
* @param out OutputStream * @param out OutputStream
*/ */
public abstract void write(OutputStream out); public abstract void write(OutputStream out);
/**yyyyMMdd, yyMMdd yyyy-MM-dd .
* @param obj yyyyMMdd, yyMMdd
* @return yyyy-MM-dd
*/
public final String str2date(Object obj) {
if (isEmpty(obj)) return "";
String str = obj.toString();
int length = str.length(),
pos = length - 2;
String
day = str.substring(pos),
month = str.substring(pos = pos - 2, pos + 2),
year = str.substring(0, pos);
return String.format("%s-%s-%s", year, month, day);
}
/**HHmmss HH:mm:ss .
* @param obj HHmmss
* @return HH:mm:ss
*/
public final String str2time(Object obj) {
if (isEmpty(obj)) return "";
String str = obj.toString();
int length = str.length(),
pos = length - 2;
String
ss = str.substring(pos),
mm = str.substring(pos = pos - 2, pos + 2),
hh = str.substring(0, pos);
return String.format("%s:%s:%s", hh, mm, ss);
}
/**yyyyMMddHHmmss, yyMMddHHmmss yyyy-MM-dd HH:mm:ss .
* @param obj yyyyMMddHHmmss, yyMMddHHmmss
* @return yyyy-MM-dd HH:mm:ss
*/
public final String str2datetime(Object obj) {
if (isEmpty(obj)) return "";
String str = obj.toString();
int length = str.length(),
pos = length - 6;
String
date = str.substring(0, pos),
time = str.substring(pos);
return str2date(date) + " " + str2time(time);
}
public final Function<Map<?, ?>, Object> wrap(Function<Map<?, ?>, Object> func) {
return func;
}
} }

@ -7,6 +7,7 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
@ -38,11 +39,11 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
* dataset . * dataset .
* dataset . * dataset .
* <pre><code> xlsx.values(dataset, "RCPT_ID", "RCPT_DT", "PAYER_NM", "RCPT_AMT");</code></pre> * <pre><code> xlsx.values(dataset, "RCPT_ID", "RCPT_DT", "PAYER_NM", "RCPT_AMT");</code></pre>
* dataset {@link KeyStyle} . * dataset {@link Formatter} .
* <pre><code> CellStyle * <pre><code> CellStyle
* datetime = xlsx.yyyy_mm_dd_hh_mm_ss(), * datetime = xlsx.yyyy_mm_dd_hh_mm_ss(),
* numeric = xlsx.n_nn0(); * numeric = xlsx.n_nn0();
* xlsx.values(dataset, "RCPT_ID", xlsx.keyStyle("RCPT_DT", datetime), "PAYER_NM", xlsx.keyStyle("RCPT_AMT", numeric));</code></pre> * xlsx.values(dataset, "RCPT_ID", xlsx.style("RCPT_DT", datetime), "PAYER_NM", xlsx.style("RCPT_AMT", numeric));</code></pre>
* @author mjkhan * @author mjkhan
*/ */
public class XLSWriter extends XLS { public class XLSWriter extends XLS {
@ -76,7 +77,7 @@ public class XLSWriter extends XLS {
} }
} }
private SXSSFWorkbook getWorkbook() { public SXSSFWorkbook workbook() {
if (workbook != null) if (workbook != null)
return workbook; return workbook;
@ -102,9 +103,9 @@ public class XLSWriter extends XLS {
*/ */
public XLSWriter worksheet(int index) { public XLSWriter worksheet(int index) {
try { try {
worksheet = getWorkbook().getSheetAt(index); worksheet = workbook().getSheetAt(index);
} catch (Exception e) { } catch (Exception e) {
worksheet = getWorkbook().createSheet(); worksheet = workbook().createSheet();
} }
worksheet.setRandomAccessWindowSize(rowAccessWindowSize); worksheet.setRandomAccessWindowSize(rowAccessWindowSize);
return this; return this;
@ -117,7 +118,7 @@ public class XLSWriter extends XLS {
*/ */
public XLSWriter worksheet(String name) { public XLSWriter worksheet(String name) {
try { try {
SXSSFWorkbook wb = getWorkbook(); SXSSFWorkbook wb = workbook();
worksheet = ifEmpty(wb.getSheet(name), () -> wb.createSheet(name)); worksheet = ifEmpty(wb.getSheet(name), () -> wb.createSheet(name));
worksheet.setRandomAccessWindowSize(rowAccessWindowSize); worksheet.setRandomAccessWindowSize(rowAccessWindowSize);
return this; return this;
@ -216,7 +217,7 @@ public class XLSWriter extends XLS {
setCellStyle(val); setCellStyle(val);
} else if (val instanceof CellStyle) { } else if (val instanceof CellStyle) {
setCellStyle(val); setCellStyle(val);
} else if (val instanceof KeyStyle) { } else if (val instanceof Formatter) {
setCellStyle(val); setCellStyle(val);
} else { } else {
col(x).value(val); col(x).value(val);
@ -236,10 +237,10 @@ public class XLSWriter extends XLS {
style = (CellStyle)obj; style = (CellStyle)obj;
} else if (obj instanceof Styler) { } else if (obj instanceof Styler) {
styler = (Styler)obj; styler = (Styler)obj;
} else if (obj instanceof KeyStyle) { } else if (obj instanceof Formatter) {
KeyStyle keyStyle = (KeyStyle)obj; Formatter formatter = (Formatter)obj;
styler = keyStyle.styler; styler = formatter.styler;
style = keyStyle.style; style = formatter.style;
} }
if (styler != null) { if (styler != null) {
@ -266,11 +267,23 @@ public class XLSWriter extends XLS {
ArrayList<Object> vals = new ArrayList<>(); ArrayList<Object> vals = new ArrayList<>();
for (Object obj: keysAndStyles) { for (Object obj: keysAndStyles) {
if (obj instanceof KeyStyle) { if (obj instanceof Formatter) {
KeyStyle keyStyle = (KeyStyle)obj; Formatter formatter = (Formatter)obj;
Object val = map.get(keyStyle.key); Object val = null;
if (formatter.key != null) {
val = map.get(formatter.key);
} else if (formatter.convert != null){
val = formatter.convert.apply(map);
}
vals.add(val); vals.add(val);
vals.add(keyStyle);
Styler styler = formatter.styler;
CellStyle style = formatter.style;
if (styler != null)
style = cellStyle(styler);
if (style != null)
vals.add(style);
} else { } else {
vals.add(map.get(obj)); vals.add(map.get(obj));
} }
@ -280,7 +293,7 @@ public class XLSWriter extends XLS {
/** map .<br /> /** map .<br />
* .<br /> * .<br />
* {@link KeyStyle} .<br /> * {@link Formatter} .<br />
* map . * map .
* <pre><code> DataObject rec = new DataObject().set("NO", 1).set("PAYER", "홍길동").set("RCPT_AMT", 10000).set("RCPT_DT", new Date()); * <pre><code> DataObject rec = new DataObject().set("NO", 1).set("PAYER", "홍길동").set("RCPT_AMT", 10000).set("RCPT_DT", new Date());
* XLSWriter xlsx = new XLSWriter() * XLSWriter xlsx = new XLSWriter()
@ -297,7 +310,7 @@ public class XLSWriter extends XLS {
* ... * ...
* xlsx.rowValues(rec, "PAYER", xlsx.keyStyle("RCPT_AMT", numeric), xlsx.keyStyle("RCPT_DT", datetime));</code></pre> * xlsx.rowValues(rec, "PAYER", xlsx.keyStyle("RCPT_AMT", numeric), xlsx.keyStyle("RCPT_DT", datetime));</code></pre>
* @param map * @param map
* @param keysAndStyles , {@link KeyStyle } * @param keysAndStyles , {@link Formatter }
* @return XLSWriter * @return XLSWriter
*/ */
public XLSWriter rowValues(Map<?, ?> map, Object... keysAndStyles) { public XLSWriter rowValues(Map<?, ?> map, Object... keysAndStyles) {
@ -306,7 +319,7 @@ public class XLSWriter extends XLS {
/** . /** .
* .<br /> * .<br />
* {@link KeyStyle} .<br /> * {@link Formatter} .<br />
* . * .
* <pre><code> List&lt;DataObject> dataset = ...; * <pre><code> List&lt;DataObject> dataset = ...;
* XLSWriter xlsx = new XLSWriter() * XLSWriter xlsx = new XLSWriter()
@ -321,9 +334,9 @@ public class XLSWriter extends XLS {
* <pre><code> CellStyle numeric = xlsx.n_nn0(); //{@link #cellStyle(Styler)} 참고 * <pre><code> CellStyle numeric = xlsx.n_nn0(); //{@link #cellStyle(Styler)} 참고
* CellStyle datetime = xlsx.yyyy_mm_dd_hh_mm_ss(); * CellStyle datetime = xlsx.yyyy_mm_dd_hh_mm_ss();
* ... * ...
* xlsx.rowValues(dataset, "PAYER", xlsx.keyStyle("RCPT_AMT", numeric), xlsx.keyStyle("RCPT_DT", datetime));</code></pre> * xlsx.rowValues(dataset, "PAYER", xlsx.style("RCPT_AMT", numeric), xlsx.style("RCPT_DT", datetime));</code></pre>
* @param dataset * @param dataset
* @param keysAndStyles , {@link KeyStyle } * @param keysAndStyles , {@link Formatter }
* @return XLSWriter * @return XLSWriter
*/ */
public XLSWriter values(Iterable<? extends Map<?, ?>> dataset, Object... keysAndStyles) { public XLSWriter values(Iterable<? extends Map<?, ?>> dataset, Object... keysAndStyles) {
@ -339,25 +352,14 @@ public class XLSWriter extends XLS {
return this; return this;
} }
public XLSWriter styles(Styler... stylers) {
if (!isEmpty(stylers)) {
int start = cell.getColumnIndex(),
x = start;
for (Styler styler: stylers) {
col(x);
setCellStyle(styler);
++x;
}
col(start);
}
return this;
}
/** . /** .
* @param styler * @param styler
* @return * @return
*/ */
public CellStyle cellStyle(Styler styler) { public CellStyle cellStyle(Styler styler) {
if (styler == null)
return null;
CellStyle cellStyle = workbook.createCellStyle(); CellStyle cellStyle = workbook.createCellStyle();
styler.set(cellStyle); styler.set(cellStyle);
if (styler.dataFormat != null) { if (styler.dataFormat != null) {
@ -371,17 +373,30 @@ public class XLSWriter extends XLS {
* @param style * @param style
* @return KeyStyle * @return KeyStyle
*/ */
public KeyStyle keyStyle(Object key, CellStyle style) { public Formatter formatter(Object key) {
return new KeyStyle(key, style); return new Formatter().key(key);
} }
/**style KeyStyle . /** function .
* @param key * @param func function
* @param style * @return Formatter
* @return */
public Formatter format(Function<Map<?, ?>, Object> func) {
return formatter(null).format(func);
}
/** (CellStyle, Styler) .
* @param key
* @param style (CellStyle, Styler)
* @return Formatter
*/ */
public KeyStyle keyStyle(Object key, Styler style) { public Formatter style(Object key, Object style) {
return keyStyle(key, cellStyle(style)); Formatter fmt = formatter(key);
if (style instanceof CellStyle)
fmt.style((CellStyle)style);
else if (style instanceof Styler)
fmt.styler((Styler)style);
return fmt;
} }
/** XLSWriter . /** XLSWriter .
@ -424,6 +439,13 @@ public class XLSWriter extends XLS {
return cellStyle(new Styler().dataFormat("yyyy-MM-dd").merge(Styler.CENTER)); return cellStyle(new Styler().dataFormat("yyyy-MM-dd").merge(Styler.CENTER));
} }
/** (HH:mm:ss) .
* @return (HH:mm:ss)
*/
public CellStyle hh_mm_ss() {
return cellStyle(new Styler().dataFormat("HH:mm:ss").merge(Styler.CENTER));
}
/** . {@link #set(CellStyle) CellStyle } . /** . {@link #set(CellStyle) CellStyle } .
* @author mjkhan * @author mjkhan
*/ */
@ -699,30 +721,51 @@ public class XLSWriter extends XLS {
} }
} }
/** /** ,
* @author mjkhan * @author mjkhan
*/ */
public static class KeyStyle { public static class Formatter {
private Object key; private Object key;
private Function<Map<?, ?>, Object> convert;
private CellStyle style; private CellStyle style;
private Styler styler; private Styler styler;
/** KeyStyle . /** .
* @param key * @param key
* @return Formatter
*/
public Formatter key(Object key) {
this.key = key;
return this;
}
/** .
* @param style * @param style
* @return Formatter
*/ */
KeyStyle(Object key, CellStyle style) { public Formatter style(CellStyle style) {
this.key = notEmpty(key, "key");
this.style = style; this.style = style;
return this;
} }
/** KeyStyle . /** .
* @param key
* @param styler * @param styler
* @return Formatter
*/ */
KeyStyle(Object key, Styler styler) { public Formatter styler(Styler styler) {
this.key = notEmpty(key, "key");
this.styler = styler; this.styler = styler;
return this;
}
/** function .
* @param func
* @return Formatter
*/
public Formatter format(Function<Map<?, ?>, Object> func) {
convert = func;
return this;
} }
} }
} }
Loading…
Cancel
Save