分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

word转html工具

发布时间:2023-09-06 02:25责任编辑:熊小新关键词:word
<!-- word转html -->
<dependency>
???<groupId>org.apache.poi</groupId>
???<artifactId>poi-scratchpad</artifactId>
???<version>3.10.1</version>
</dependency>
<dependency>
???<groupId>org.apache.xmlbeans</groupId>
???<artifactId>xmlbeans</artifactId>
???<version>2.6.0</version>
</dependency>
<dependency>
???<groupId>fr.opensagres.xdocreport</groupId>
???<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
???<version>1.0.6</version>
</dependency>
<dependency>
???<groupId>fr.opensagres.xdocreport</groupId>
???<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
???<version>1.0.6</version>
</dependency>


package com.vivo.magazineunlock.common.dal.common.util;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;

import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

/**
* author:ruisheng Date:2018/12/6 Time:19:23
*/
public class WordToHtml {

???public static void main(String[] args) throws Throwable {
???????final String path = "F:\\";
???????final String file = "test.doc";
???????InputStream input = new FileInputStream(path + file);
???????HWPFDocument wordDocument = new HWPFDocument(input);
???????WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
???????????DocumentBuilderFactory.newInstance().newDocumentBuilder()
???????????????.newDocument());
???????wordToHtmlConverter.setPicturesManager(new PicturesManager() {
???????????@Override public String savePicture(byte[] content, PictureType pictureType,
???????????????String suggestedName, float widthInches, float heightInches) {
???????????????return suggestedName;
???????????}
???????});
???????wordToHtmlConverter.processDocument(wordDocument);
???????List pics = wordDocument.getPicturesTable().getAllPictures();
???????if (pics != null) {
???????????for (int i = 0; i < pics.size(); i++) {
???????????????Picture pic = (Picture) pics.get(i);
???????????????try {
???????????????????pic.writeImageContent(new FileOutputStream(path
???????????????????????+ pic.suggestFullFileName()));
???????????????} catch (FileNotFoundException e) {
???????????????????e.printStackTrace();
???????????????}
???????????}
???????}
???????Document htmlDocument = wordToHtmlConverter.getDocument();
???????ByteArrayOutputStream outStream = new ByteArrayOutputStream();
???????DOMSource domSource = new DOMSource(htmlDocument);
???????StreamResult streamResult = new StreamResult(outStream);
???????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);
???????outStream.close();
???????String content = new String(outStream.toByteArray());
???????FileUtils.write(new File(path, "test.html"), content, "utf-8");
???}

}

word转html工具

原文地址:https://www.cnblogs.com/shenggege5240/p/10081006.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved