62,074
社区成员
发帖
与我相关
我的任务
分享
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Literal ID="lit" runat="server"></asp:Literal>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
lit.Text = GetText(Server.MapPath("a.doc"));
}
public string GetText(string fileName)
{
//实例化COM
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileobj = fileName;
object nullobj = System.Reflection.Missing.Value;
//打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
);
//取得doc文件中的文本
string outText = doc.Content.Text;
//关闭文件
doc.Close(ref nullobj, ref nullobj, ref nullobj);
//关闭COM
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
//返回
return outText;
}
<html>
<div id='div'>
</div>
<input type="button" value="读取Word" onclick='readWord();' />
</html>
<script language="javascript">
function readWord()
{
var WordApp,WordDoc,str;
WordApp = new ActiveXObject("Word.application");
WordDoc = WordApp.Documents.Open("C:\\a.doc");
str=WordDoc.content.text;
WordDoc.close();
WordApp.quit();
document.getElementById('div').innerHTML=str;
}
</script>