poi word2007转换成html,poi的word转html,如何显示修订内容的最终状态

探讨使用Apache POI将Word文档(.doc)转换为HTML时遇到的问题,修订内容未正确呈现为最终状态,及标题控制和乱码等问题。
部署运行你感兴趣的模型镜像

poi的word转html,怎么显示修订内容的最终状态?

问题描述:使用poi将.doc转换成html时,发现其将修订前的内容也显示出来了,(另外html的标题无法控制,有时候会出现乱码)

操作如下

1编辑测试文档:修订状态(附件中可以下载该doc文档)

15255711.jpeg

2期望显示效果:最终状态

15255712.jpg

3转换成html的效果:在修订中删除的内容会显示出来(html的title显示内容不是我想要的)

15255713.jpg

实现代码

public class Word2Html {

public static void main(String argv[]) {

try {

//word 路径       html输出路径

convert2Html("D:/doctohtml/1.doc","D:/doctohtml/1.html");

} catch (Exception e) {

e.printStackTrace();

}

}

public static void writeFile(String content, String path) {

FileOutputStream fos = null;

BufferedWriter bw = null;

try {

File file = new File(path);

fos = new FileOutputStream(file);

bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8"));

bw.write(content);

} 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) {

}

}

}

public static void convert2Html(String fileName, String outPutFile)

throws TransformerException, IOException,

ParserConfigurationException {

HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));

WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(

DocumentBuilderFactory.newInstance().newDocumentBuilder()

.newDocument());

wordToHtmlConverter.setPicturesManager( new PicturesManager()

{

public String savePicture( byte[] content,

PictureType pictureType, String suggestedName,

float widthInches, float heightInches )

{

                 //html 中  图片标签中 显示的图片路路径  

return "d:/doctohtml/"+suggestedName;

}

} );

wordToHtmlConverter.processDocument(wordDocument);

//save pictures

List pics=wordDocument.getPicturesTable().getAllPictures();

if(pics!=null){

for(int i=0;i

Picture pic = (Picture)pics.get(i);

System.out.println();

try {

//word中图片的存储路径

pic.writeImageContent(new FileOutputStream("D:/doctohtml/"

+ pic.suggestFullFileName()));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

Document htmlDocument = wordToHtmlConverter.getDocument();

ByteArrayOutputStream out = new ByteArrayOutputStream();

DOMSource domSource = new DOMSource(htmlDocument);

StreamResult streamResult = new StreamResult(out);

TransformerFactory tf = TransformerFactory.newInstance();

Transformer serializer = tf.newTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");

serializer.setOutputProperty(OutputKeys.INDENT, "yes");

serializer.setOutputProperty(OutputKeys.METHOD, "html");

serializer.transform(domSource, streamResult);

out.close();

writeFile(new String(out.toByteArray()), outPutFile);

}

}

doc文件的下载链接(在最下面):http://www.iteye.com/problems/113815

------解决思路----------------------

跟踪源码看看。

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.9

TensorFlow-v2.9

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值