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 += " ";
// 判断是否为水平制表符
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) {
}
}
}
}
分享到:
相关推荐
利用POI将word转换成html实现在线阅读
使用poi将word读取后替换指定内容后再次生成新word,本人经过测试,拿来即用!
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
1.用户导入word生成pdf -首先导入word的时候读取word里面的全部内容。调用(ReadAndWriteDoc)然后将内容 传送到createPdf里面生成...3.WordToHtmlPO类可以将word转换成html(itext) 4.将html转换成pdf(itext)
使用POI对word文件内容进行html转换,并支持对目录进行提取,提取后保留目录原本的超链接格式,且转换后的html保留原有word样式。 适合人群:具备一定编程基础,工作1-3年的研发人员 能学到什么:可以学习到word文档...
集合了poi读取word的并转换为Html格式的功能,包含axis客户端功能,还包含job任务,里面的dmp文件为数据库文件,没什么用,可以根据自己的条件去修改,定时任务使用的是quartz技术,程序修改了数据库的话可以使用。
Java通过POI读取Excel遍历数据,根本word模板批量生成word文档,demo可运行,可根据需求修改
包含word的上传,word转换成html,转换完成后对样式的处理;html转word并导出。支持doc和docx两种格式的word文档
java poi XWPFDocument解决替换文字不完整问题 并保存原文档样式
poi-tl相关的请参考:http://deepoove.com/poi-tl/ 相关思路是先通过Jsoup将富文本Html迭代循环...想要具体的Demo请看:https://gitee.com/xuwangcheng/poi-tl-html-to-word 有疑问请加QQ群:468324085 作者:白卡pala
1.用户导入word生成pdf -首先导入word的时候读取word里面的全部内容。调用(ReadAndWriteDoc)然后将内容 传送到createPdf里面生成pdf。...3.WordToHtmlPO类可以将word转换成html(itext) 4.将html转换成pdf(itext)
Apache POI 简介是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office(Excel、WORD、PowerPoint、Visio等)格式档案读和写的功能。POI为“Poor Obfuscation Implementation...
Java读取Word中的表格(Excel),并导出文件为Excel
示例中转word有两种方法:iText和poi。转pdf用的是pd4ml,提供无水印版jar包,且中文乱码问题也有说明。
c#版本的NPOI-POI,可以利用 POI把 Word2003转换成 html,读取内容
java中读取word文档并转换成html语言,其中还包含OSS上传文件的实例
最近一直在做关于前端富文本生成的html代码转换成word的需求,对各种工具进行了尝试,对效果都不是很满意,最后从茫茫大海中找到一个非常好的工具,顾分享出来 主要转换方式 经过了一周的艰苦测试,主要通过以下三种...
在日常的开发工作中,我们时常会遇到导出Word文档... 第二种方式可以生成复杂的Word文档,但是还要进行Word转xml,xml转ftl的双重转换,不适合内容经常变更的Word文档; 第三种方式有时候不适合对格式要求严格的文档。
Android里通过POI包将word,excel等转换为html代码进行展示
使用 poi 操作 doc 与 docx 相关读写的jar包,亲测可以,支持读取文本,与 word 与 html转换的jar包