在寻找ASP.NET在线预览WORD的方法时,无意间看到了3种打开WORD的展示方法,分享咯!!!。
1.以弹出框选择方式打开WORD。
aspx源码:
<iframe ></iframe> aspx.cs源码:
protected void Page_Load(object sender, EventArgs e) { ifr_test.Attributes["src"] = "temp/新建 Microsoft Word 文档.docx"; }
2.使用office直接打开WORD
aspx源码:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> aspx.cs源码:
protected void Button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Word.Application wapp = new Microsoft.Office.Interop.Word.Application(); //在office软件中打开 wapp.Visible = true; object filename = Server.MapPath("~/temp/新建 Microsoft Word 文档.docx"); ; object isread = false; object isvisible = true; object miss = System.Reflection.Missing.Value; wapp.Documents.Open(ref filename, ref miss, ref isread, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref isvisible, ref miss, ref miss, ref miss, ref miss); wapp = null; }
3.在浏览器下方提示打开WORD
aspx源码:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> aspx.cs源码:
protected void Button1_Click(object sender, EventArgs e) { FileToWord(this.Page, "新建 Microsoft Word 文档.docx"); } public void FileToWord(System.Web.UI.Page page,string File) { string Pa = page.Server.MapPath("~/temp/" + File); page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + File + ""); page.Response.ContentType = "application/ms-word"; page.Response.WriteFile(Pa); page.Response.End(); }
|
请发表评论