word转换html源码,word转html(一)
一、依赖的包,部署环境
二、后台代码实现
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
*
*
【导入word文件,解析word文件转换成HTML】
*
条件:
*
备注:
*
例子:
*
日志:
*
* @author:zhu [2016年1月29日 下午2:50:28]
*/
public void importDocToHtml() {
//启动word
ActiveXComponent axc = new ActiveXComponent("Word.Application");
StringWriter stringWriter = null;
try {
// doc临时存放文件夹路径
String realpath = ServletActionContext.getServletContext().getRealPath("/UserUploadFile/WordToHTML");
File tempfile = null;
if (docFile != null) {
String tempName = String.valueOf((new Date()).getTime());
tempfile = new File(new File(realpath), tempName + ".doc");
//判断文件是否存在
if (!tempfile.getParentFile().exists()) {
//创建文件
tempfile.getParentFile().mkdirs();
}
//copy文件的创建的文件上
FileUtils.copyFile(docFile, tempfile);
//设置word不可见
axc.setProperty("Visible", new Variant(false));
Dispatch docs = axc.getProperty("Documents").toDispatch();
//打开word文档
Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
new Object[] { docFile.getPath(), new Variant(false), new Variant(true) }, new int[1])
.toDispatch();
String htmlUrl = tempfile.getPath().substring(0, tempfile.getPath().lastIndexOf(".") + 1) + "html";
//作为html格式保存到临时文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { htmlUrl, new Variant(8) }, new int[1]);
//下方如果报错,去除f参数变量
//0不保存修改 -1 保存修改 -2 提示是否保存修改
Variant f = new Variant(0);
Dispatch.call(doc, "Close", f);
//删除文件
//FileUtils.forceDelete(tempfile);
File file = new File(htmlUrl);
//读取需要注意编码
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "gbk");
BufferedReader br = new BufferedReader(isr);
String s = null;
StringBuffer html = new StringBuffer();
while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
html.append(s);
}
br.close();
Map result = new HashMap();
//因为一次读一行的原因,可以标签和属性之间没间隔,所以需要格式化
result.put("html", formatHTML(html.toString(), tempName));
// 操作成功的话,将文档id返回
Struts2Utils.outJSON(result);
}
} catch (Exception e) {
setErrMessage("导入Excel数据错误,请检查数据!");
} finally {
axc.invoke("Quit", new Variant[] {});
}
}
/**
*
*
【对当前html进行处理】
*
条件:
*
备注:如果有图片会在html同目录下生成一个存放图片的文件夹
*
例子:
*
日志:
*
* @param htmlhtml的内容
* @param htmlNamehtml文件名
* @return
* @author:zhu [2016年2月3日 下午5:01:36]
*/
private String formatHTML(String html, String htmlName) {
//截取出body中的内容
html = html.substring(html.indexOf("body"), html.lastIndexOf("body"));
html = html.substring(html.indexOf(">") + 1, html.lastIndexOf("
//对src、style、lang进行处理,可能和标签链接紧密
html = html.replaceAll("src", "\t src").replaceAll("style", "\t style").replaceAll("lang", "\t lang");
//图片需要真是的路径
html = html.replaceAll(htmlName, "../../UserUploadFile/WordToHTML/" + htmlName);
return html;
}
三、前台实现
前台主要一个上传,和获取html代码后直接赋值到编辑器上的功能。
我使用uploadify实现上传,核心代码
$(function() {
$("#fileUp").uploadify({
swf: '${request.contextPath}/resources/uploadify/uploadify.swf',
uploader: 'hdAction!importDocToHtml.shtml',// 用于接收上传文件的action
auto: true,// 是否自动开始 上传
buttonText: '导入Word', // 按钮上的文字
debug: false,// 是否调试状态
fileObjName: 'docFile',// action中的文件对象名
fileSizeLimit: (100*1024*1024), // 设置单个文件大小限制,单位为byte。设置为100m
fileTypeDesc: '支持格式:*.doc', // 如果配置了以下的'fileExt'属性,那么这个属性是必须的
fileTypeExts: '*.doc',// 允许的格式,如:*.jpg;*.gif;*.jpeg;*.png;*.bmp
method : 'post',// 上传数据的方法
multi: true,// 是否支持多文件上传
onUploadSuccess : function(file, data, response) {
var result=$.parseJSON(data);
//eWebEditor编辑器赋值
$("#eWebEditor1").contents().find("body").find("#eWebEditor").contents().find("body").html(result.html);
},
onError: function(event, queueID, fileObj) {
alert("文件:" + fileObj.name + "上传失败!");
},
onUploadError : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {// 上传文件出错是触发(每个出错文件触发一次)
alert( '上传文件出错,id: ' + file.id
+ ' \r\n- 索引: ' + file.index
+ ' \r\n- 文件名: ' + file.name
+ ' \r\n- 文件大小: ' + file.size
+ ' \r\n- 类型: ' + file.type
+ ' \r\n- 创建日期: ' + file.creationdate
+ ' \r\n- 修改日期: ' + file.modificationdate
+ ' \r\n- 文件状态: ' + file.filestatus
+ ' \r\n- 错误代码: ' + errorCode
+ ' \r\n- 错误描述: ' + errorMsg
+ ' \r\n- 简要错误描述: ' + errorString
+ ' \r\n- 出错的文件数: ' + swfuploadifyQueue.filesErrored
+ ' \r\n- 错误信息: ' + swfuploadifyQueue.errorMsg
+ ' \r\n- 要添加至队列的数量: ' + swfuploadifyQueue.filesSelected
+ ' \r\n- 添加至对立的数量: ' + swfuploadifyQueue.filesQueued
+ ' \r\n- 队列长度: ' + swfuploadifyQueue.queueLength);
},
onCancel: function(event, queueID, fileObj){
//alert("取消了" + fileObj.name);
}
});
})
word转换html源码,word转html(一)相关推荐
- 爱心树表白动画源码,word转pdf源码,websocket源码
1,爱心树表白动画源码 https://jmj.cc/s/6hr0mvj5 2,word转pdf源码 https://jmj.cc/s/6hr0mvj5 3 websocket源码 https://j ...
- c语言谷歌坐标转百度坐标,关于GPS定位经纬度到谷歌(google)坐标以及百度(百度)坐标的转换c#源码...
关于GPS定位经纬度到谷歌(google)坐标以及百度(百度)坐标的转换c#源码 2020年08月10日 阅读数:31 这篇文章主要向大家介绍关于GPS定位经纬度到谷歌(google)坐标以及百度(百 ...
- word转换html的软件,word转换为Html工具(Word Cleaner)
word转HTML转换器(Word Cleaner)将MS Word文档存为HTML网页的工具软件. 虽然 Microsfot Word 也能将 .DOC 文件另存为WEB页面格式, 但是它会增加许多 ...
- markdown转换html源码,利用Nodejs+Express将Markdown转换为HTML(附源码)
MarkdownToHtml [TOC] 将Markdown文档转换为HTML显示 说明 在程序的中,我结合了bootstrap的样式,所以稍微更改了下模块marked的源码,这也是为了显示更好.我的 ...
- OC Extension QRCodeUtil(二维码转换封装)(源码)
一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹.靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希 ...
- Android Word转HTML源码
Word是我们常用的的办公软件,广泛被运用,那么我们怎么把Word转换为网页html格式? 文件:url80.ctfile.com/f/25127180-734987515-e5f15e?p=5516 ...
- 一个简体/繁体字在线转换工具源码
<html> <head> <META HTTP-EQUIV="MSThemeCompatible" CONTENT="Yes"& ...
- 简洁UI好玩的文字转换emoji表情微信小程序支持句子词语转换_源码
介绍: 支持自定义文字输入,支持随机切换,支持句子转换: 用emoji来做暗语啥的是不是很刺激,反正至于怎么玩就看各位的脑洞了~~ 这款小程序安装方法简单,无需服务器域名与合法域名!! 直接使用微信开 ...
- opencv: 形态学 转换(图示+源码)
综述 OpenCV中的形态学转换操作有七种:腐蚀,膨胀,开运算,闭运算,形态学梯度,礼帽,黑帽. API参照表 中文名 英文名 api 原理 个人理解 腐蚀 erode erosion = cv2.e ...
最新文章
- cad一键标注闭合区域lisp_自从用了这个CAD神器,我下班总比别人早
- 设计模式学习笔记(二十二:备忘录模式)
- 【New】SAPUI5开发环境配置步骤
- iOS Safari 中click点击事件失效的解决办法
- mysql常用操作(一)
- 操作系统面试题目详解
- 上市公司行情查询站点
- 学习C语言,要从入门到精通
- android复用字符串文件夹,如何从Android Studio中的值xml字符串文件夹更改字体
- Flutter视图基础简介--Widget、Element、RenderObject
- SQL获得当前时间函数(MySQL)
- 百度信息流 绑定服务器,百度信息流账户怎么搭建?百度信息流账户搭建教程...
- 魔兽世界怀旧服务器无限刷怪点,魔兽世界怀旧服:4处无限刷新的打怪点!升级比血色站桩还快!...
- UML建模之用例图(重点讲extend和include关系)
- Python人脸识别—我的眼里只有你
- BMS(电池管理系统)第四课 ——核心!!!SOC算法开发
- 旋转矩阵、变换矩阵,李群(Lie Group)、李代数(Lie Algebra)及扰动模型
- drawio 绘制手绘素描线稿样式
- java 使用itext合并多个pdf文件成一个pdf
- 污水处理常用6种流量计
热门文章
- 51模拟器显示服务器连接失败,安装51模拟器时显示文件缺失的三种处理方法
- 将excel导入到页面表格_如何将Excel文档导入Google表格
- html5 mac os 开发工具,如何通过TextMate或命令行在Mac OS上使用W3C tidy-html5软件包?...
- 认识HTTP以及迭代历程
- 32位计算机精度,16bit、24bit和32bit有什么区别?
- Zip和7-zip谁更强,如何选择?
- 牛津大学和剑桥大学计算机科学博士专业申请要求PK
- 开源社邀请您参加亚洲自由开源软件峰会2022 (FOSSASIA SUMMIT 2022)
- A1166 Summit(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【图】
- 七牛云外链无法使用,[ 5402 ] 获取 bucket 域名失败问题