最近做一个关于文档导出功能, 顺便学习了下freemarker,做了个关于导出带图片的word文档,模板并没有写全,只是验证代码的正确性

这只是做一个小功能,故只做了后台代码关于导出的代码,并未与前台关联,可酌情处理

首先将需要导出的word文档做处理,关于word文档最好是后缀为.doc,应为有些软件可能无法打开导出的文档,将需要修改的数据修改成${xxx}

类型的内容,例如下面的文档

修改后则变为如下类型: 

后将文档另存为.xml文档,将需要修改的然后再打开xml文档,找到图片的位置,是一大段base64编码后的

代码,形如<w:binData w:name="wordml://自定义.png" xml:space="preserve">UUUA......FFFFA</w:binData>

将中间编码后的代码修改为${xxx}的类型,关于xxx的内容可以根据自己的需求修改,而后将文件关闭。将其后

缀转为 .ftl 的格式。而后上后台代码:

 final static String separator = File.separator; // 系统路径分割符 

     public void exportSellPlan(HttpServletRequest request, HttpServletResponse response){
String evaluation = "四年的大学生涯是我人生的一大转折点。"
+ "我深深地懂得“责任,荣誉,国家”这六个字的含义。作为大学生我最基本的责任是学习。"
+ "在大学期间我认真学习,发挥自己的特长,挖掘自身的潜力,从而提高了"
+ "自身的学习能力和分析处理问题的能力,也获得了大家的认同。";
//获得数据
Map<String, Object> map = new HashMap<String, Object>();
map.put("profession", "xxx");
map.put("name", "xxx");
map.put("address","山西省xx县");
map.put("email", "xxx@163.com");
map.put("tel", "173xxx9927");
map.put("skill1", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想");
map.put("skill2", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想");
map.put("skill3", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想");
map.put("skill4", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想");
map.put("hobby", "运动,看书,上网聊天,看电影。");
map.put("evaluation", evaluation);
map.put("graduateInstitutions", "南昌航空大学");
map.put("degree", "本科");
//map.put("image", getImageBase("E:"+ separator +"photo.jpg"));
try {
WordUtils.exportMillCertificateWord(request,response,map,"简历","jianli.ftl");
} catch (IOException e) {
e.printStackTrace();
}
} //获得图片的base64码
public static String getImageBase(String src) {
if(src==null||src==""){
return "";
}
File file = new File(src);
//File file = new File(getRequest().getRealPath("/")+src.replace(getRequest().getContextPath(), ""));
if(!file.exists()) {
return "";
}
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}

关于excel导出:

 package com.haiyisoft.iecp.crm.web.struts;

 import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView; import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException; public class WordUtils extends BaseAction{
//配置信息,代码本身写的还是很可读的,就不过多注解了
private static Configuration configuration = null;
//这里注意的是利用WordUtils的类加载器动态获得模板文件的位置
// private static final String templateFolder = WordUtils.class.getClassLoader().getResource("../../").getPath() + "WEB-INF/templetes/";
private static final String templateFolder = "D:/uepstudio_uep/workspace/iecp/src/com/haiyisoft/iecp/crm/web/struts";
static {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
try {
configuration.setDirectoryForTemplateLoading(new File(templateFolder));
} catch (IOException e) {
e.printStackTrace();
}
} private WordUtils() {
throw new AssertionError();
} @SuppressWarnings("rawtypes")
public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map,String title,String ftlFile) throws IOException {
//Template freemarkerTemplate = configuration.getTemplate(ftlFile);
Template t = null;
try {
// temp.ftl为要装载的模板
t = configuration.getTemplate(ftlFile);
} catch (IOException e) {
e.printStackTrace();
}
// 获取桌面路径
File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
String desktopPath = desktopDir.getAbsolutePath();
desktopPath = desktopPath.replace("\\", "/");
// 输出文档路径及名称
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
File outFile = new File(desktopPath + "/" + sdf.format(new Date()) + title);
Writer out = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
out = new BufferedWriter(oWriter);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
t.process(map, out);
out.close();
fos.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter outW = response.getWriter();
outW.write("导出成功!</br>" + outFile.getPath());
outW.close();
}
}

导出后的文件为20180322135608简历.doc桌面文件:

freemarker导出带图片的word文档的更多相关文章

  1. Java Web用Freemarker生成带图片的Word文档

    步骤一:模板制作 用world2003做一个导出模板,如果有图片则加入一张图片占位,将world另存为xml,将xml中需要导出的内容用Freemarker标签表示,最后另存为.ftl结尾的模板: 步 ...

  2. Freemarker导出带图片的word

    1.新建一doc文档

  3. 基于springboot的freemarker创建指定格式的word文档

    在web或其他应用中,经常我们需要导出或者预览word文档,比较实际的例子有招聘网站上预览或者导出个人简历,使用POI导出excel会非常的方便,但是如果想导出word,由于其格式控制非常复杂,故而使 ...

  4. C#导出文本内容到word文档源码

    将做工程过程中较好的代码片段珍藏起来,下面的代码内容是关于C#导出文本内容到word文档的代码,希望能对小伙伴们也有好处.<%@ Page Language="C#" Aut ...

  5. java 在MySQL中存储文件,读取文件(包括图片,word文档,excel表格,ppt,zip文件等)

    转自:https://blog.csdn.net/u014475796/article/details/49893261 在设计到数据库的开发中,难免要将图片或文档文件(如word)插入到数据库中的情 ...

  6. 关于PowerDesigner导出数据库表到word文档

    关于PowerDesigner导出数据库表到word文档 一.查看全部模板: powerdesigner默觉得我们提供了非常多的模版,在工具栏中选择[Report(报告)--->Report T ...

  7. 解决图片插入word文档后清晰度降低的问题

    解决图片插入word文档后清晰度降低的问题 在默认情况下,word程序会自动压缩插入word文档中的图片以减小整个word文档的.当我们需要插入word文档中的图片保持原始清晰度时,可以通过设置wor ...

  8. PowerDesigner导出pdm设计为Word文档

    点击Report->Reports 点击New Report 选择Standard Physical Report,语言选择简体中文,如下图 此时目录下就会多一个Report 右窗口: 根据自己 ...

  9. java通过freemarker导出包含富文本图片的word文档

    废话不多说,进入正题! 本文重点在于:对富文本图片的导出(基础的freemarker+word模板导出这里不做详细解说哈) 参考文章:http://www.cnblogs.com/liaofeifig ...

随机推荐

  1. Leetcode 992 Subarrays with K Different Integers

    题目链接:https://leetcode.com/problems/subarrays-with-k-different-integers/ 题意:已知一个全为正数的数组A,1<=A.leng ...

  2. 2018-2019-2 20165314『网络对抗技术』Exp5:MSF基础应用

    一.原理与实践说明 1.实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实践,如ms08-067; (1分) 一个针对浏览器的攻 ...

  3. Angular2 ng2 如何配置惰性加载

    需要修改至少四个地方1. 将子组件进行模块化操作2.生成子组件module .子组件router3.配置主路由 信息 改为loadChild4.配置appModule 删除引入 以product组件 ...

  4. 深入理解css优先级

    为什么要写这篇文章是因为 <style type="text/css"> body h1 { color: green; } html h1 { color: purp ...

  5. Linux 高阶命令进阶(一)

    Linux 高阶命令进阶 (一)输出重定向 1. > :正确覆盖输出,会覆盖掉原先的文件内容 把文本写入文档中                # vim test                 ...

  6. Gradle 下载的依赖包在什么位置?

    Mac系统默认下载到:/Users/(用户名)/.gradle/caches/modules-2/files-2.1Windows系统默认下载到:C:\Users\(用户名)\.gradle\cach ...

  7. Python实现RSA无填充加密,兼容BouncyCastle

    场景 某系统登录时密码经过前台rsa加密传给后端,为实现模拟登录需要原样生成加密串. 分析 前台通过RSA.js.BigInt.js.Barrett.js三个js文件实现加密,公钥通过ajax请求获得 ...

  8. bs4库学习

    # -*- coding:utf-8 -*- import bs4 import requests def tags_val(tag, key='', index=0): ''' tag指HTML元素 ...

  9. 【C语言编程练习】7.1 线型表就地逆置

    写在前面的话:直接从第5章跳到了第7章数据结构的趣题,原因是前面的数学趣题做久了,会觉得稍许疲倦,所以想“变个口味”,以后数学趣题和数据结构混合着练习. 1. 题目要求 编写一个函数,实现顺序表的就地 ...

  10. WWH——学习方法理解与分析

    WWH是"What+Why+How"的简称,是对学习方法最完美的概括."如果不按照WWH这种模式来教学,90%的结果是老师没教好,学生学不好." 1.What( ...