天天看點

ASP.NET讀取word到頁面

 JavaScript實作:

<mce:script type ="text/javascript" ><!--

function readWord()

{

var div1=document .getElementById ("div1");

var WordApp,WordDoc,Str;

WordApp =new ActiveXObject ("Word.application");

WordDoc =WordApp.Documents.Open("F://工作日志.doc");

Str =WordDoc .content.text;

WordDoc .close();

WordApp .quit();

div1.innerHTML =Str ;

}

// --></mce:script>

<div id="div1">

</div>

<input id="Button2" type="button" value="button" OnClick ="readWord();"/>

C#實作:(添加office引用)

<asp:Literal ID="Literal1" runat="server"></asp:Literal>

protected void Page_Load(object sender, EventArgs e)

{

this .Literal1 .Text =GetTest ("F://工作日志.doc");

}

public string GetTest(string FileName)

{

Microsoft.Office.Interop.Word.ApplicationClass WordApi = new Microsoft.Office.Interop.Word.ApplicationClass();

object fileObject = FileName;

object nullobj = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Document doc = WordApi.Documents.Open(ref fileObject, 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);

string StrText = doc.Content.Text;

doc.Close(ref nullobj, ref nullobj, ref nullobj);

WordApi.Quit(ref nullobj, ref nullobj, ref nullobj);

return StrText;

}

繼續閱讀