將 Word 文檔轉換為 HTML 的Java工具類
第一:下載必備工具OpenOffice.org 3.2 (zh-CN) Installation Files 使用此工具 主要是 使用 其內部服務。
如果想要 以工具形式 轉換, 此8100服務必須 的開啦,要不 不能轉換的啦。
第二:下載 jodconverter-2.2.2.zip 包, 該包中的 lib 包下的 jar 包 都是 為 轉換準備的。
第三:下載了 不用是不對i地,默認 安裝 OpenOffice.org 3.2 程序, 有的個別 殺毒軟件 回報一些 警告型阻止 提示 , 直接 給 添加 到信任服務即可。
第四:安裝完畢之后呢, 當然 是要 開啟 服務了, 安裝 OpenOffice.org 3.2 的 目的 就是 為了 使用 其中的 8100 服務地,
打開 運行文本框中 輸入 cmd 進入 命令窗口, 進入到 你的 默認安裝路徑
【提示一下,一般都會在C:\Program Files\OpenOffice.org 3\program這個路徑下,
但是 64位系統中會在 C:\Program Files(x86)\OpenOffice.org 3\program 的這個路徑下 ,
其區別 就是 多了一個 (x86),我自己用的 是 64位的Win7系統,后來發現安裝好多軟件
都不兼容,郁悶的不得了,現在用的是公司配了 32位的Win7系統。】
依次使用如下兩條命令即可:
命令1:cd C:\Program Files\OpenOffice.org 3\program
命令2:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
執行玩 這兩條命令之后 , 8100 服務就會 被打開了 , 你也可以 使用 netstat -an 這個 命令 查看 8100 端口 是否 真的 被打開了。
打開 MyEclipse6.5 , 開發到現在 幾乎 MyEclipse這些 版本 差不多 都用過了, 但是后期 還是 覺得 這個 6.5 比較好用,
8.5之后 機器 卡的的不行, 配置 好的機器還可以,配置 稍微 差點的 , 你跑起來 就是到有多么的杯具了。
呃~~~~~好似 又脫離正題了,我們繼續哈.... 在其中建立一個公共類:名為WordToHtml.java 代碼共享如下 :package com.stars.windpowersystem.util;
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.util.Date;
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;
/**
* 描述: TODO 將Word文檔轉換成html字符串的工具類
*
* @類名稱: WordToHtml
* @作者: 宋延軍
* @郵箱: songyanjun_stars@126.com
* @日期: Feb 9, 2012 10:07:21 AM
*/
public class WordToHtml {
/**
* 描述: TODO 調用測試
* @標題: main
* @設定: @param args
* @返回類型: void
*/
public static void main(String[] args) {
Long time1 = System.currentTimeMillis();
Long time2 = System.currentTimeMillis();
System.out.println(
toHtmlString(new File("C:/Users/Administrator/Desktop/風電可靠性信息管理系統/01_需求分析文檔/風電可靠性管理信息系統需求分析文檔V5.0(最新).doc"), "C:/Users/Administrator/Desktop/風電可靠性信息管理系統/01_需求分析文檔/tempDir"));
System.out.println("執行效率為:"+(time2-time1));
}
/**
* 將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);
// 獲取html文件流
StringBuffer htmlSb = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(htmlFile)));
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();
// 返回經過清潔的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;
}
}
來自:http://blog.csdn.net/songyanjun2011/article/details/7244767