Java用freemarker导出Word 文档
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<String, Object>中key,如${landName};
6.在加入了图片占位的地方,会看到一片base64编码后的代码,把base64替换成${image},也就是Map<String, Object>中key,值必须要处理成base64;
代码如:<w:binData w:name="wordml://自定义.png" xml:space="preserve">${image}</w:binData>
注意:“>${image}<”这尖括号中间不能加任何其他的诸如空格,tab,换行等符号。
如果需要循环,则使用:<#list maps as map></#list> maps是Map<String, Object>中key,值为数组,map为自定义;
7. 标识替换完之后,模板就弄完了,另存为.ftl后缀文件即可。注意:一定不要用word打开ftl模板文件,否则xml内容会发生变化,导致前面的工作白做了。
导出到本地:
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat; import freemarker.template.Configuration;
import freemarker.template.Template;
import sun.misc.BASE64Encoder; public class WordTest { // 此目录路径 要求 完整的路径,并且 windows 不能以 /开头 如 : /c:/xxx/xxx
static URL freemarker = WordTest.class.getClassLoader().getResource("freemarker");
static String freemarkerPath = freemarker != null ? new File(freemarker.getFile()).getAbsolutePath() : new File("freemarker").getAbsolutePath(); //去除PDF水印文件
static URL aspose = WordTest.class.getClassLoader().getResource("freemarker");
static String asposePath = freemarker != null ? new File(freemarker.getFile()).getAbsolutePath() : new File("freemarker").getAbsolutePath(); public static void main(String[] args) {
WordTest wordTest = new WordTest();
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy.MM.dd");
Date date = new Date();
// 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
Map<String, Object> dataMap = new HashMap<String, Object>();
List<Object> list = new ArrayList<Object>();
for (int i = 0; i < 10; i++) {
WordUser word = new WordUser();
word.setImgEntityId(new Long(i));
word.setImgEntityName("目标" + i);
word.setLeftUpLatitude(231.342 + i * 10);
word.setLeftUpLongitude(234.32425 + i * 10);
list.add(word);
}
dataMap.put("date", sdf.format(date));
dataMap.put("dateTime", sdf1.format(date));
dataMap.put("organicName", "国有控股");
dataMap.put("picCreateTime", sdf1.format(new Date()));
dataMap.put("picCreateTime2", sdf2.format(new Date()));
dataMap.put("picCreateTime3", sdf3.format(new Date()));
dataMap.put("imgTypeName", "E-22战斗机");
dataMap.put("imgRootName", "无人机");
dataMap.put("imgEntity", "飞机、战斗机、歼-22、隐形战机");
dataMap.put("list", list); dataMap.put("station2", "先空下来");
dataMap.put("coordinate", "先空下来");
dataMap.put("thematicWords", "境外低轨侦察卫星"); dataMap.put("imgStr", wordTest.getImageStr2("http://47.93.82.37:8180/1.png"));
// dataMap.put("imgStr", wordTest.getImageStr("/Users/macbook/Documents/1.png"));
try {
wordTest.exportSimpleWord(dataMap);
}
catch (Exception e) {
e.printStackTrace();
}
} public void exportSimpleWord(Map<String, Object> dataMap) throws Exception {
// Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8"); /*
* 以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 指定ftl文件所在目录的路径,而不是ftl文件的路径
*/
// 指定路径的第一种方式(根据某个类的相对路径指定)
// configuration.setClassForTemplateLoading(this.getClass(),""); // 指定路径的第二种方式,我的路径是C:/a.ftl
configuration.setDirectoryForTemplateLoading(new File(freemarkerPath)); // 创建临时文件
// File outFile = File.createTempFile("pattern", ".docx"); // 输出文档路径及名称
File outFile = new File("/Users/macbook/Documents/test1.doc"); // 以utf-8的编码读取模版ftl文件
Template t = configuration.getTemplate("test4.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close();
// 删除临时文件
// outFile.delete(); // word文档转换成PDF
String wordPath = "/Users/macbook/Documents/test1.doc";
doc2pdf(wordPath);
}
// 获取图片流
public String getImageStr2(String imgPath) {
ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
URL url = new URL(imgPath);
byte[] by = new byte[1024];
// 创建链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(60 * 1000);
InputStream is = conn.getInputStream();
// 将内容读取内存中
int len = -1;
while ((len = is.read(by)) != -1) {
data.write(by, 0, len);
}
// 关闭流
is.close();
}
catch (Exception e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data.toByteArray());
} // word转PDF
public void doc2pdf(String wordPath) {
// if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
// return;
// }
try {
long old = System.currentTimeMillis();
File file = new File("/Users/macbook/Documents/test1.pdf"); // 新建一个空白pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); // Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
}
catch (Exception e) {
e.printStackTrace();
}
} // 验证License 若不验证则转化出的pdf文档会有水印产生
public boolean getLicense() {
boolean result = false;
try {
//InputStream is = WordTest.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
InputStream is = new FileInputStream(asposePath+"/license2.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
}
catch (Exception e) {
e.printStackTrace();
}
return result;
} }
浏览器直接下载(get请求):
@RequestMapping(value="/investigation/exportWord",method = RequestMethod.GET)
@ResponseBody
public void exportWord(HttpServletRequest request, HttpServletResponse response) {
JSONObject result = new JSONObject();
Long imgPicId = Long.valueOf(request.getParameter("imgPicId"));
// 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
Map<String, Object> dataMap = exportSimpleWordService.exportSimpleWord(imgPicId);
//获取图片类型名称作为文件名
String imgTypeName = (String) dataMap.get("imgTypeName"); // Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8"); // 指定路径我的路径是C:/a.ftl
try {
configuration.setDirectoryForTemplateLoading(new File(freemarkerPath));// 指定ftl所在目录,根据自己的改
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename=\"" + new String((imgTypeName+"侦查要报.doc").getBytes("GBK"), "iso8859-1") + "\"");
response.setCharacterEncoding("utf-8");// 此句非常关键,不然word文档全是乱码
PrintWriter out = response.getWriter();
Template t = configuration.getTemplate("investigation.ftl", "utf-8");// 以utf-8的编码读取ftl文件
t.process(dataMap, out);
out.close();
}
catch (Exception e) {
logger.error("转换成word文档业务服务异常!", e);
result.put("respCode", BaseRspConstants.RSP_CODE_FAILUR);
result.put("respDesc", BaseRspConstants.RSP_DESC_FAILUR);
} // return jsonObject;
}
Java用freemarker导出Word 文档的更多相关文章
- Java使用freemarker导出word文档
通过freemarker,以及JAVA,导出word文档. 共分为三步: 第一步:创建模板文件 第二步:通过JAVA创建返回值. 第三步:执行 分别介绍如下: 第一步: 首先创建word文档,按照想要 ...
- freemarker导出word文档——WordXML格式解析
前不久,公司一个项目需要实现导出文档的功能,之前是一个同事在做,做了3个星期,终于完成了,但是在项目上线之后却发现导出的文档有问题,此时,这个同事已经离职,我自然成为接班者,要把导出功能实现,但是我看 ...
- freemarker导出word文档
使用freemarker导出word文档的过程 **************************************************************************** ...
- java使用freemarker 生成word文档
java 生成word文档 最近需要做一个导出word的功能, 在网上搜了下, 有用POI,JXL,iText等jar生成一个word文件然后将数据写到该文件中,API非常繁琐而且拼出来的 ...
- 使用Freemarker导出Word文档(包含图片)代码实现及总结
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- java使用freemarker生成word文档
1.原料 开源jar包freemarker.eclipse.一份模板word文档 2.首先设计模板word文档 一般,通过程序输出的word文档的格式是固定的,例如建立一个表格,将表格的标题写好,表格 ...
- 使用FreeMarker导出word文档(支持导出图片)
一.添加maven依赖,导入FreeMarker所需要的jar包 <dependency> <groupId>org.freemarker</groupId> &l ...
- java用iText导出word文档
1.需要导入的jar包 2.导出word并下载其实是分两步的. 第一步是将需要导出的数据导出(上传)到服务器上 第二步是将服务器上的文档下载到本地 3. 第一步.上传文档 (1)设置响应信息以及构造上 ...
- Java 用Freemarker完美导出word文档(带图片)
Java 用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...
随机推荐
- 插入排序InsertSort
插入排序:从第二个数开始 一直和前面的数组比较 获得排序定位 代码 /** *插入排序 */ public class InsertSort { public static void inser ...
- BZOJ 2333 [SCOI2011]棘手的操作 (可并堆)
码农题.. 很显然除了两个全局操作都能用可并堆完成 全局最大值用个multiset记录,每次合并时搞一搞就行了 注意使用multiset删除元素时 如果直接delete一个值,会把和这个值相同的所有元 ...
- Linux 获取帮助
Linux中获取帮助的方法 方法: COMMAND --help 或者 help COMMAND whatis man或info 本地帮助文档 /usr/share/doc/ ...
- Python编程:从入门到实践 - matplotlib篇 - plot & scatter
matplotlib篇 plot & scatter # filename.py 获取当前文件名方法 import sys # 当前文件名 print(sys.argv[0]) # 去除后缀后 ...
- LaTeX soul包
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50774955 详细的soul包的官方P ...
- yii 表单小部件使用
首先创建model层 因为要使用表单小部件 所以要加载相应的组件 这里需要的组件有 yii\widgets\ActiveForm 和 yii\helpers\Html 接下来在model定义的clas ...
- bzoj 3236: [Ahoi2013]作业(缺线段树)
3236: [Ahoi2013]作业 Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1744 Solved: 702[Submit][Status ...
- jdk环境变量设置理解
1.系统变量→新建 JAVA_HOME 变量 . 变量值填写jdk的安装目录(本人是 E:\Java\jdk1.7.0) 2.系统变量→寻找 Path 变量→编辑 在变量值最后输入 %JAVA_HOM ...
- chrome打开网址但是没有地址栏
chrome打开网址但是没有地址栏 C:\Users\Administrator>C:\Users\Administrator\AppData\Local\Google\Chrome\Appli ...
- UESTC--1262--Memory(dfs)
Memory Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu SubmitStatus De ...