java poi xml转word文档_使用POI转换word doc文件
目錄
1???????轉(zhuǎn)換為Html文件
2???????轉(zhuǎn)換為Xml文件
3???????轉(zhuǎn)換為Text文件
在POI中還存在有針對于word doc文件進(jìn)行格式轉(zhuǎn)換的功能。我們可以將word的內(nèi)容轉(zhuǎn)換為對應(yīng)的Html文件,也可以把它轉(zhuǎn)換為底層用來描述doc文檔的xml文件,還可以把它轉(zhuǎn)換為底層用來描述doc文檔的xml格式的text文件。這些格式轉(zhuǎn)換都是通過AbstractWordConverter特定的子類來完成的。
1???????轉(zhuǎn)換為Html文件
將doc文檔轉(zhuǎn)換為對應(yīng)的Html文檔是通過WordToHtmlConverter類進(jìn)行的。它會盡量的利用Html的方式來呈現(xiàn)原文檔的樣式。示例代碼:
Java代碼
/**
*?Word轉(zhuǎn)換為Html
*?@throws?Exception
*/
@Test
public?void?testWordToHtml()?throws?Exception?{
InputStream?is?=?new?FileInputStream("D:\\test.doc");
HWPFDocument?wordDocument?=?new?HWPFDocument(is);
WordToHtmlConverter?converter?=?new?WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//對HWPFDocument進(jìn)行轉(zhuǎn)換
converter.processDocument(wordDocument);
Writer?writer?=?new?FileWriter(new?File("D:\\converter.html"));
Transformer?transformer?=?TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(?OutputKeys.ENCODING,?"utf-8"?);
//是否添加空格
transformer.setOutputProperty(?OutputKeys.INDENT,?"yes"?);
transformer.setOutputProperty(?OutputKeys.METHOD,?"html"?);
transformer.transform(
new?DOMSource(converter.getDocument()?),
new?StreamResult(?writer?)?);
}
2???????轉(zhuǎn)換為Xml文件
將doc文檔轉(zhuǎn)換為對應(yīng)的Xml文件是通過WordToFoConverter類進(jìn)行的。它可以把doc文檔轉(zhuǎn)換為底層用來描述doc文檔的Xml文檔。示例代碼:
Java代碼
/**
*?Word轉(zhuǎn)Fo
*?@throws?Exception
*/
@Test
public?void?testWordToFo()?throws?Exception?{
InputStream?is?=?new?FileInputStream("D:\\test.doc");
HWPFDocument?wordDocument?=?new?HWPFDocument(is);
WordToFoConverter?converter?=?new?WordToFoConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//對HWPFDocument進(jìn)行轉(zhuǎn)換
converter.processDocument(wordDocument);
Writer?writer?=?new?FileWriter(new?File("D:\\converter.xml"));
Transformer?transformer?=?TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(?OutputKeys.ENCODING,?"utf-8"?);
//是否添加空格
transformer.setOutputProperty(?OutputKeys.INDENT,?"yes"?);
//?????transformer.setOutputProperty(?OutputKeys.METHOD,?"html"?);
transformer.transform(
new?DOMSource(converter.getDocument()?),
new?StreamResult(?writer?)?);
}
3???????轉(zhuǎn)換為Text文件
將doc文檔轉(zhuǎn)換為text文檔是通過WordToTextConverter來進(jìn)行的。它可以把doc文檔轉(zhuǎn)換為底層用于描述doc文檔的Xml格式的text文檔。示例代碼:
Java代碼
/**
*?Word轉(zhuǎn)換為Text
*?@throws?Exception
*/
@Test
public?void?testWordToText()?throws?Exception?{
InputStream?is?=?new?FileInputStream("D:\\test.doc");
HWPFDocument?wordDocument?=?new?HWPFDocument(is);
WordToTextConverter?converter?=?new?WordToTextConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//對HWPFDocument進(jìn)行轉(zhuǎn)換
converter.processDocument(wordDocument);
Writer?writer?=?new?FileWriter(new?File("D:\\converter.txt"));
Transformer?transformer?=?TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(?OutputKeys.ENCODING,?"utf-8"?);
//是否添加空格
transformer.setOutputProperty(?OutputKeys.INDENT,?"yes"?);
transformer.setOutputProperty(?OutputKeys.METHOD,?"text"?);
transformer.transform(
new?DOMSource(converter.getDocument()?),
new?StreamResult(?writer?)?);
}
(注:本文是基于poi3.9所寫)
總結(jié)
以上是生活随笔為你收集整理的java poi xml转word文档_使用POI转换word doc文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue移动端项目模板h5
- 下一篇: html meta页面自适应,【转载·收