建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 网站开发 > Java教程 >

java将word转换为html(代码)

时间:2019-08-14 17:02来源:PHP中文网 作者:猪哥 点击:
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>
(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片