Word转HTML

Word转HTML

把Work生成的HTML中的代码进行简化处理

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WORD转HTML</title>
<style>
body{ font-size:12px; padding:20px; margin:0px;}
p{ font-size:12px; margin:0px; line-height:25px; padding:0px;}
table{ border-left:1px solid #CCCCCC; border-top:1px solid #CCCCCC;}
table td{ border-right:1px solid #CCCCCC; border-bottom:1px solid #CCCCCC;}
</style>
<script language="javascript" type="text/javascript">
//运行文本域代码
function runEx(cod1)  {
 cod=document.all(cod1)
  var code=cod.value;
  if (code!=""){
  var newwin=window.open('','','');  //打开一个窗口并赋给变量newwin。
  newwin.opener = null // 防止代码对论谈页面修改
  newwin.document.write(code);  //向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
  newwin.document.close();
}
}
</script>
</head>
<body>
<script>
function GetClipboardHTML()
{
var oDiv = document.getElementById("divTemp")
oDiv.innerHTML = "" ;
var oTextRange = document.body.createTextRange() ;
oTextRange.moveToElementText(oDiv) ;
oTextRange.execCommand("Paste") ;
var sData = oDiv.innerHTML ;
oDiv.innerHTML = "" ;
return sData ;
}
function cleanAndPaste( html )
{
// Remove all SPAN tags
html = html.replace(/<\/?SPAN[^>]*>/gi, "" );
// Remove all B tags
html = html.replace(/<\/?B[^>]*>/gi, "" );
// Remove all COLGROUP tags
html = html.replace(/<\/?COLGROUP[^>]*>/gi, "" );
html = html.replace(/<\/?COL[^>]*>/gi, "" );
// Remove all td tags
html = html.replace(/<td[^>]*>/gi, "<td>" );
html = html.replace(/TD/gi, "td" );
html = html.replace(/TR/gi, "tr" );
html = html.replace(/TABLE/gi, "table" );
html = html.replace(/<\/?TBODY[^>]*>/gi, "" );
// Remove all tr tags
html = html.replace(/<tr[^>]*>/gi, "<tr>" );
// Remove all table tags
html = html.replace(/<table[^>]*>/gi, "<table>" );
// Remove Class attributes
html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove Style attributes
html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
// Remove Lang attributes
html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
// Remove XML elements and declarations
html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
// Remove Tags with XML namespace declarations: <o:p></o:p>
html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
// Replace the &nbsp;
html = html.replace(/&nbsp;/, "" );
//replace H to <P>
html = html.replace( /<H1>/gi, "" ) ;
html = html.replace( /<\/H1>/gi, "" ) ;
html = html.replace( /<H2>/gi, "" ) ;
html = html.replace( /<\/H2>/gi, "" ) ;
html = html.replace( /<H3>/gi, "" ) ;
html = html.replace( /<\/H3>/gi, "" ) ; 
//删除font
html = html.replace( /<(font[^>]*)>/gi, "" ) ;
html = html.replace( /<\/font>/gi, "" ) ;
//html = html.replace(/<\/?A[^>]*>/gi, "" );
// Transform <P> to <DIV>
//把td里面的p去除
var re = new RegExp("(<td>[^>]*<P)([^>]*>.*?)(<\/P><\/td>)","gi") ;
// Different because of a IE 5.0 error
html = html.replace( re, "<td$2</td>" ) ;
//删除p空格
html = html.replace(/<(p[^>]*)><\/p>/gi, "");
html = html.replace(/<(p[^>]*)>&nbsp;<\/p>/gi, "");
//insertHtml( html ) ;
//return html;
Form1.my.value = html;
}
function Paste()
{
var sHTML = GetClipboardHTML() ;
var re = /<\w[^>]* class="?MsoNormal"?/gi ;
var re2 = /<\w[^>]* class="?xl"?/gi ;
if ( re.test( sHTML ) )
{
alert("您要粘贴的内容好像是来自 Word,系统将清除 Word 格式后再粘贴!")
}
if ( re2.test( sHTML ) )
{
alert("您要粘贴的内容好像是来自 Excel,系统将清除 Excel 格式后再粘贴!")
}
cleanAndPaste( sHTML ) ;
}
</script>
<Form id="Form1" name="Form1">
<p>点击粘贴</p>
<input type="button" value="粘贴" onclick="Paste()">
<br/>
<textarea name="my" cols="60" rows="10" id="rn01"></textarea>
<br/>
<INPUT onclick="runEx('rn01')"  type="button" value="运行代码" style="cursor:hand">
<div id=divTemp style="VISIBILITY: hidden; OVERFLOW: hidden; WIDTH: 1px; POSITION: absolute; HEIGHT: 1px"></div>
</Form>
</BODY>
</HTML>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值