富文本编辑器内容实现word导出下载,请各位大神们指点,感激不尽-
1、环境支持 1.1 添加poi支持:包下载地址http://www.apache.org/dyn/closer.cgi/poi/release/ 1.2 POI对Excel文件的读取操作比较方便,POI还提供对Word的DOC格式文件的读取。
但在它的发行版本中没有发布对Word支持的模块,需要另外下载一个POI的扩展的Jar包。下载地址为http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/ 下载extractors-0.4_zip这个文件package com.ray.poi.util;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.textmining.text.extraction.WordExtractor;/** * 读写doc * @author wangzonghao * */public class POIWordUtil { /** * 读入doc * @param doc * @return * @throws Exception */ public static String readDoc(String doc) throws Exception { // 创建输入流读取DOC文件 FileInputStream in = new FileInputStream(new File(doc)); WordExtractor extractor = null; String text = null; // 创建WordExtractor extractor = new WordExtractor(); // 对DOC文件进行提取 text = extractor.extractText(in); return text; } /** * 写出doc * @param path * @param content * @return */ public static boolean writeDoc(String path, String content) { boolean w = false; try { // byte b[] = content.getBytes("ISO-8859-1"); byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bais); FileOutputStream ostream = new FileOutputStream(path); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } return w; } }测试package com.ray.poi.util;import junit.framework.TestCase;public class POIUtilTest extends TestCase { public void testReadDoc() { try{ String text = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); System.out.println(text); }catch(Exception e){ e.printStackTrace(); } } public void testWriteDoc() { String wr; try { wr = POIWordUtil.readDoc("E:/work_space/poi/com/ray/poi/util/demo.doc"); boolean b = POIWordUtil.writeDoc("c:\\demo.doc",wr); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}。
JAVA中如何把WORD文档直接转换成html
jacob是java和windows下的com桥,通过它我们可以在java程序中调用COM组件。
如果你的JDK是1。4,那你需要下载jacob1。
9的jni库才能正常运行,早期版本在JDK1。4下有些问题。
packagecom;/***Title:Word文档转html类*Description:*Copyright:()2002*@author舵手*@version1。 0*/importcom。
jacob。com。
*;importcom。jacob。
activeX。*;publicclassWordtoHtml{/***文档转换函数*@paramdocfileword文档的绝对路径加文件名(包含扩展名)*@paramhtmlfile转换后的html文件绝对路径和文件名(不含扩展名)*/publicstaticvoidchange(Stringdocfile,Stringhtmlfile){ActiveXComponentapp=newActiveXComponent("Word。
Application");//启动wordtry{app。setProperty("Visible",newVariant(false));//设置word不可见Objectdocs=app。
getProperty("Documents")。 toDispatch();Objectdoc=Dispatch。
invoke(docs,"Open",Dispatch。Method,newObject[]{docfile,newVariant(false),newVariant(true)},newint[1])。
toDispatch();//打开word文件Dispatch。invoke(doc,"SaveAs",Dispatch。
Method,newObject[]{htmlfile,newVariant(8)},newint[1]);//作为html格式保存到临时文件Variantf=newVariant(false);Dispatch。 call(doc,"Close",f);}catch(Exceptione){e。
printStackTrace();}finally{app。invoke("Quit",newVariant[]{});}}publicstaticvoidmain(String[]strs){WordtoHtml。
change("c:\\a\\运输管理调度系统总体方案。doc","c:\\a\\t");}}。
java如何根据word模板生成word文档
先下载jacob_1.10.1.zip。
解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。将jacob.jar加入项目。
/** Java2word.java** Created on 2007年8月13日, 上午10:32** To change this template, choose Tools | Template Manager* and open the template in the editor.*//** 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。* word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……,以免执行错误的替换。
* 所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、.jpg、.gif)。* 要替换表格中的数据时,HashMap中的Key格式为“table$R@N”,其中:R代表从表格的第R行开始替换,N代表word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、第五列的数据,则第一条记录为String[3] {“1”,”3”,”5”}。
*/package com.word.util;/**** @author kdl*/import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class Java2word { private boolean saveOnExit;/** * word文档 */ Dispatch doc = null;/** * word运行程序对象s */ private ActiveXComponent word; /** * 所有word文档 */ private Dispatch documents;/** * 构造函数 */public Java2word() { if(word==null){ word = new ActiveXComponent("Word.Application"); word.setProperty("Visible",new Variant(false)); } if(documents==null)documents = word.getProperty("Documents").toDispatch(); saveOnExit = false; } /** * 设置参数:退出时是否保存 * @param saveOnExit boolean true-退出时保存文件,false-退出时不保存文件 */ public void setSaveOnExit(boolean saveOnExit) { this.saveOnExit = saveOnExit; } /** * 得到参数:退出时是否保存 * @return boolean true-退出时保存文件,false-退出时不保存文件 */ public boolean getSaveOnExit() { return saveOnExit; }/** * 打开文件 * @param inputDoc String 要打开的文件,全路径 * @return Dispatch 打开的文件 */ public Dispatch open(String inputDoc) { return Dispatch.call(documents,"Open",inputDoc).toDispatch(); //return Dispatch.invoke(documents,"Open",Dispatch.Method,new Object[]{inputDoc},new int[1]).toDispatch(); } /** * 选定内容 * @return Dispatch 选定的范围或插入点 */ public Dispatch select() { return word.getProperty("Selection").toDispatch();} /** * 把选定内容或插入点向上移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveUp(Dispatch selection,int count) { for(int i = 0;i < count;i="" ++)="" dispatch.call(selection,"moveup");}="" *="" *="" 把选定内容或插入点向下移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" movedown(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"movedown");}="" *="" *="" 把选定内容或插入点向左移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveleft(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" {="" dispatch.call(selection,"moveleft");}="" }="" *="" *="" 把选定内容或插入点向右移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */public="" void="" moveright(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)dispatch.call(selection,"moveright");="" }="" *="" *="" 把插入点移动到文件首位置="" *="" @param="" selection="" dispatch="" 插入点="" */="" public="" void="" movestart(dispatch="" selection)="" {dispatch.call(selection,"homekey",new="" variant(6));="" }/**="" *="" 从选定内容或插入点开始查找文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" tofindtext="" string="" 要查找的文本="" *="" @return="" boolean="" true-查找到并选中该文本,false-未查找到文本="" */="" public="" boolean="" find(dispatch="" selection,string="" tofindtext)="" {="" 从selection所在位置开始查询dispatch="" find="word.call(selection,"Find").toDispatch();//设置要查找的内容" dispatch.put(find,"text",tofindtext);//向前查找="" dispatch.put(find,"forward","true");//设置格式="" dispatch.put(find,"format","true");//大小写匹配="" dispatch.put(find,"matchcase","true");//全字匹配="" dispatch.put(find,"matchwholeword","true");//查找并选中="" return="" dispatch.call(find,"execute").getboolean();}="" *="" *="" 把选定内容替换为设定文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" newtext="" string="" 替换为文本="" */="" public="" void="">
怎么用java导出word文档
java导出word大致有6种解决方案:1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。
使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。DLL动态链接库的生成需要windows平台的支持。
该方案只能在windows平台实现,是其局限性。2:Apache POI包括一系列的API,它们可以操作基于MicroSoft OLE 2 Compound Document Format的各种格式文件,可以通过这些API在Java中读写Excel、Word等文件。
他的excel处理很强大,对于word还局限于读取,目前只能实现一些简单文件的操作,不能设置样式。3:Java2word是一个在java程序中调用 MS Office Word 文档的组件(类库)。
该组件提供了一组简单的接口,以便java程序调用他的服务操作Word 文档。 这些服务包括: 打开文档、新建文档、查找文字、替换文字,插入文字、插入图片、插入表格,在书签处插入文字、插入图片、插入表格等。
填充数据到表格中读取表格数据 ,1.1版增强的功能: 指定文本样式,指定表格样式。如此,则可动态排版word文档。
是一种不错的解决方案。4:iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。
通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。功能强大。
5:JSP输出样式,该方案实现简单,但是处理样式有点缺陷,简单的导出可以使用。6:用XML做就很简单了。
Word从2003开始支持XML格式,大致的思路是先用office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样。
java如何根据word模板生成word文档
先下载jacob_1.10.1.zip。
解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。 将jacob.jar加入项目。
/* * Java2word.java * * Created on 2007年8月13日, 上午10:32 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /* * 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。 * word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……, 以免执行错误的替换。
* 所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、 .jpg、.gif)。 * 要替换表格中的数据时,HashMap中的Key格式为“table$R@N”,其中:R代表从表格的第R行开始替换,N代表 word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代 表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、 第五列的数据,则第一条记录为String[3] {“1”,”3”,”5”}。
*/ package com.word.util; /** * * @author kdl */ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Java2word { private boolean saveOnExit; /** * word文档 */ Dispatch doc = null; /** * word运行程序对象s */ private ActiveXComponent word; /** * 所有word文档 */ private Dispatch documents; /** * 构造函数 */ public Java2word() { if(word==null){ word = new ActiveXComponent("Word.Application"); word.setProperty("Visible",new Variant(false)); } if(documents==null) documents = word.getProperty("Documents").toDispatch(); saveOnExit = false; } /** * 设置参数:退出时是否保存 * @param saveOnExit boolean true-退出时保存文件,false-退出时不保存文件 */ public void setSaveOnExit(boolean saveOnExit) { this.saveOnExit = saveOnExit; } /** * 得到参数:退出时是否保存 * @return boolean true-退出时保存文件,false-退出时不保存文件 */ public boolean getSaveOnExit() { return saveOnExit; } /** * 打开文件 * @param inputDoc String 要打开的文件,全路径 * @return Dispatch 打开的文件 */ public Dispatch open(String inputDoc) { return Dispatch.call(documents,"Open",inputDoc).toDispatch(); //return Dispatch.invoke(documents,"Open",Dispatch.Method,new Object[]{inputDoc},new int[1]).toDispatch(); } /** * 选定内容 * @return Dispatch 选定的范围或插入点 */ public Dispatch select() { return word.getProperty("Selection").toDispatch(); } /** * 把选定内容或插入点向上移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveUp(Dispatch selection,int count) { for(int i = 0;i < count;i="" ++)="" dispatch.call(selection,"moveup");="" }="" *="" *="" 把选定内容或插入点向下移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" movedown(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"movedown");="" }="" *="" *="" 把选定内容或插入点向左移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveleft(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" {="" dispatch.call(selection,"moveleft");="" }="" }="" *="" *="" 把选定内容或插入点向右移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveright(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"moveright");="" }="" *="" *="" 把插入点移动到文件首位置="" *="" @param="" selection="" dispatch="" 插入点="" */="" public="" void="" movestart(dispatch="" selection)="" {="" dispatch.call(selection,"homekey",new="" variant(6));="" }="" *="" *="" 从选定内容或插入点开始查找文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" tofindtext="" string="" 要查找的文本="" *="" @return="" boolean="" true-查找到并选中该文本,false-未查找到文本="" */="" public="" boolean="" find(dispatch="" selection,string="" tofindtext)="" {="" 从selection所在位置开始查询="" dispatch="" find="word.call(selection,"Find").toDispatch();" 设置要查找的内容="" dispatch.put(find,"text",tofindtext);="" 向前查找="" dispatch.put(find,"forward","true");="" 设置格式="" dispatch.put(find,"format","true");="" 大小写匹配="" dispatch.put(find,"matchcase","true");="" 全字匹配="" dispatch.put(find,"matchwholeword","true");="" 查找并选中="" return="" dispatch.call(find,"execute").getboolean();="" }="" *="" *="" 把选定内容替换为设定文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" newtext="" string="" 替换为文本="" */="" public="" void="">
转载请注明出处51数据库 » java富文本导出word
説好的25905213