没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Java实现将实现将word转换为转换为html的方法示例【的方法示例【doc与与docx格式】格式】
主要介绍了Java实现将word转换为html的方法,结合实例形式分析了java针对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 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();
}
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
资源评论
weixin_38633157
- 粉丝: 5
- 资源: 969
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功