135-1821-9792

java将word转换为html的示例

小编给大家分享一下java将word转换为html的示例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

网站建设、成都网站建设,成都做网站公司-创新互联公司已向上1000+企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。

代码:

public static void main(String[] args) throws Exception {
    String filePath = "C:/Users/Administrator/Desktop/92个诊疗方案及临床路径/";
    File file = new File(filePath);
    File[] files = file.listFiles();
    String name = null;
    for (File file2 : files) {
        Thread.sleep(500);
        name = file2.getName().substring(0, file2.getName().lastIndexOf("."));
        System.out.println(file2.getName());
        if (file2.getName().endsWith(".docx") || file2.getName().endsWith(".DOCX")) {
            CaseHtm.docx(filePath ,file2.getName(),name +".htm");
        }else{
            CaseHtm.dox(filePath ,file2.getName(),name +".htm");
        }
    
    }
}
/**
* 转换docx
* @param filePath
* @param fileName
* @param htmlName
* @throws Exception
*/
public static void docx(String filePath ,String fileName,String htmlName) throws Exception{
    final String file = filePath + fileName;
    File f = new File(file);  
    // ) 加载word文档生成 XWPFDocument对象
    InputStream in = new FileInputStream(f);
    XWPFDocument document = new XWPFDocument(in);
    // ) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
    File imageFolderFile = new File(filePath);
    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{
    final String file = filePath + fileName;
    InputStream input = new FileInputStream(new File(file));
    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(filePath + htmlName);
    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();
}

pom.xml配置:


    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

以上是java将word转换为html的示例的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


当前名称:java将word转换为html的示例
转载源于:http://kswsj.com/article/pjgido.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号