java word转成html

package com;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
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.*;

public class TestExport {

public static void main(String[] args) throws Exception {
    String filePath = "C:\\Users\\hasee\\Desktop\\图片测试.docx";
    File file = new File(filePath);

        Thread.sleep(500);
        String name = file.getName().substring(0, file.getName().lastIndexOf("."));
        System.out.println(file.getName());
        if (file.getName().endsWith(".docx") || file.getName().endsWith(".DOCX")) {
            TestExport.docx(filePath ,file.getName(),name +".htm");
        }else{
            TestExport.dox(filePath ,file.getName(),name +".htm");
        }


}
/**
 * 转换docx
 * @param filePath
 * @param fileName
 * @param htmlName
 * @throws Exception
 */
public static void docx(String filePath ,String fileName,String htmlName) throws Exception{
    File f = new File(filePath);

// ) 加载word文档生成 XWPFDocument对象
InputStream in = new FileInputStream(f);
XWPFDocument document = new XWPFDocument(in);
// ) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
File imageFolderFile = new File(“C:\Users\hasee\Desktop\图片测试copy.docx”);
XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
options.setExtractor(new FileImageExtractor(imageFolderFile));
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
// ) 将 XWPFDocument转换成XHTML
OutputStream out = new FileOutputStream(new File(filePath + htmlName));
XHTMLConverter.getInstance().convert(document, out, options);
}
/**
* 转换doc
* @param filePath
* @param fileName
* @param htmlName
* @throws Exception
*/
public static void dox(String filePath ,String fileName,String htmlName) throws Exception{
InputStream input = new FileInputStream(new File(filePath));
HWPFDocument wordDocument = new HWPFDocument(input);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//解析word文档
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
File htmlFile = new File(“C:\Users\hasee\Desktop\图片测试copy.docx”);
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();
}
}



依赖

fr.opensagres.xdocreport fr.opensagres.xdocreport.document 1.0.5 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.xhtml 1.0.5 org.apache.poi poi 3.12 org.apache.poi poi-scratchpad 3.12 ```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值