java预览word,
思路:先将word转pdf,在用iframe 来预览pdf
1. 生成生成word
思路:生成思路:生成word大概有两种思路,一种是大概有两种思路,一种是freemarker,一种是,一种是poi 。由于。由于freemarker生成的生成的word本质上就是一堆本质上就是一堆 xml,
兼容性不好这里我们用兼容性不好这里我们用poi,
首先我们先创建一个首先我们先创建一个word模板(要求是模板(要求是 2003版版word以以doc结尾),把需要替换的内容以结尾),把需要替换的内容以 ${xxx}标记,然后替换这些标标记,然后替换这些标
记。代码如下记。代码如下
1.1.所需所需jar包包
dom4j-1.6.1.jar
poi-3.12-20150511.jar
poi-examples-3.12-20150511.jar
poi-excelant-3.12-20150511.jar
poi-ooxml-3.12-20150511.jar
poi-ooxml-schemas-3.12-20150511.jar
poi-scratchpad-3.12-20150511.jar
xmlbeans-2.3.0.jar
1.2. 代码代码
public void testWrite() throws Exception {
String templatePath = "E:\\demo\\zgtzs.doc"; //这个是一个doc模板 样式完全不会乱
InputStream is = new FileInputStream(templatePath);
HWPFDocument doc = new HWPFDocument(is);
Range range = doc.getRange();
//替换数据(当然如果你要替换的比较多,那建议你写一个实体类的反射来填充数据)
range.replaceText("${top_year}", new SimpleDateFormat("yyyy").format(new Date()));
range.replaceText("${content_year1}", new SimpleDateFormat("yyyy").format(new Date()));
range.replaceText("${unit}", "北京云盾科技666");
range.replaceText("${bianHao}", "22");
range.replaceText("${content2}", "asdfasdfasdfassdfasdfasdfaf");
OutputStream os = new FileOutputStream("E:\\demo\\write2.doc");
//把doc输出到输出流中
doc.write(os);
os.close();
is.close();
}
1.3.模板模板
评论0
最新资源