java freemark导出word (模板、单张图片、多张图片源码)
首先模板设置
代码编辑
设置word只读、全部
环境设置、jar包
**
模板设置
**
设置模板然后另存为
这里一定要选择对xml格式,否则后期模板图片设置的时候有问题,无法多张图片,被这坑过,勿踩!!!
然后就是把xml文件直接修改后缀.ftl文件直接放到项目目录下
代码:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.DynaBean;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder;
/**
* freemark导出word模板
* @since JDK1.8
* @history 2017年12月6日
*/
public class WordExportService {
private static WordExportService wordExport = null;
public static WordExportService getInstance(){
if(wordExport == null){
wordExport = new WordExportService();
}
return wordExport;
}
private Configuration configuration = null;
private static JcglDao dao = new JcglDao();
public WordExportService(){
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
//主方法
public void wordExport(HttpServletRequest request,HttpServletResponse response) throws IOException{
String yjbh = request.getParameter("yjbh");
String src = createWord(yjbh);
response.getWriter().write(src);
}
public String createWord(String yjbh){
List<DynaBean> beanList =dao.queryYjxxRwxq(yjbh);
String yjmc = BeanUtils.getProperty(beanList.get(0), "YJMC");
Map<String,Object> dataMap=new HashMap<String,Object>();
getData(dataMap,beanList);
configuration.setClassForTemplateLoading(this.getClass(), "/com/szboanda/ewaq/jcsj/service/template"); //FTL文件所存在的位置
Template t=null;
try {
t = configuration.getTemplate("rwgla.ftl"); //文件名
} catch (IOException e) {
e.printStackTrace();
}
String path = GgdmService.getInstance().getDmnrByDmjAndDm("TPDZ","FILEPATH");
File outFile = new File(path+"/"+yjmc+".doc");
// File outFile = new File(“D:/vdir/”+Math.random()*10000+”.doc”);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
//设置word导出格式 如果不加读出的word是只读(false)true是非只读
out.close();
outFile.setWritable(true);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return outFile.getName();
}
private void getData(Map<String, Object> dataMap,List<DynaBean> beanList) {
DynaBean bean = beanList.get(0);
List<DynaBean> qysbList = new ArrayList<DynaBean>();
dataMap.put("rwmc", BeanUtils.getProperty(bean, "YJMC") );
dataMap.put("kssj", BeanUtils.getProperty(bean, "YJKSSJ").substring(0, 11));
dataMap.put("jssj", BeanUtils.getProperty(bean, "YJJSSJ").substring(0, 11));
dataMap.put("zrr", BeanUtils.getProperty(bean, "CJR"));
dataMap.put("yjlb", BeanUtils.getProperty(bean, "YJLX"));
dataMap.put("rwms", BeanUtils.getProperty(bean, "YJXX"));
List<String> qymcList = new ArrayList<String>();
List<String> wtmsList = new ArrayList<String>();
List<List<String>> qytpList = new ArrayList<List<String>>();
List<String> qyList = new ArrayList<String>();
for(DynaBean beans: beanList){
String yjbh = BeanUtils.getProperty(beans, "YJBH");
String qybh = BeanUtils.getProperty(beans, "WRYBH");
String qymc = BeanUtils.getProperty(beans, "WRYMC");
qymcList.add(qymc);
qysbList=dao.queryRwpfXq(yjbh,qybh);
List<String> urlList = new ArrayList<String>();
for(DynaBean qysbBean : qysbList){
String url = BeanUtils.getProperty(qysbBean, "URL");
urlList.add(url);
qyList.add(qymc);
}
qytpList.add(urlList);
String wtms = "";
if(qysbList.size() > 0){
wtms = BeanUtils.getProperty(qysbList.get(0), "WTMS");
}else{
wtms = "未检查";
}
wtmsList.add(wtms);
}
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for (int i = 0; i < qymcList.size(); i++) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("qymc", qymcList.get(i));
map.put("wtms", wtmsList.get(i));
list.add(map);
}
List<String> imageMsgList = new ArrayList<String>();
for (int i = 0; i < qytpList.size(); i++) {
for (int j = 0; j < qytpList.get(i).size(); j++) {
Map<String,Object> map = new HashMap<String,Object>();
// map.put("image", getImageStr(qytpList.get(i).get(j)));
//图片封装多张就是多个String 集合就可以
imageMsgList.add(getImageStr(qytpList.get(i).get(j)));
}
}
//这里是因为要与企业名称对应顾要一起按顺序添加
List<Map<String,Object>> wryList = new ArrayList<Map<String,Object>>();
for (int i = 0; i < qyList.size(); i++) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("qymc", qymcList.get(i));
map.put("image", imageMsgList.get(i));
wryList.add(map);
}
// dataMap.put(“imageMsgList”, imageMsgList); 多张图片显示时
// dataMap.put(“qyList”,qyList);
dataMap.put(“wryList”,wryList);
dataMap.put(“list”, list);
}
public String getImageStr(String imgFile) {
// String imgFile = “d:/aa.png”;
String path = GgdmService.getInstance().getDmnrByDmjAndDm(“TPDZ”,”IMGPATH”);
imgFile = path+imgFile;
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
}
ftl模板修改
通过w:tr搜索找到换行处添加
<#list list as test>
里面的值要用test.name代替 如果是一个就不用了后台代码封装就是一个list 不是map就不用
如果是map
列表循环
多张图片
多张图片一定要切记找到如图然后增加
src=”${“wordml://0300000”+qy_index+1+”.png”}”
这里设置成变量 否则freemark只显示第一张图片
<#list wryList as qy>
${qy.image}