web中,如何讀取客戶端Word內(nèi)容并且顯示在頁面中?
我發(fā)現(xiàn)有2種方法,一種是直接在客戶端操作,一種是把客戶端的word上傳到服務(wù)器端操作,這2種各有優(yōu)缺點(diǎn)。
我現(xiàn)就舉出客戶端操作的例子
首先在.aspx中添加如下js代碼
<script language='javascript'>
//默認(rèn)word轉(zhuǎn)化文件放于C://下
var os__localPath = "C://";
//保存的文件名
var os__localFile = "defaultFileWord.htm";
var os__xmlDom = new ActiveXObject("MSXML2.DOMDocument");
var os__xmlfso ;
//保存數(shù)據(jù)到當(dāng)前客戶端(可以傳入一個(gè)要保存的文件名).
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轉(zhuǎn)化為Html文件
function WorcChangeHtml()
{
var os_xmlFSO;
//獲得上傳控件對(duì)象
var objUpFile = window.document.Form1.updFile;
//獲得客戶端Word文件路徑和文件
var UpFileValue = window.document.Form1.updFile.value;
if(os__xmlFSO == null)
os__xmlFSO = new ActiveXObject("Scripting.FileSystemObject");
try
{
if(window.document.Form1.updFile.value == "")
{
alert('請(qǐng)選擇對(duì)應(yīng)的Word文件');
objUpFile.focus();
}
else if(UpFileValue.indexOf(".doc") == -1)
{
alert('您選擇的不是Word文件 /r/n請(qǐng)選擇正確的Word文件');
objUpFile.focus();
}
else if(!os__xmlFSO.FileExists(objUpFile.value))
{
alert('對(duì)應(yīng)的Word文件不存在');
objUpFile.focus();
}
else
{
var wdFormatHTML = 8;
var objWord = new ActiveXObject("Word.application");
objWord.Application.Visible = false;
var objDoc = objWord.Documents.Open(UpFileValue);
objDoc.SaveAs(os__localPath+os__localFile, wdFormatHTML);
window.document.Form1.updFile.value = "";
objDoc.Close();
objWord.Quit();
var GetHtml = GetLine();
var iBeginIndex = GetHtml.indexOf("<body");
var iEndIndex = GetHtml.lastIndexOf("</body>");
GetHtml = GetHtml.substring(iBeginIndex,iEndIndex+7).replace("<body","<div");
GetHtml = GetHtml.replace("</body>","</div>");
//將轉(zhuǎn)化后的值賦給頁面控件txtIdea的值,我為了將Word值保存進(jìn)數(shù)據(jù)庫所以用<input type = "hidden" ..... 如果將Word內(nèi)容顯示可以考慮 window.document.Form1."你的顯示控件ID".innerText = GetHtml;
window.document.Form1.txtIdea.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>
注意頁面上需要添加以下2個(gè)控件和對(duì)應(yīng)的客戶端事件
<input id="updFile" type="file" style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; BORDER-LEFT: 1px solid; WIDTH: 77.46%; BORDER-BOTTOM: 1px solid; HEIGHT: 26px"
size="71"> <input style="BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid; FONT-SIZE: 15pt; BORDER-LEFT: #999999 1px solid; WIDTH: 103px; BORDER-BOTTOM: #999999 1px solid; HEIGHT: 28px"
onclick="WorcChangeHtml()" runat="server" id="btnUpLoad" type="submit" value="導(dǎo)入" name="btnUpLoad">
<textarea style="WIDTH: 15.25%; HEIGHT: 23px" rows="50" cols="16" id="txtIdea"
runat="server">
其中txtIdea中的值就是客戶端Word中的內(nèi)容了,注意:需要調(diào)整IE的安全性設(shè)置,否則將無效.
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注