Step one: Open the import template, add a placeholder and add an image placeholder
Figure:
Step Two: template as xml format; open xml code, modify the data at key locations; mainly modify $ {a.itemi} placeholder template according to xml, wherein removing extra portions.
FIG modified data:
list traversal cycle output, then to add before and after each line <#list variable name list as your data conversion> </ # list> , equivalent to the output of the preceding traversal cycle li tag, like find the row cycle, plus the list on the line
Figure:
Picture, in which the template to join the picture placeholder, open xml document, there will be a bunch of base64 encoded location where the picture, the long list of base64 encoded form of the picture into picture data variables, such as $ {image}
Figure:
/**
* Service layer
**/
public ResponseFile exportWord(ServiceRequest request, int id) thorws Exception;
/**
* Impl layer
**/
public ResponseFile exportWord(ServiceRequest request, int id) thorws Exception{
YoungInfoQuery voById = youngInfo.getVOById(id);
// prepare data
Map dataMap = new HashMap();
dataMap.put("lastMonth",voById.getTitle() != null ? voById.getTitle() : "");
dataMap.put("item1",voById.getTiaoMu1() != null ? voById.getTiaoMu1() : "");
dataMap.put("item2",voById.getTiaoMu2() != null ? voById.getTiaoMu2() : "");
dataMap.put("item3",voById.getTiaoMu3() != null ? voById.getTiaoMu3() : "");
dataMap.put("item4",voById.getTiaoMu4() != null ? voById.getTiaoMu4() : "");
dataMap.put("item5",voById.getTiaoMu5() != null ? voById.getTiaoMu5() : "");
dataMap.put("item6",voById.getTiaoMu6() != null ? voById.getTiaoMu6() : "");
String oapath = System.getProperty("oapath");
Template t = null;
String templateFilePath = oapath + xxx.ftl; //存放ftl文件的位置
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8")
try {
String ftlPath = templateFilePath.substring(0,templateFilePath.lastIndexof("/"));//ftl文件位置
configuration.setDirectoryForTemplateLoading(new File(ftlPath));//加载文件
String ftlFile = templateFilePath.substring(templateFilePath.lastIndexof("/")+1);//ftl文件
String targetFile = oapath+voById.getTitle()+".doc";
File outFile = new File(targetFile)://指定输出word的路径
if(!outFile.exists(){
outFile.createNewFile();
}
String fileName = outFile.getName();
Writer out = null;
out new BufferedWriter (new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"),10240);
t.process(dataMap,out);
return new ResponseFile(fileName,new FileInputStream(outFile),outFile.length(),outFile.lastModifie()):
} catch (Exception e) {
e.printStacTrace():
}
}