zoukankan      html  css  js  c++  java
  • word转html工具

    <!-- 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");
    }

    }
  • 相关阅读:
    循环播放音乐
    在发板实现24位jpg和bmp图片用手划动显示上一张与下一张图片
    AnsiIO
    PosixIO
    java3
    java2
    java1
    ios音乐播放器demo
    添加文章查看连接
    elementary os变成mac风(笔记)
  • 原文地址:https://www.cnblogs.com/shenggege5240/p/10081006.html
Copyright © 2011-2022 走看看