`
z276356445t
  • 浏览: 150983 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

POI读取word转换html

阅读更多
apache POI读取word文档的文档比较少,所以只有自己慢慢的摸索,这篇文章也属于比较基础入门的,主要是针对读取word中的图片,以及文字的各种样式,如有不好的地方,请各位多多指教!
/**
 * 
 */
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 += "&nbsp;";
				// 判断是否为水平制表符
				else if (currentChar == TABULATION_ASCII)
					tempString += " &nbsp;&nbsp;&nbsp;";
				// 比较前后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) {
			}
		}
	}
}


4
8
分享到:
评论
9 楼 乐乐在吗 2012-05-18  
能读出来,为什么乱码?
8 楼 pywepe 2011-08-13  
还是不对,
最后没实现是因为网站编写富文本文章的机制有限制

操作office,用java很是不爽,c#操作起来却真是很方便,谁让它是ms自家的东西
7 楼 pywepe 2011-08-13  
我想想,
不对,是图片能读取出来,但无法确定它的位置
6 楼 pywepe 2011-08-13  
这篇文章是11年的
10年年底做一个项目,用poi读取word,图片硬是读不出来
不知道这上面的代码有没有测试过?
5 楼 z276356445t 2011-07-25  
Jackie_GP 写道
能保存word全部格式吗?

不能,文章中已经说明了只能对一些文字的样式做保存.
4 楼 Jackie_GP 2011-07-16  
能保存word全部格式吗?
3 楼 txjs06 2011-06-17  
谢谢博主,已经解决,呵呵,是因为有个库没有导入。
2 楼 z276356445t 2011-06-17  
txjs06 写道
博主,你好,非常感谢你的博文,帮助很大。有个问题请教您一下,第71行Range range = new Range(i, i + 1, doc);,我这里总是提示有错误信息The type org.apache.poi.POIDocument cannot be resolved. It is indirectly referenced from required .class files.不知道是怎么回事,期待您的帮助。

错误信息提示类型无法解决,也许是word中的一些格式POI无法解析所造成的吧,因为现在的POI对word的读写支持本身就不好,你试着读取一些简单格式的word文档看还报错不.
1 楼 txjs06 2011-06-17  
博主,你好,非常感谢你的博文,帮助很大。有个问题请教您一下,第71行Range range = new Range(i, i + 1, doc);,我这里总是提示有错误信息The type org.apache.poi.POIDocument cannot be resolved. It is indirectly referenced from required .class files.不知道是怎么回事,期待您的帮助。

相关推荐

Global site tag (gtag.js) - Google Analytics