XLSWriter 스타일링, 포맷팅 수정

master
mjkhan21 11 months ago
parent 815ac2676e
commit 92cbb0f8b3

@ -4,6 +4,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.function.Function;
import org.springframework.core.io.ClassPathResource;
@ -64,4 +66,62 @@ public abstract class XLS extends AbstractComponent {
* @param out OutputStream
*/
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.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
@ -38,11 +39,11 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
* dataset .
* dataset .
* <pre><code> xlsx.values(dataset, "RCPT_ID", "RCPT_DT", "PAYER_NM", "RCPT_AMT");</code></pre>
* dataset {@link KeyStyle} .
* dataset {@link Formatter} .
* <pre><code> CellStyle
* datetime = xlsx.yyyy_mm_dd_hh_mm_ss(),
* 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
*/
public class XLSWriter extends XLS {
@ -76,7 +77,7 @@ public class XLSWriter extends XLS {
}
}
private SXSSFWorkbook getWorkbook() {
public SXSSFWorkbook workbook() {
if (workbook != null)
return workbook;
@ -102,9 +103,9 @@ public class XLSWriter extends XLS {
*/
public XLSWriter worksheet(int index) {
try {
worksheet = getWorkbook().getSheetAt(index);
worksheet = workbook().getSheetAt(index);
} catch (Exception e) {
worksheet = getWorkbook().createSheet();
worksheet = workbook().createSheet();
}
worksheet.setRandomAccessWindowSize(rowAccessWindowSize);
return this;
@ -117,7 +118,7 @@ public class XLSWriter extends XLS {
*/
public XLSWriter worksheet(String name) {
try {
SXSSFWorkbook wb = getWorkbook();
SXSSFWorkbook wb = workbook();
worksheet = ifEmpty(wb.getSheet(name), () -> wb.createSheet(name));
worksheet.setRandomAccessWindowSize(rowAccessWindowSize);
return this;
@ -216,7 +217,7 @@ public class XLSWriter extends XLS {
setCellStyle(val);
} else if (val instanceof CellStyle) {
setCellStyle(val);
} else if (val instanceof KeyStyle) {
} else if (val instanceof Formatter) {
setCellStyle(val);
} else {
col(x).value(val);
@ -236,10 +237,10 @@ public class XLSWriter extends XLS {
style = (CellStyle)obj;
} else if (obj instanceof Styler) {
styler = (Styler)obj;
} else if (obj instanceof KeyStyle) {
KeyStyle keyStyle = (KeyStyle)obj;
styler = keyStyle.styler;
style = keyStyle.style;
} else if (obj instanceof Formatter) {
Formatter formatter = (Formatter)obj;
styler = formatter.styler;
style = formatter.style;
}
if (styler != null) {
@ -266,11 +267,23 @@ public class XLSWriter extends XLS {
ArrayList<Object> vals = new ArrayList<>();
for (Object obj: keysAndStyles) {
if (obj instanceof KeyStyle) {
KeyStyle keyStyle = (KeyStyle)obj;
Object val = map.get(keyStyle.key);
if (obj instanceof Formatter) {
Formatter formatter = (Formatter)obj;
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(keyStyle);
Styler styler = formatter.styler;
CellStyle style = formatter.style;
if (styler != null)
style = cellStyle(styler);
if (style != null)
vals.add(style);
} else {
vals.add(map.get(obj));
}
@ -280,7 +293,7 @@ public class XLSWriter extends XLS {
/** map .<br />
* .<br />
* {@link KeyStyle} .<br />
* {@link Formatter} .<br />
* map .
* <pre><code> DataObject rec = new DataObject().set("NO", 1).set("PAYER", "홍길동").set("RCPT_AMT", 10000).set("RCPT_DT", new Date());
* 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>
* @param map
* @param keysAndStyles , {@link KeyStyle }
* @param keysAndStyles , {@link Formatter }
* @return XLSWriter
*/
public XLSWriter rowValues(Map<?, ?> map, Object... keysAndStyles) {
@ -306,7 +319,7 @@ public class XLSWriter extends XLS {
/** .
* .<br />
* {@link KeyStyle} .<br />
* {@link Formatter} .<br />
* .
* <pre><code> List&lt;DataObject> dataset = ...;
* XLSWriter xlsx = new XLSWriter()
@ -321,9 +334,9 @@ public class XLSWriter extends XLS {
* <pre><code> CellStyle numeric = xlsx.n_nn0(); //{@link #cellStyle(Styler)} 참고
* 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 keysAndStyles , {@link KeyStyle }
* @param keysAndStyles , {@link Formatter }
* @return XLSWriter
*/
public XLSWriter values(Iterable<? extends Map<?, ?>> dataset, Object... keysAndStyles) {
@ -339,25 +352,14 @@ public class XLSWriter extends XLS {
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
* @return
*/
public CellStyle cellStyle(Styler styler) {
if (styler == null)
return null;
CellStyle cellStyle = workbook.createCellStyle();
styler.set(cellStyle);
if (styler.dataFormat != null) {
@ -371,17 +373,30 @@ public class XLSWriter extends XLS {
* @param style
* @return KeyStyle
*/
public KeyStyle keyStyle(Object key, CellStyle style) {
return new KeyStyle(key, style);
public Formatter formatter(Object key) {
return new Formatter().key(key);
}
/**style KeyStyle .
* @param key
* @param style
* @return
/** function .
* @param func function
* @return Formatter
*/
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) {
return keyStyle(key, cellStyle(style));
public Formatter style(Object key, Object 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 .
@ -424,6 +439,13 @@ public class XLSWriter extends XLS {
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 } .
* @author mjkhan
*/
@ -699,30 +721,51 @@ public class XLSWriter extends XLS {
}
}
/**
/** ,
* @author mjkhan
*/
public static class KeyStyle {
public static class Formatter {
private Object key;
private Function<Map<?, ?>, Object> convert;
private CellStyle style;
private Styler styler;
/** KeyStyle .
* @param key
/** .
* @param key
* @return Formatter
*/
public Formatter key(Object key) {
this.key = key;
return this;
}
/** .
* @param style
* @return Formatter
*/
KeyStyle(Object key, CellStyle style) {
this.key = notEmpty(key, "key");
public Formatter style(CellStyle style) {
this.style = style;
return this;
}
/** KeyStyle .
* @param key
/** .
* @param styler
* @return Formatter
*/
KeyStyle(Object key, Styler styler) {
this.key = notEmpty(key, "key");
public Formatter styler(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