`

openOffice 把word转换成html

    博客分类:
  • Java
阅读更多
  最近项目中需要把word转换成html格式,直接通过Java代码转化,出来的格式对不上号,网上找到有一些方法,可以接触一下插件或者第三方jar可以进行转换,这里先说说,使用openOffices 转换的过程以及使用.

1、下载安装OpenOffice.org 3.4.1,安装过程略过。
   官网地址:http://download.openoffice.org/index.html
2、下载第三方包Jodconverter.jar,开启OpenOffice进行格式转化
  官网地址:[url] http://www.artofsolving.com/opensource/jodconverter[/url]

3、一切准备工作就绪开始动手时间,

首先,启动端口:
进入dos, cd  C:\Program Files (x86)\OpenOffice.org 3\program
启动命令:soffice -headless -accept="socket,port=8100;urp;


package com.artofsolving.jodconverter.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

/**
 * 
 * 端口启动命令:
 * soffice -headless -accept="socket,port=8100;urp;
 * C:\Program Files (x86)\OpenOffice.org 3\program
 * 
 * 将Word文档转换成html字符串的工具类
 * 
 * 
 */

public class Test {

	public static void main(String[] args) {
		toHtmlString(new File("C:\\Users\\Administrator\\Desktop\\a111.doc"), "E:/test");
	}

	/**
	 * 将word文档转换成html文档
	 * @param docFile   需要转换的word文档
	 * @param filepath  转换之后html的存放路径
	 * @return 转换之后的html文件
	 */
	public static File convert(File docFile, String filepath) {

		// 创建保存html的文件
		File htmlFile = new File(filepath + "/" + new Date().getTime() + ".html");
		// 创建Openoffice连接
		OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);
		try {
			// 连接
			con.connect();
		} catch (ConnectException e) {
			System.out.println("获取OpenOffice连接失败...");
			e.printStackTrace();
		}
		
		// 创建转换器
		DocumentConverter converter = new OpenOfficeDocumentConverter(con);
		// 转换文档问html
		converter.convert(docFile, htmlFile);
		// 关闭openoffice连接
		con.disconnect();
		return htmlFile;
	}

	/**
	 * 
	 * 将word转换成html文件,并且获取html文件代码。
	 * @param docFile  需要转换的文档
	 * @param filepath  文档中图片的保存位置
	 * @return 转换成功的html代码
	 */
	public static String toHtmlString(File docFile, String filepath) {
		// 转换word文档
		File htmlFile = convert(docFile, filepath);
		
		/*try {
			FileUtils.copyFile(docFile,new File("e://ee.doc"));
			FileInputStream input = FileUtils.openInputStream(docFile);
			FileOutputStream output = new FileOutputStream(new File("e://ff.doc"));
			IOUtils.copyLarge(input, output);
		
		} catch (IOException e1) {
			e1.printStackTrace();
		}*/
		// 获取html文件流
		StringBuffer htmlSb = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(htmlFile),Charset.forName("gb2312")));
			while (br.ready()) {
				htmlSb.append(br.readLine());
			}
			br.close();
			// 删除临时文件
			htmlFile.delete();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// HTML文件字符串
		String htmlStr = htmlSb.toString();
		System.out.println("htmlStr=" + htmlStr);
		// 返回经过清洁的html文本
		return clearFormat(htmlStr, filepath);
	}

	/**
	 * 
	 * 清除一些不需要的html标记
	 * 
	 * 
	 * 
	 * @param htmlStr
	 * 
	 *            带有复杂html标记的html语句
	 * 
	 * @return 去除了不需要html标记的语句
	 */

	protected static String clearFormat(String htmlStr, String docImgPath) {

		// 获取body内容的正则
		String bodyReg = "<BODY .*</BODY>";
		Pattern bodyPattern = Pattern.compile(bodyReg);
		Matcher bodyMatcher = bodyPattern.matcher(htmlStr);
		if (bodyMatcher.find()) {
			// 获取BODY内容,并转化BODY标签为DIV
			htmlStr = bodyMatcher.group().replaceFirst("<BODY", "<DIV").replaceAll("</BODY>", "</DIV>");
		}

		// 调整图片地址
		htmlStr = htmlStr.replaceAll("<IMG SRC=\"", "<IMG SRC=\"" + docImgPath + "/");
		// 把<P></P>转换成</div></div>保留样式
		// content = content.replaceAll("(<P)([^>]*>.*?)(<\\/P>)",
		// "<div$2</div>");
		// 把<P></P>转换成</div></div>并删除样式
		htmlStr = htmlStr.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>");
		// 删除不需要的标签
		htmlStr = htmlStr.replaceAll("<[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^>]*?>","");
		// 删除不需要的属性
		htmlStr = htmlStr.replaceAll("<([^>]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>","<$1$2>");

		return htmlStr;

	}

}
1
2
分享到:
评论
11 楼 静心丶净心丶尽心 2016-01-19  
楼主你好: word在转换html 过程中  word中的图片样式“大小或者转换的结果跟原图不一样”有没有什么解决方式
10 楼 yuming.xiao 2015-05-08  
博主你好,请问你有没有遇到xml类型的word,这种word用这种方式转换不出来,转出来的全是一堆xml,求解决
9 楼 honglulu06 2013-08-09  
转换的时候格式是完全按照word里面的格式吗?如果有页眉页脚还有版记的话还能格式正常吗
8 楼 jadethao 2013-03-14  
jadethao 写道
rubskin 写道
有没有可以只依赖jar包的能转换word,excel,ppt的,而不是基于服务或dll那样的


有,可以用jacob.

7 楼 jadethao 2013-03-14  
rubskin 写道
有没有可以只依赖jar包的能转换word,excel,ppt的,而不是基于服务或dll那样的


有,可以用jacobe.
6 楼 cs3793 2013-03-01  
jadethao 写道
cs3793 写道
为什么按照你说的我的运行不了呢?能给我发一个例子吗?

这代码就是一个例子,你看看是否加载jar包,还有是否启动openOffice监听端口。

他需要哪些JAR包啊?能加载的我都加上了
5 楼 rubskin 2013-03-01  
有没有可以只依赖jar包的能转换word,excel,ppt的,而不是基于服务或dll那样的
4 楼 jadethao 2013-02-28  
asialee 写道
openoffice的转换有时候格式有问题!

发现什么问题了,还望指教。
3 楼 jadethao 2013-02-28  
cs3793 写道
为什么按照你说的我的运行不了呢?能给我发一个例子吗?

这代码就是一个例子,你看看是否加载jar包,还有是否启动openOffice监听端口。
2 楼 asialee 2013-02-28  
openoffice的转换有时候格式有问题!
1 楼 cs3793 2013-02-28  
为什么按照你说的我的运行不了呢?能给我发一个例子吗?

相关推荐

    使用openoffice把word转化成html

    使用openoff把word转化成html,详细步骤

    OpenOffice转换Office文档为PDF、HTML

    OpenOffice转换Office文档为PDF、HTML,将word,ppt转成html,Excel转为Html

    利用OpenOffice对html、word、pdf进行转换

    NULL 博文链接:https://zhang-637.iteye.com/blog/1487556

    c#使用openoffice组件操作文件

    c#使用openoffice组件操作文件,各主流文件之间的转换,word,html,excel,pdf等等

    将word、excel、ppt、html、txt、pdf转换成图片

    解决通过OpenOffice如何将word、excel、ppt、html、txt转换成pdf 解决如何将pdf转换成图片 解决如何将word、excel、ppt、html、txt转换成图片,之前有人传过,但不能运行,现在传个自己的,可以完美转换,谁下谁知道...

    OpenOffice相关文件.zip

    OpenOffice相关文件,内含OpenOffice的Linux版本安装包以及支持转换docx的jodconverter-2.2.2的jar包【该jar包无法通过Maven仓库下载】。

    解决linux下openoffice word文件转PDF中文乱码的问题

    下面小编就为大家带来一篇解决linux下openoffice word文件转PDF中文乱码的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    jave借助于JodConverter对openoffice组件进行文件转换

    使用jodconverter操作类库对openoffice组件进行文件转换操作,文件格式转换种类繁多,包括word,html,excel,pdf,图片,xps等一些常见文件格式的相互转换,初学者可以用来参考,具体详情请查看源代码

    在线预览word/excel/ppt

    Java实现在线预览word/excel/ppt,在网上找了很多的方式,有将word/excel/ppt转换为HTML的,还有转swf的,但是转swf的偏多,转HTML有些可是不好,转swf的效果好一些,但是在转excel的时候,默认是已A4的纸张来进行...

    PDF转WordExcelPPT

    AnyBizSoft PDF Converter 是一个专业的PDF转换器,支持将PDF转换为 Word (DOC/DOCX)、PowerPoint (PPT/PPTX)、Excel (XLS/XLSX)、HTML、TXT 等格式。 经测试 AnyBizSoft PDF Converter 对中文字符(简繁中文)支持...

    openofficedemo

    解决如何将word、excel、ppt、html、txt转换成pdf 解决如何将pdf转换成图片 解决如何将word、excel、ppt、html、txt转换成图片

    pandoc文档转换利器

    通用文档转换器Pandoc ,可以把markdown、 reStructuredText、 textile、 HTML、或者LaTeX转换成: HTML格式: XHTML, HTML5, 以及HTML幻灯片Slidy, S5,或者DZSlides. 文字处理软件格式: Microsoft Word docx, ...

    txt,word xls转pdf pdf转swf

    txt,word xls转pdf转换pdf用到外部工具OpenOffice pdf转换swf 用到外部工具swftools 我对这个俩个工具进行了代码调用,执行引用jar,变可以调用。 文件转换pdf的时候需要jar包 下载地址:...

    文档格式转换工具Pandoc.zip

    Word processor formats: Microsoft Word docx, OpenOffice/LibreOffice ODT, OpenDocument XML Ebooks: EPUB Documentation formats: DocBook, GNU TexInfo, Groff man pages TeX formats: LaTeX, ConTeXt, ...

    jodconverter包

    使用openOffice,将word转换长HTML、PDF时所需的包。里面包括jodconverter2、和jodconverter3

    CyberArticle 网文快捕 doc2help

    和使用office自己生成的html或者一般的word转换软件不同,使用doc2help生成的帮助具有如下特色:  1. 能够根据章节自动生成目录;  2. 能够把很大的帮助拆成多个页面,避免太大的文档打开困难;  3. 特别的,...

    Spring boot 在线预览办公文件(doc、docx、xls等)

    Spring boot 在线预览办公文件(doc、docx、xls、xlsx、pdf等),转化效果个人觉得比较理想

    docx odt等文件转换器

    A program to convert Word 2007 Docx and Dotx, OpenOffice Sxw and Odt files to the universal rtf format without needing MS Office 2007 or OpenOffice installed. Docx2Rtf can also open, view and print ...

    word ,xls txt转pdf ,pdf转换 swf 工具类

    对 swftools(pdf转换swf)工具和OpenOffice工具的调用进行代码封装 工具下载地址 OpenOffice http://www.javaworld.com.tw/jute/post/view?bid=11&id=248862 swftools ...

    jodconverter-2.2.2.jar和jodconverter-cli-2.2.2.jar

    linux和windows安装openOffice java通过jodconverter 将excel、doc文件转成pdf或html,比2.2.1版本相比 提供office 2007版本支持 maven是只有2.2.1版本的maven依赖; maven 安装jar包到本地命令 mvn install:install-...

Global site tag (gtag.js) - Google Analytics