Java POI Word转html

maven 依赖

<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>fr.opensagres.xdocreport</groupId>
    <artifactId>xdocreport</artifactId>
    <version>1.0.6</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.15</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.11.3</version>
</dependency>

1: docx格式转换

2:doc格式转换

  以下方法没有转图片

/**
 * @param path 文件地址
 * @param fileName 文件名 (word名)
 * @Description docx格式Word转html
 * @Throws
 * @Return void
 * @Date 2020-04-08 19:27:55
 * @Author WangKun
 */
public static void docxConvertHtml(String path, String fileName) throws IOException {

    InputStream input = new FileInputStream(path + fileName);
    XWPFDocument document = new XWPFDocument(input);
    XHTMLOptions options = XHTMLOptions.create();
    OutputStream out = new FileOutputStream(new File(path + fileName + ".html"));
    XHTMLConverter.getInstance().convert(document, out, options);
}
/**
     * @param path 文件地址
     * @param fileName 文件名 (word名)
     * @Description doc格式word转换
     * @Throws
     * @Return void
     * @Date 2020-04-08 18:42:55
     * @Author WangKun
     */
    public static void docConvertHtml(String path, String fileName) throws Exception {

        InputStream input = new FileInputStream(new File(path + fileName));
        HWPFDocument wordDocument = new HWPFDocument(input);
        WordToHtmlConverter wordToHtmlConverter = new   WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        wordToHtmlConverter.processDocument(wordDocument);
        Document htmlDocument = wordToHtmlConverter.getDocument();
        File htmlFile = new File(path + "\\" + fileName + ".html");
        OutputStream outStream = new FileOutputStream(htmlFile);
        DOMSource domSource = new DOMSource(htmlDocument);
        StreamResult streamResult = new StreamResult(outStream);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer serializer = factory.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");
        serializer.transform(domSource, streamResult);
        outStream.close();
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值