用户登录
用户注册

分享至

java怎么把html转换成word

  • 作者: 贫道法号胜少
  • 来源: 51数据库
  • 2020-04-21

一、JAVA中如何把WORD文档直接转换成html

jacob是java和windows下的com桥,通过它我们可以在java程序中调用COM组件。

如果你的JDK是1。4,那你需要下载jacob1。

9的jni库才能正常运行,早期版本在JDK1。4下有些问题。

package com;/** * Title:Word文档转html类 * Description: * Copyright:() 2002 * @author 舵手 * @version 1。 0 */import com。

jacob。com。

*;import com。jacob。

activeX。*; public class WordtoHtml { /** *文档转换函数 *@param docfile word文档的绝对路径加文件名(包含扩展名) *@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名) */ public static void change(String docfile, String htmlfile) { ActiveXComponent app = new ActiveXComponent("Word。

Application");// 启动word try { app。setProperty("Visible", new Variant(false)); //设置word不可见 Object docs = app。

getProperty("Documents")。 toDispatch(); Object doc = Dispatch。

invoke(docs,"Open",Dispatch。Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1])。

toDispatch(); // 打开word文件 Dispatch。invoke(doc, "SaveAs", Dispatch。

Method, new Object[] {htmlfile, new Variant(8) }, new int[1]); // 作为html格式保存到临时文件 Variant f = new Variant(false); Dispatch。 call(doc, "Close", f); } catch (Exception e) { e。

printStackTrace(); } finally { app。invoke("Quit", new Variant[]{}); } } public static void main(String[] strs){ WordtoHtml。

change("c:\\a\\运输管理调度系统总体方案。doc", "c:\\a\\t"); }}。

二、如何java程序将 word转换成html

Dispatch wordfile = Dispatch.invoke(

wordacc,

"Open",

Dispatch.Method,

new Object[] { ls_word, new Variant(false),

new Variant(true) }, new int[1]).toDispatch();

Dispatch.invoke(wordfile, "SaveAs", Dispatch.Method, new Object[] {

ls_html, new Variant(8) }, new int[1]);

Variant f = new Variant(false);

// 编写 生成的html

Dispatch.call((Dispatch) wordfile, "Close", (Object) f);

在执行Dispatch.invoke(wordfile, "SaveAs", Dispatch.Method, new Object[] {

ls_html, new Variant(8) }, new int[1]);

jacob.jar

jacob-1.14-x86.dll jacob-1.14-x64.dll 这些的位置有什么要求

抛出异常 路径绝对没有错

com.jacob.com.ComFailException: Invoke of: SaveAs

Source: Microsoft Word

Description: 这不是有效文件名。

请试用下列方法:

* 检查路径,确认键入无误。

* 从文件和文件夹列表中选择文件。

at com.jacob.com.Dispatch.invokev(Native Method)

at com.jacob.com.Dispatch.invokev(Dispatch.java:858)

at com.jacob.com.Dispatch.invoke(Dispatch.java:502)

三、java怎么由html生成word,保留html样式

@RequestMapping("download")

public void exportWord( HttpServletRequest request, HttpServletResponse response)

throws Exception {

User user = AppContext.getLoginUser();

Student student = studentSvc.findByUserId(user.getId());

try {

//word内容

String content="<html><body></body></html>";

byte b[] = content.getBytes("utf-8"); //这里是必须要设置编码的,不然导出中文就会乱码。

ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中

/*

* 关键地方

* 生成word格式

*/

POIFSFileSystem poifs = new POIFSFileSystem();

DirectoryEntry directory = poifs.getRoot();

DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);

//输出文件

String fileName="实习考核鉴定表";

request.setCharacterEncoding("utf-8");

response.setContentType("application/msword");//导出word格式

response.addHeader("Content-Disposition", "attachment;filename=" +

new String( (fileName + ".doc").getBytes(),

"iso-8859-1"));

OutputStream ostream = response.getOutputStream();

poifs.writeFilesystem(ostream);

bais.close();

ostream.close();

}catch(Exception e){

AppUtils.logError("导出出错:%s", e.getMessage());

}

}

四、怎样用Java把word文档转换为html文档

在线学习的话应该是B/S模式吧,如果楼主是想将我word内容连同样式一起转换成html有两种方法

一种是手动将要上传的word文件另存为html文件,并将html文件传进服务器,由浏览器打开就行

另一种是使用控件将要上传的word文件内容转成html代码,现在较好的控件有FCKeditor,eWebEditor,前者免费,后者精简版免费,商业版支持直接上传word文件转成html代码,不过是收费的

源码的话真的没有,本人也是最近要做类似的项目,现学现卖的~

eWebEditor主页:

FCKeditor主页:

五、java代码怎么将网页转换为doc文档

在线学习的话应该是B/S模式吧,如果楼主是想将我word内容连同样式一起转换成html有两种方法一种是手动将要上传的word文件另存为html文件,并将html文件传进服务器,由浏览器打开就行另一种是使用控件将要上传的word文件内容转成html代码,现在较好的控件有FCKeditor,eWebEditor,前者免费,后者精简版免费,商业版支持直接上传word文件转成html代码,不过是收费的源码的话真的没有,本人也是最近要做类似的项目,现学现卖的~eWebEd。

转载请注明出处51数据库 » java怎么把html转换成word

软件
前端设计
程序设计
Java相关