关于链式调用的写法,参考:poi-tl java插件
1:需要的类: 概述类 概述类的静态内部类-构建数据的类,数据类
概述类
{
STATIC OF() —返回构建数据的静态内部类
构建数据的静态内部类{
对应数据属性私有变量1:
对应数据属性私有变量2:
实例方法设置数据属性1:{
}
实例方法设置数据属性2{
}
//create返回真正的数据对象
CREATE:{
new 数据类()
数据类属性1 = 私有变量1
数据类属性2 = 私有变量2
RETURN 数据类
}
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
//Texts
package com.deepoove.poi.data;
import com.deepoove.poi.data.style.Style;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
public final class Texts {
private Texts() {
}
public static Texts.TextBuilder of(String text) {<br /> return new Texts.TextBuilder(text);<br /> }
public static class TextBuilder implements RenderDataBuilder<TextRenderData> {<br /> private String text;<br /> private Style style;<br /> private String url;<br /> private String bookmark;
private TextBuilder(String text) {<br /> this.text = text;<br /> }
public Texts.TextBuilder style(Style style) {<br /> this.style = style;<br /> return this;<br /> }
public Texts.TextBuilder color(String color) {<br /> if (null != this.style) {<br /> this.style.setColor(color);<br /> } else {<br /> this.style = Style.builder().buildColor(color).build();<br /> }
return this;<br /> }
public Texts.TextBuilder bold() {<br /> if (null != this.style) {<br /> this.style.setBold(true);<br /> } else {<br /> this.style = Style.builder().buildBold().build();<br /> }
return this;<br /> }
public Texts.TextBuilder italic() {<br /> if (null != this.style) {<br /> this.style.setItalic(true);<br /> } else {<br /> this.style = Style.builder().buildItalic().build();<br /> }
return this;<br /> }
public Texts.TextBuilder sup() {<br /> if (null != this.style) {<br /> this.style.setVertAlign("superscript");<br /> } else {<br /> this.style = Style.builder().buildSuper().build();<br /> }
return this;<br /> }
public Texts.TextBuilder sub() {<br /> if (null != this.style) {<br /> this.style.setVertAlign("subscript");<br /> } else {<br /> this.style = Style.builder().buildSub().build();<br /> }
return this;<br /> }
public Texts.TextBuilder fontSize(double fontSize) {<br /> if (null != this.style) {<br /> this.style.setFontSize(fontSize);<br /> } else {<br /> this.style = Style.builder().buildFontSize(fontSize).build();<br /> }
return this;<br /> }
public Texts.TextBuilder fontFamily(String fontFamily) {<br /> if (null != this.style) {<br /> this.style.setFontFamily(fontFamily);<br /> } else {<br /> this.style = Style.builder().buildFontFamily(fontFamily).build();<br /> }
return this;<br /> }
public Texts.TextBuilder link(String url) {<br /> this.url = url;<br /> if (null == this.style) {<br /> this.style = Style.builder().buildColor("0000FF").buildUnderlinePatterns(UnderlinePatterns.SINGLE).build();<br /> }
return this;<br /> }
public Texts.TextBuilder bookmark(String name) {<br /> this.bookmark = name;<br /> return this;<br /> }
public Texts.TextBuilder mailto(String address, String subject) {<br /> StringBuilder sb = new StringBuilder(128);<br /> sb.append("mailto:").append(address).append("?subject=").append(subject);<br /> return this.link(sb.toString());<br /> }
public Texts.TextBuilder anchor(String anchorName) {<br /> StringBuilder sb = new StringBuilder(32);<br /> sb.append("anchor:").append(anchorName);<br /> return this.link(sb.toString());<br /> }
public TextRenderData create() {<br /> TextRenderData data = null;<br /> if (null != this.url) {<br /> data = new HyperlinkTextRenderData(this.text, this.url);<br /> } else if (null != this.bookmark) {<br /> data = new BookmarkTextRenderData(this.text, this.bookmark);<br /> } else {<br /> data = new TextRenderData(this.text);<br /> }
((TextRenderData)data).setStyle(this.style);<br /> return (TextRenderData)data;<br /> }<br /> }<br />}
//TextRenderData
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.deepoove.poi.data;
import com.deepoove.poi.data.style.Style;
public class TextRenderData implements RenderData {
private static final long serialVersionUID = 1L;
protected Style style;
protected String text;
public TextRenderData() {<br /> }
public TextRenderData(String text) {<br /> this.text = text;<br /> }
public TextRenderData(String color, String text) {<br /> this.style = Style.builder().buildColor(color).build();<br /> this.text = text;<br /> }
public TextRenderData(String text, Style style) {<br /> this.style = style;<br /> this.text = text;<br /> }
public Style getStyle() {<br /> return this.style;<br /> }
public void setStyle(Style style) {<br /> this.style = style;<br /> }
public String getText() {<br /> return this.text;<br /> }
public void setText(String text) {<br /> this.text = text;<br /> }
public String toString() {<br /> return this.text;<br /> }<br />}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.deepoove.poi.data;
import java.io.Serializable;
public interface RenderData extends Serializable {
}
//Texts.of(“□”).create()返回数据对象实例,数据对象可以继续添加内部对象
//使用的方式
Texts.of(“□”).fontFamily(“仿宋”).create();
//Texts.of(“□”) 返回构建数据的静态内部类
//Texts.of(“□”).fontFamily(“仿宋”) 依然是返回构建数据的静态内部类
ParagraphRenderData paragraph = new ParagraphRenderData();
paragraph.addText(map.get(picDescKey).toString() + “\n”);
String fileName = ApplicationConfig.uploadPath + map.get(relativePicPathKey);
paragraph.addPicture((Pictures.ofLocal(fileName)).size(picWidth, picHeight).create());
listCell.add(Cells.of().create().addParagraph(paragraph)); //这里是数据实例
if (listCell.size() % columnCount == 0) {
listRow.add(Rows.of(listCell.toArray(new CellRenderData[listCell.size()])).create());
listCell = new ArrayList();
}
}