freemaker导出word

 ftl文件

1.用Microsoft Office Word打开word原件;

2.把需要动态修改的内容替换成***,如果有图片,尽量选择较小的图片几十K左右,并调整好位置;

3.另存为,选择保存类型Word 2003 XML 文档(*.xml)【这里说一下为什么用Microsoft Office Word打开且要保存为Word 2003XML,本人亲测,用WPS找不到Word 2003XML选项,如果保存为Word XML,会有兼容问题,避免出现导出的word文档不能用Word 2003打开的问题】;

4.用Firstobject free XML editor打开文件,选择Tools下的Indent【或者按快捷键F8】格式化文件内容。左边是文档结构,右边是文档内容;

5. 将文档内容中需要动态修改内容的地方,换成freemarker的标识。其实就是Map中key,如${landName};

6.在加入了图片占位的地方,会看到一片base64编码后的代码,把base64替换成${image},也就是Map中key,值必须要处理成base64;

  代码如:${image}

  注意:“>${image}<”这尖括号中间不能加任何其他的诸如空格,tab,换行等符号。

  如果需要循环,则使用:<#list maps as map>  maps是Map中key,值为数组,map为自定义;

7. 标识替换完之后,模板就弄完了,另存为.ftl后缀文件即可。注意:一定不要用word打开ftl模板文件,否则xml内容会发生变化,导致前面的工作白做了。


function exportDetail1(name,id,type){

   
    var picBase64Info = myChart.getDataURL(); 
    console.log(picBase64Info);
    var picBase64Info1 = myChart1.getDataURL(); 
    var picBase64Info2 = getFullCanvasDataURL("contain2");
   
window.location.href="${root!}/supervision/mainPage/punishMonitorMainPage/exportDetail03?name="+picBase64Info+"&name2="+picBase64Info1+"&name3="+c;
}
    

@SuppressWarnings({ "deprecation", "unused" })

@RequestMapping(value = "/exportDetail03", method = RequestMethod.GET)
public void exportDetail03(HttpServletRequest request,
HttpServletResponse response) throws Exception {


try {

String a = this.getPara("name");
String a2 = this.getPara("name2");
String a3 = this.getPara("name3");
String root = null;
if (root == null) {
root = request.getSession().getServletContext().getRealPath("/");// 服务器路径
}
String dir = "assets/data/";


// Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");

configuration.setDirectoryForTemplateLoading(new File(root+"/data/template"));


Map dataMap = new HashMap();




String nname = "file.doc";
File outFile = new File(root + dir + nname);

//echart直接前台传图片编码到后台
a = a.replaceAll(" ", "+");

String[] arr = a.split("base64,");  
dataMap.put("image", org.apache.commons.lang.StringEscapeUtils.unescapeJava(arr[1]));

a2 = a2.replaceAll(" ", "+");

String[] arr2 = a2.split("base64,"); 
//不转义
dataMap.put("image2", org.apache.commons.lang.StringEscapeUtils.unescapeJava(arr2[1]));



a3 = a3.replaceAll(" ", "+");

String[] arr3 = a3.split("base64,");  
dataMap.put("image3", org.apache.commons.lang.StringEscapeUtils.unescapeJava(arr3[1]));



Template t = null;
t = configuration.getTemplate("baodeyan.ftl", "utf-8");

// Template t =
// configuration.getTemplate("fctestpaper1.ftl","utf-8");
Writer out1 = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out1);
out1.close();

// 取得文件名。
String filename1 = outFile.getName();
// 取得文件的后缀名。
String ext = filename1.substring(filename1.lastIndexOf(".") + 1)
.toUpperCase();


// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(root
+ dir + filename1));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
// response.addHeader("Content-Disposition", "attachment;filename="
// + new String(filename1.getBytes()));
response.setHeader(
"Content-Disposition",
"attachment;"
+ (new StringBuilder("filename=").append(encode(
request, filename1)).toString()));
// response.addHeader("Content-Length", "");


response.addHeader("Content-Length", "" + outFile.length());
OutputStream toClient = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
// 删除本地文件
boolean del = outFile.delete();


} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}

你可能感兴趣的:(公司框架)