最近做一个关于文档导出功能, 顺便学习了下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. technologies

    RPC Apache Thrift, Thrift is an interface definition language and binary communication protocol, lik ...

  2. 多个 Github 网站账号 的配置

    账号的基本配置可参考上篇 紧跟上篇已将config 文件配置好了,下面是多账号的配置 一, 多账号也是在config里面配置 1.   首先:  在git 里面输入命令: ll      可以看到  ...

  3. Arrays类的运用,二分法,数组的复制,命令行参数的运用,二维数组,Object,equals

    /*Arrays jdk中为了便于开发,给开发者提供了Arrays类, 其中包含了很多数组的常用操作.例如快速输出.排序.查找等.*/ import java.util.Arrays; public ...

  4. 重磅发布:阿里 OpenJDK终于开源啦! 将长期支持版本 Dragonwell

    前几天的北京阿里云峰会,阿里巴巴正式宣布对外开源 OpenJDK 长期支持版本 Alibaba Dragonwell.作为 Java 全球管理组织 Java Community Process (JC ...

  5. Lesson 1-2

    1.5 模块 模块可视为扩展,通过将其导入可以扩展python的功能.python中自带有一组模块,也称为“标准库”. 1.5.1 模块的导入:import + 模块名称 • 使用关键字import导 ...

  6. 马拉车算法——边界拓展时加限制hdu4513

    #include<bits/stdc++.h> using namespace std; #define maxn 500005 int n,p[maxn],s[maxn],s_new[m ...

  7. 使用token做认证

    对当前用户,使用base64加密token,再解密token,但是不如JWT加密安全 import time import base64 import hmac def generate_token( ...

  8. spring-data-rest的魔力 10分钟实现增删改查

    目录 创建项目 启动项目 添加person 查看person 及 person 列表 条件查询 分页查询 controller 去哪里了 自定义 spring-data-rest 魔力之外的contr ...

  9. C++一个类对象的大小计算

    计算一个类对象的大小时的规律: 1.空类.单一继承的空类.多重继承的空类所占空间大小为:1(字节,下同): 2.一个类中,虚函数本身.成员函数(包括静态与非静态)和静态数据成员都是不占用类对象的存储空 ...

  10. FbinstTools制作多系统启动U盘(Windows+Linux)

    U盘启动盘制作工具在国内有倆工具,老毛桃.大白菜.也不知道是谁模仿谁的,反正PE肯定是Microsoft的. PE其实就是精简版的Windows维护系统,那如何制作Linux启动盘呢,百度搜“linu ...