首先介绍几种java导出word方案css
一、Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态连接库,并经过JNI的方式实现了在Java平台上对COM程序的调用。DLL动态连接库的生成须要windows平台的支持。html
二、 Apache POI包括一系列的API,它们能够操做基于MicroSoft OLE 2 Compound Document Format的各类格式文件,能够经过这些API在Java中读写Excel、Word等文件。他的excel处理很强大,对于word还局限于读取,目前只能实现一些简单文件的操做,不能设置样式。java
三、 Java2word是一个在java程序中调用 MS Office Word 文档的组件(类库)。该组件提供了一组简单的接口,以便java程序调用他的服务操做Word 文档。
这些服务包括: 打开文档、新建文档、查找文字、替换文字,插入文字、插入图片、插入表格,在书签处插入文字、插入图片、插入表格等。填充数据到表格中读取表格数据 ,1.1版加强的功能: 指定文本样式,指定表格样式。如此,则可动态排版word文档。linux
四、 iText操做Excel还行。对于复杂的大量的word也是噩梦。用法很简单, 可是功能不多, 不能设置打印方向等问题。apache
五、 JSP输出样式基本不达标,并且要打印出来就更是惨不忍睹。windows
六、 用XML作就很简单了。Word从2003开始支持XML格式,大体的思路是先用office2003或者2007编辑好word的样式,而后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档彻底符合office标准,样式、内容控制很是便利,打印也不会变形,生成的文档和office中编辑文档彻底同样。api
看过方案后就知道了 第 6 种方案效果好点,下面咱们就举个例子介绍下这个方案函数
首先要制做模板 模板里的须要传入的数据用${data} 填充,在代码里给其赋值便可测试
模板制做好后 ,点击另存为 xml 文件 如:data.xmlthis
若是xml须要动态填充 能够用
<!-- 循环开始 -->
< #list personnelView as e>
</#list>
<!-- 循环结束 -->
<!-- 循环开始 -->
<#list personnelView as e> // personnelView为循环集合
<w:tr wsp:rsidR="001E023B" wsp:rsidTr="004E45BD">
<w:tblPrEx>
<w:tblCellMar>
<w:top w:w="0" w:type="dxa"/>
<w:bottom w:w="0" w:type="dxa"/>
</w:tblCellMar>
</w:tblPrEx>
<w:trPr>
<w:trHeight w:val="405"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="1920" w:type="dxa"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:pPr>
<w:jc w:val="center"/>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e_index +1}</w:t> //e_index 为索引从0 开始
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1083" w:type="dxa"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_1}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1142" w:type="dxa"/>
<w:gridSpan w:val="3"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:pPr>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_2}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1578" w:type="dxa"/>
<w:gridSpan w:val="3"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_3}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1168" w:type="dxa"/>
<w:gridSpan w:val="2"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_4}</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="2181" w:type="dxa"/>
<w:gridSpan w:val="3"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:p wsp:rsidR="001E023B" wsp:rsidRDefault="001D2E3B" wsp:rsidP="004E45BD">
<w:r>
<w:rPr>
<w:rFonts w:hint="fareast"/>
</w:rPr>
<w:t>${e.COLUMN_5}</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</#list>
<!-- 循环结束 -->
下面是代码
package com.asiainfo.boms.machineroom.action;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class DocumentHandler {
private Configuration configuration = null;
public DocumentHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
public Template getTemplate(){
configuration.setClassForTemplateLoading(this.getClass(),
"/com/asiainfo/boms/machineroom/template");
Template t = null;
try {
// test.ftl为要装载的模板
t = configuration.getTemplate("sg.xml");
t.setEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return t;
}
public Writer getWriter(String jobId){
// 输出文档路径及名称
String savePath = ServletActionContext.getServletContext().getRealPath("/");
File file = new File(savePath+"upload");
if(!file.exists()){
file.mkdirs();
}
File outFile = new File(savePath+"upload/machine"+jobId+".doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
return out;
}
public void createDoc(Template t,Map dataMap,Writer out) {
try {
t.process(dataMap, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// private void getData(Map dataMap) {
//
// dataMap.put("title_name", "用户信息");
//
// dataMap.put("user_name", "李四");
//
// dataMap.put("org_name", "微软公司");
//
// dataMap.put("dept_name", "事业部");
//
// }
}
public String personnelImportWord() throws Exception{
String path = ServletActionContext.getServletContext().getRealPath("/");
String jobId = getStringParameter("job_id");
Map<String, Object> paraMap = new HashMap<String, Object>();
Map dataMap = new HashMap();
paraMap.put("jobId", jobId);
DocumentHandler dh = new DocumentHandler();
Template t = dh.getTemplate();
Writer out = dh.getWriter(jobId);
Map queryWFjobObj = .............
dataMap.put("COLUMN_1", queryWFjobObj.get("COLUMN_1"));
dataMap.put("COLUMN_2", queryWFjobObj.get("COLUMN_2"));
dataMap.put("COLUMN_3", queryWFjobObj.get("COLUMN_3"));
dataMap.put("CLOB_1", queryWFjobObj.get("CLOB_1"));
dataMap.put("DATE_1", queryWFjobObj.get("DATE_1"));
dataMap.put("DATE_2", queryWFjobObj.get("DATE_2"));
dataMap.put("COLUMN_4", queryWFjobObj.get("COLUMN_4"));
Map mobileObj = .................
dataMap.put("CALLER", mobileObj.get("CALLER"));
dataMap.put("STARTDATE", mobileObj.get("STARTDATE"));
dataMap.put("MSISDN", mobileObj.get("MSISDN"));
Map machineroomObj = .......................
dataMap.put("CALLER1", machineroomObj.get("CALLER"));
dataMap.put("STARTDATE1", machineroomObj.get("STARTDATE"));
dataMap.put("MSISDN1", machineroomObj.get("MSISDN"));
List<Map> personnelView = .........................
dataMap.put("personnelView", personnelView);
dh.createDoc(t, dataMap, out);
fileName = "machine"+jobId+".doc";
return "personnelImportWord";
}
public InputStream getInputStream() throws Exception
{
String path = ServletActionContext.getServletContext().getRealPath("/");
File file = new File(path+"upload");
if(!file.exists()){
file.mkdirs();
}
return new FileInputStream(path+"upload/"+fileName);
}
下面再说几种导出pdf 的方案
java生成pdf方案总结
1. Jasper Report生成pdf:设计思路是先生成模板,而后获得数据,最后将二者整合获得结果。可是Jasper Report的问题在于,其生成模板的方式过于复杂,即便有IDE的帮助,咱们仍是须要对其中的众多规则有所了解才行,不然就会给调试带来极大的麻烦。
2. openoffice生成pdf:openoffice是开源软件且能在windows和linux平台下运行。
3. itext + flying saucer生成pdf:itext和flying saucer都是免费开源的,且与平台无关,结合css和velocity技术,能够很好的实现。
通常使用第三种方案比较多,它实现的步骤是很是简单的。
JAVA生成word优缺点对比
所用技术 |
优势 |
缺点 |
Jacob |
功能强大 |
代码量大,设置样式繁琐;须要windows平台支持,没法跨平台 |
Apache POI |
读写excel功能强大、操做简单 |
通常只用它读取word,可以建立简单的word,不能设置样式,功能太少 |
Java2word |
功能强大,操做简单 |
能知足通常要求,不支持07格式,国人开发的,参考资料较多,须要windows平台支持 |
iText |
功能全,能知足通常要求 |
不能直接生成或操做doc文档,只能生成rtf格式的文档,rtf也能够用word打开 |
JSP |
操做简单,代码量少 |
能把当前页面导出简单的word,不能设置样式,美观性差,没法操做word |
XML(最佳) |
代码量少,样式、内容容易控制,打印不变形,彻底符合office标准 |
须要提早设计好word模板,把须要替换的地方用特殊标记标出来 |
JAVA生成pdf优缺点对比
所用技术 |
优势 |
缺点 |
openoffice |
自己就是office软件,很容易设计一些文档模板,支持java调用实现word转换成pdf |
须要先安装,设计好pdf模板样式,而后用程序来填充那些预留好的变量 |
itext |
能知足要求,自己提供了一些api |
没法识别不少html的tag和attribute,没法识别css,须要用其api函数来设置样式 |
Jasper Report |
能知足要求,市面上使用的比较多,相关文档多 |
复杂,很难彻底掌握,须要先设计模板,强依赖于IDE进行可视化编辑 |
flying sauser(最佳) |
能解析html和css输出成image、pdf等格式,操做简单,api强大 |
须要编写freemarker或velocity模板,打造html,勾画pdf的样式 |