使用Java的POI工具进行Word的DOC文档转为HTML页面技术简介
1. 下载POI工具并引用。
2. 读取整个doc文档,获得该文档的所有字符串。
3. 从该字符串中得到标题,把该标题构成一个HTML格式的字符串,如<html><head><title>测试文档</title></head><body>。
4. 从该文档中判断是否有表格,如有,把每个表格的开始偏移量,结束偏移量记录下来,同时根据每个表格的行,列读取表格的内容,并构造出表格的HTML字符串。
5. 从该字符串的第一个字符开始逐个字符循环,得到字符的字体,字号大小,直到下一个字符的字体,字号不一样时,把这些字符内容构造成一个HTML格式的字符串。
6. 如果碰到字符为回车符,制表符,把回车符,制表符构造成HTML格式的字符串。
7. 如果碰到字符为图片,读取图片,把图片放在指定路径,再把这一路径的信息构造成HTML字符串,如<img src='c://test//1.jpg'/>。
8. 如读取字符串的位置等于表格的开始偏移量时,插入前面一构造出的表格HTML字符串,同时跳过表格的结束偏移量,继续往下循环读取字符。
9. 由于以上读取是按字符串逐个读取,并且根据字符的变化同时构造出HTML字符串,所以当字符串读取完毕后,即构造出一个完整的HTML字符串。、
10. 举例源代码:
/**
* POI读取word转换html
* 引用自http://z276356445t.iteye.com/blog/963950
*/
package com.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range;
/**
*
* @author 张廷 下午10:36:40
*
*/
public class WordToHtml {
/**
* 回车符ASCII码
*/
private static final short ENTER_ASCII = 13;
/**
* 空格符ASCII码
*/
private static final short SPACE_ASCII = 32;
/**
* 水平制表符ASCII码
*/
private static final short TABULATION_ASCII = 9;
private String htmlText = "";
/**
* 读取每个文字样式
*
* @param fileName
* @throws Exception
*/
public void getWordAndStyle(String fileName) throws Exception {
FileInputStream in = new FileInputStream(new File(fileName));
HWPFDocument doc = new HWPFDocument(in);
// 取得文档中字符的总数
int length = doc.characterLength();
// 创建图片容器
PicturesTable pTable = doc.getPicturesTable();
htmlText = "<html><head><title>" + doc.getSummaryInformation().getTitle() + "</title></head><body>";
// 创建临时字符串,好加以判断一串字符是否存在相同格式
String tempString = "";
for (int i = 0; i < length - 1; i++) {
// 整篇文章的字符通过一个个字符的来判断,range为得到文档的范围
Range range = new Range(i, i + 1, doc);
CharacterRun cr = range.getCharacterRun(0);
if (pTable.hasPicture(cr)) {
// 读写图片
this.readPicture(pTable, cr);
} else {
Range range2 = new Range(i + 1, i + 2, doc);
// 第二个字符
CharacterRun cr2 = range2.getCharacterRun(0);
// 当前字符
char currentChar = cr.text().charAt(0);
// 判断是否为回车符
if (currentChar == ENTER_ASCII)
tempString += "<br/>";
// 判断是否为空格符
else if (currentChar == SPACE_ASCII)
tempString += " ";
// 判断是否为水平制表符
else if (currentChar == TABULATION_ASCII)
tempString += " ";
// 比较前后2个字符是否具有相同的格式
boolean flag = compareCharStyle(cr, cr2);
String fontStyle = "<span style='font-family:" + cr.getFontName() + ";font-size:" + cr.getFontSize() / 2 + "pt;";
if (cr.isBold())
fontStyle += "font-weight:bold;";
if (cr.isItalic())
fontStyle += "font-style:italic;";
if (flag && i != length - 2)
tempString += currentChar;
else if (!flag) {
htmlText += fontStyle + "'>" + tempString + currentChar + "</span>";
tempString = "";
} else
htmlText += fontStyle + "'>" + tempString + currentChar + "</span>";
}
htmlText += "</body></html>";
this.writeFile(htmlText);
}
/**
* 读写文档中的图片
*
* @param pTable
* @param cr
* @throws Exception
*/
private void readPicture(PicturesTable pTable, CharacterRun cr) throws Exception {
// 提取图片
Picture pic = pTable.extractPicture(cr, false);
// 返回POI建议的图片文件名
String afileName = pic.suggestFullFileName();
OutputStream out = new FileOutputStream(new File("g:\\test" + File.separator + afileName));
pic.writeImageContent(out);
htmlText += "<img src='g:\\test\\" + afileName + "'/>";
}
private boolean compareCharStyle(CharacterRun cr1, CharacterRun cr2) {
boolean flag = false;
if (cr1.isBold() == cr2.isBold() && cr1.isItalic() == cr2.isItalic() && cr1.getFontName().equals(cr2.getFontName()) && cr1.getFontSize() == cr2.getFontSize()) {
flag = true;
}
return flag;
}
/**
* 写文件
*
* @param s
*/
private void writeFile(String s) {
FileOutputStream fos = null;
BufferedWriter bw = null;
try {
File file = new File("g:\\abc.html");
fos = new FileOutputStream(file);
bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write(s);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fos != null)
fos.close();
} catch (IOException ie) {
}
}
}
}