登录  /  注册
首页 > Java > java教程 > 正文

java将word转换为html(代码)

(*-*)浩
发布: 2019-08-14 16:29:24
转载
5325人浏览过

java将word转换为html(代码)

代码:

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配置:

<dependency>
    <groupid>fr.opensagres.xdocreport</groupid>
    <artifactid>fr.opensagres.xdocreport.document</artifactid>
    <version>1.0.5</version></dependency><dependency>  
    <groupid>fr.opensagres.xdocreport</groupid>  
    <artifactid>org.apache.poi.xwpf.converter.xhtml</artifactid>  
    <version>1.0.5</version>  
</dependency>
    <dependency>
    <groupid>org.apache.poi</groupid>
    <artifactid>poi</artifactid>
    <version>3.12</version></dependency><dependency>
    <groupid>org.apache.poi</groupid>
    <artifactid>poi-scratchpad</artifactid>
    <version>3.12</version></dependency>
登录后复制

以上就是java将word转换为html(代码)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:CSDN网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号