日期:2014-05-20  浏览次数:20984 次

急,一个关于网页Word的问题,解决马上结贴
可以在前台实现将一个Word文档转换为网页形式吗?如何实现?
用后台的方式我可以转换,但经理说那样太浪费服务器资源!

------解决方案--------------------
如果有问题,可以调一下:

<html>
<head>
<title> sfgf </title>
<script language= 'javascript '>
//默认WORD转化文件放于C:\\下
var os__localPath= "C:\\ ";
//保存的文件名
var os__localFile = "gexing100.htm ";
var os__xmlDom = new ActiveXObject( "MSXML2.DOMDocument ");
var os__xmlFSO ;

//保存数据到当前客户端(可以传入一个要保存的文件名).
function os_SaveToLocal()
{
var _saveAs = " ";
if(arguments.length > 0)
_saveAs = arguments[0] + " ";
else
_saveAs = os__localFile;

try
{
if(os__xmlFSO == null)
os__xmlFSO = new ActiveXObject( "Scripting.FileSystemObject ");
}
catch(e)
{
window.alert(e);
}
}

//Word转化为Html文件
function WorcChangeHtml()
{
var os_xmlFSO;
var objUpFile = window.document.all.updFile; //获得上传控件对象
//获得客户端Word文件路径和文件
var UpFileValue = window.document.all.updFile.value;
if(os__xmlFSO == null)
os__xmlFSO = new ActiveXObject( "Scripting.FileSystemObject ");

try
{
if(window.document.Form1.all.value == " ")
{
alert( '请选择对应的Word文件 ');
objUpFile.focus();
}
else if(UpFileValue.indexOf( ".doc ") == -1)
{
alert( '您选择的不是Word文件 \r\n请选择正确的Word文件 ');
objUpFile.focus();
}
else if(!os__xmlFSO.FileExists(objUpFile.value))
{
alert( '对应的Word文件不存在 ');
objUpFile.focus();
}
else
{
// var wdFormatHTML = 8;
var wdFormatHTML=10;
var objWord = new ActiveXObject( "Word.Application ");
if(objWord==null)
{
window.alert( "您的电脑中没有安装Word,无法使用此功能 ");
return;
}
objWord.Application.Visible = false;
var objDoc = objWord.Documents.Open(UpFileValue);
objDoc.SaveAs(os__localPath+os__localFile, wdFormatHTML);
window.document.all.updFile.value = " ";
objDoc.Close();
objWord.Quit();
var GetHtml = GetLine(); //从生成的本地HTML中取到标记
var iBeginIndex = GetHtml.indexOf( " <body ");
var iEndIndex = GetHtml.lastIndexOf( " </body> ");

GetHtml = GetHtml.substring(iBeginIndex,iEndIndex+7).replace( " <body ", " <div ");
GetHtml = GetHtml.replace( " </body> ", " </div> ");
document.all.mainDiv.value=GetHtml;
}
}
catch(e)
{
window.alert(e);
}
}

//读取文本文件
function GetLine()
{
var fso, txtfile, strValue;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject( "Scripting.FileSystemObject ");
txtfile = fso.OpenTextFile(os__localPath+os__localFile, ForReading);
while(!txtfile.AtEndOfStream)
{
strValue = strValue + txtfile.ReadLine();
}
txtfile.Close();
return(strValue);
}
</script>
</head>
<body>