Freemarker进行非空处理

时间:2021-05-18 09:43:28

1.需求是这样的:

一个对象可能有图片,也可能没有,在对象有图片的时候输出图片,没有的时候不做处理。

2.新建一个doc文档,另存为Xml文档(2003版本的)。

修改Xml文档:用Freemarker的if标签包住图片那一行,如果不为空显示,否则不做处理。

<#if test.img??>

<w:p wsp:rsidR="00602F11" wsp:rsidRDefault="00602F11">
<w:r wsp:rsidRPr="009527B6"><w:rPr><w:noProof/></w:rPr><w:pict>
<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/><v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/><v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
<o:lock v:ext="edit" aspectratio="t"/></v:shapetype> <w:binData w:name="${"wordml://0200000"+test_index+1+".jpg"}" xml:space="preserve">
${test.img}
</w:binData> <v:shape id="图片 1" o:spid="_x0000_i1025" type="#_x0000_t75" style="width:236pt;height:144.5pt;visibility:visible;mso-wrap-style:square">
<v:imagedata src="${"wordml://0200000"+test_index+1+".jpg"}" o:title=""/>
</v:shape>
</w:pict>
</w:r>
</w:p> </#if>

4.Java测试代码

package Test8;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.Test; import bean.TestFreemarkerBean;
import freemarker.template.Configuration;
import freemarker.template.Template;
import sun.misc.BASE64Encoder; /**
* 如果图片不为空才显示图片,为空则不显示。
* @author: qlq
* @date : 2017年7月26日下午4:44:59
*/
public class FreeamrkExport { @Test
public void exportSimpleWord() throws Exception {
// 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
Map<String, Object> dataMap = new HashMap<String, Object>(); // Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8"); /*
* 以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 指定ftl文件所在目录的路径,而不是ftl文件的路径
*/
// 指定路径的第一种方式(根据某个类的相对路径指定)
configuration.setClassForTemplateLoading(this.getClass(), "");
// 指定路径的第二种方式,我的路径是C:/a.ftl
// configuration.setDirectoryForTemplateLoading(new File("C:/")); List<person> mylist = new ArrayList<person>();
mylist.add(new person("QQQ", getImageStr("C:/Users/liqiang/Desktop/tupian/kdmt.jpg"), "11111111111111"));
mylist.add(new person("QQQ", getImageStr("C:/Users/liqiang/Desktop/tupian/2.jpg"), "11111111111111"));
mylist.add(new person("QQQ", getImageStr("C:/Users/liqiang/Desktop/tupian/1.jpg"), "11111111111111"));
mylist.add(new person("QQQ", getImageStr("C:/Users/liqiang/Desktop/tupian/3.jpg"), "11111111111111"));
mylist.add(new person("QQQ", getImageStr("C:/Users/liqiang/Desktop/tupian/4.jpg"), "11111111111111"));
mylist.add(new person("QQQ", "11111111111111"));
mylist.add(new person("QQQ", "11111111111111"));
dataMap.put("mylist", mylist);
// 输出文档路径及名称
File outFile = new File("F:/test8.doc"); // 以utf-8的编码读取ftl文件
Template t = configuration.getTemplate("test8.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close();
}
private String getImageStr(String imgFile) throws Exception {
InputStream inputStream = null;
byte []data=null;
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream .close();
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data); }
}

5.结果:

最后两个img属性为null,则不输出。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlEAAADsCAIAAADb1WQLAAAWfUlEQVR4nO3d2XIb14GA4X6Ffga8Aq4nN1PTF8m4JnEcOHYcObI2SLQkWyQlQhRFUaQoiCv2xg5ia+xcQVKSYzvuZDK5wwvgDfodMBdHQqGIhYRiggbP/xVLRQIHh42+OL+6ATSV1vkzYhEjFqlsJbez6Wo6VU2nqlupcipWy8R3cqnd/FYxES7EQ0YslPBtJHyefDScDYdSAV9GD6ZDgYTPk/B5chHdiEWy4VAuEi4l4+VUopyKF6N6OrCZ073FSGAnFdnbihRCm6n1peTa86K+XolsxlcXIi/nNucmX07dnrvz5YMvP3FP34655zKbS5nNxaz3ZT6wtp0IVuPBbGA9HdjIhjzpwEbSuyr+TfnW4puv4puvtvzrW/71+OarhGcl4VmJb76Kbbi3/O8eUowF82FfNuTJ6d6c7k0HNo1wIB8KZPzeXMif9nsyAW8u5M+Fg9lwMKMHMnog+/77fCRUiAaMiD+++UpfWYptuGMb7qR3NenfSAU82XAgGw7kIkEjpuejoUI8nI+GxI/FRKQQDxvxcCEeLsaClWS4nNDFl5iwEAsVEhHxKCOmixmKiUg5FaumE6Vk1IjphXhY3FLZile24rVMsppOiFv2C9ntbKqWSYobK1vxajqxk9uqppPbmfR2NrOTy+7mc9vZTDW9tZvL7Rq5nXx2J5/dNXL7RaNeLu4Xjb1Cvl4uHlXLB6VC+5a9Qn4nn90r5PeLhhhZLxePa5W3u9vHtep+sXBYKR/Xqq+3a0fVykGpeFgpHVXLR9Xyca1yWCntFw3xb/vhB6WCuPegVNgvGgelgvi94peKweIbMZX45rBSOq5Vj2vVg1JRfB1Wygel4q6Rr2UztWxmr2DUy6W9giG2ZK9g7BcL9XKpXi4dVspH1cpRrXJYKx/WykfbleOd6vFO9bBW3i+9G1Mvlw5K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