请问各位大师:怎样用Java、POI技术来读取Word文档,并把Word文档的原来格式完整地显示在页面上?谢谢,在线恭候。

andy20020202 2009-06-03 09:24:47
请问各位大师:怎样用Java、POI技术来读取Word文档,并把Word文档的原来格式完整地显示在页面上?谢谢,在线恭候。
...全文
903 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
F1226759 2012-10-27
  • 打赏
  • 举报
回复
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream.GetField;

import org.apache.poi.hpbf.model.MainContents;
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.Range;
/**
*
* @author 张廷 下午
*
*/
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);
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);
}
}

}
















































cheniqit1 2011-03-09
  • 打赏
  • 举报
回复
windwordman 2011-03-09
  • 打赏
  • 举报
回复
经各方面调查确实不能取出WORD格式.要导出WORD只能通过HTML的方式建立WORD文档
tian_xuezhi 2009-09-12
  • 打赏
  • 举报
回复
你去读一下POI官方网站吧。POI并不能深入支持WORD,只能把WORD的基本内容读出来,什么格式之类都没法读取,更不用说表格、图片之类的东西。

因为做这个事情的人已经被微软挖走了。
wulin5050 2009-09-12
  • 打赏
  • 举报
回复
我也想知道
zings 2009-08-11
  • 打赏
  • 举报
回复
我也遇到这个问题 。。。 郁闷。。。怎么能让word中的格式不变的读取出来呢???楼主解决了给个方法啊!!!!我读出来的都没有格式。。。。
andy20020202 2009-06-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 JackRui2008 的回复:]
利用POI在导出excel的时候用
response.setContentType("application/vnd.ms-excel");
导出word的时候用
response.setContentType("application/msword");
[/Quote]

[color=#800080]不过,还是谢谢美女的帮助,难得见到一大美女!!![/color]
andy20020202 2009-06-03
  • 打赏
  • 举报
回复
这个不行吧,美女,这个好像是下载吧?
JackRui2008 2009-06-03
  • 打赏
  • 举报
回复
利用POI在导出excel的时候用
response.setContentType("application/vnd.ms-excel");
导出word的时候用
response.setContentType("application/msword");



andy20020202 2009-06-03
  • 打赏
  • 举报
回复
这个方法我试过了,但读取出来的仍然只是纯文本格式 的,没有word本身的格式。
sprite26 2009-06-03
  • 打赏
  • 举报
回复
public static String readDoc(String doc) throws Exception {
// 创建输入流读取DOC文件
FileInputStream in = new FileInputStream(new File(doc));
WordExtractor extractor = null;
String text = null;
// 创建WordExtractor
extractor = new WordExtractor();
// 对DOC文件进行提取
text = extractor.extractText(in);
return text;
}

public static void main(String[] args) {
try{
String text = WordReader.readDoc("c:/test.doc");
System.out.println(text);
}catch(Exception e){
e.printStackTrace();
}
}

网上搜了一个,你先看下能用不,俺没试过,http://www.ibiblio.org/maven2/org/textmining/tm-extractors/0.4/还要添加个这个扩展包
andy20020202 2009-06-03
  • 打赏
  • 举报
回复
谢谢关注

可以读取的,继续关注。
sprite26 2009-06-03
  • 打赏
  • 举报
回复
POI无法读取word文档,只能读取excel,JXL好像EXCEL和word都可以读取
andy20020202 2009-06-03
  • 打赏
  • 举报
回复
继续等待高手指点呀/

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧