freemarker导出带图片的word文档
最近做一个关于文档导出功能, 顺便学习了下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文档的更多相关文章
- Java Web用Freemarker生成带图片的Word文档
步骤一:模板制作 用world2003做一个导出模板,如果有图片则加入一张图片占位,将world另存为xml,将xml中需要导出的内容用Freemarker标签表示,最后另存为.ftl结尾的模板: 步 ...
- Freemarker导出带图片的word
1.新建一doc文档
- 基于springboot的freemarker创建指定格式的word文档
在web或其他应用中,经常我们需要导出或者预览word文档,比较实际的例子有招聘网站上预览或者导出个人简历,使用POI导出excel会非常的方便,但是如果想导出word,由于其格式控制非常复杂,故而使 ...
- C#导出文本内容到word文档源码
将做工程过程中较好的代码片段珍藏起来,下面的代码内容是关于C#导出文本内容到word文档的代码,希望能对小伙伴们也有好处.<%@ Page Language="C#" Aut ...
- java 在MySQL中存储文件,读取文件(包括图片,word文档,excel表格,ppt,zip文件等)
转自:https://blog.csdn.net/u014475796/article/details/49893261 在设计到数据库的开发中,难免要将图片或文档文件(如word)插入到数据库中的情 ...
- 关于PowerDesigner导出数据库表到word文档
关于PowerDesigner导出数据库表到word文档 一.查看全部模板: powerdesigner默觉得我们提供了非常多的模版,在工具栏中选择[Report(报告)--->Report T ...
- 解决图片插入word文档后清晰度降低的问题
解决图片插入word文档后清晰度降低的问题 在默认情况下,word程序会自动压缩插入word文档中的图片以减小整个word文档的.当我们需要插入word文档中的图片保持原始清晰度时,可以通过设置wor ...
- PowerDesigner导出pdm设计为Word文档
点击Report->Reports 点击New Report 选择Standard Physical Report,语言选择简体中文,如下图 此时目录下就会多一个Report 右窗口: 根据自己 ...
- java通过freemarker导出包含富文本图片的word文档
废话不多说,进入正题! 本文重点在于:对富文本图片的导出(基础的freemarker+word模板导出这里不做详细解说哈) 参考文章:http://www.cnblogs.com/liaofeifig ...
随机推荐
- python-基于tcp协议的套接字(加强版)及粘包问题
一.基于tcp协议的套接字(通信循环+链接循环) 服务端应该遵循: 1.绑定一个固定的ip和port 2.一直对外提供服务,稳定运行 3.能够支持并发 基础版套接字: from socket impo ...
- The walking dead
邪恶令好人团结 Facing evil brings good people together 只是没有人觉得自己是邪恶的一边 No one ever thinks that they’re the ...
- 完美解决cannot resolve symbol servlet 的报错
1.右键点击项目,打开open module settings 2.选择Libraries 3.选择中间+号,点击java,然后选择tomcat/lib/servlet-api.jar 4.点击app ...
- ***微信小程序学习文档和资料归档收集
微信小程序官方文档: https://cloud.tencent.com/document/product/619 小程序培训视频教程: https://xw.qq.com/edu/201805140 ...
- windows 下 nginx log 分割
默认 nginx 不支持 log自动分割 windows下 解决方案: 1.首先创建bat脚本 split_log.bat , 并保存在nginx 目录下: @echo off rem ...
- Using QueryRunner to insert ArrayList<Object[]>
使用QueryRunner 结合c3p0进行数据库操作时候, 需求:list<bean>进行插入数据库中,但是QueryRunner 仅仅支持batch():批处理: Object[][] ...
- Codeforces 1070J Streets and Avenues in Berhattan dp
Streets and Avenues in Berhattan 我们首先能发现在最优情况下最多只有一种颜色会分别在行和列, 因为你把式子写出来是个二次函数, 在两端取极值. 然后我们就枚举哪个颜色会 ...
- sql server 查询所有表结构
SELECT CASE WHEN col.colorder = 1 THEN obj.name ELSE '' END AS 表名, Coalesce(epTwo.value, '') AS docu ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第五集之补充-使用桥接模式实现虚拟机作为服务器,让同网段的其他主机远程连接】
参考:https://blog.csdn.net/qicheng777/article/details/73438045 https://www.cnblogs.com/hld123/p/650550 ...
- TensorFlow之多核GPU的并行运算
tensorflow多GPU并行计算 TensorFlow可以利用GPU加速深度学习模型的训练过程,在这里介绍一下利用多个GPU或者机器时,TensorFlow是如何进行多GPU并行计算的. 首先,T ...