Java实现将word转换为html的方法示例【doc与docx格式】

网友投稿 733 2023-01-12


Java实现将word转换为html的方法示例【doc与docx格式】

本文实例讲述了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 outhttp:// = 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();

}

fr.opensagres.xdocreport

fr.opensagres.xdocreport.document

1.0.5

fr.opensagres.xdocreport

org.apache.poi.xwpf.converter.xhtml

1.0.5</version>

org.apache.poi

poi

3.12

org.apache.poi

poi-scratchpad

3.12

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口测试用例的步骤(接口测试用例方法)
下一篇:研发管理平台文案范文模板(产品发布文案模板)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~