POM依赖
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
Doc转Html
@Test
public void Word2003ToHtml() throws IOException, ParserConfigurationException, TransformerException {
FileInputStream is = new FileInputStream(new File("D:\\BHFAE\\国金所\\MQ\\Websphere-MQ入门经典教程---经典.doc"));
HWPFDocument wordDocument = new HWPFDocument(is);
WordToHtmlConverter converter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//对HWPFDocument进行转换
converter.setPicturesManager(new PicturesManager() {
@Override
public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
String type = pictureType.name();
return "data:image/" + type + ";base64," + new String(Base64.encodeBase64(content));
}
});
converter.processDocument(wordDocument);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
//是否添加空格
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "html");
File file = new File("H:/testDocToHtml.html");
FileOutputStream fileOutputStream = new FileOutputStream(file);
transformer.transform(
new DOMSource(converter.getDocument()),
new StreamResult(fileOutputStream));
fileOutputStream.flush();
fileOutputStream.close();
wordDocument.close();
}
Docx转Html
@Test
public void Word2007ToHtml() throws IOException {
FileInputStream is = new FileInputStream(new File("D:\\BHFAE\\国金所\\MQ\\Websphere-MQ入门经典教程---经典.docx"));
XWPFDocument document = new XWPFDocument(is);
List<XWPFPictureData> list = document.getAllPictures();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XHTMLConverter.getInstance().convert(document, outputStream, null);
String s = new String(outputStream.toByteArray());
s = setImg(s, list);
File file = new File("H:/testDocxToHtml.html");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(s.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
document.close();
}
private String setImg(String html, List<XWPFPictureData> list){
Document doc = Jsoup.parse(html);
Elements elements = doc.getElementsByTag("img");
if (elements != null && elements.size() > 0 && list != null){
for(Element element : elements){
String src = element.attr("src");
for (XWPFPictureData data: list){
if (src.contains(data.getFileName())){
String type = src.substring(src.lastIndexOf(".") + 1);
String base64 = "data:image/" + type + ";base64," + new String(Base64.encodeBase64(data.getData()));
element.attr("src", base64);
break;
}
}
}
}
return doc.toString();
}