盲目了把一天,终于在网上找到了一个解决方法,但是速度不是很乐观:思路是:先将word转换成html文件。在用iframe显示页面
1:这是一个将word转换成html的方法:(绝对路径)
public string WordToHtml(string wordFileName)
{
//在此处放置用户代码以初始化页面
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;
//打开文件
Type docsType = docs.GetType();
Document doc = (Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
//转换格式,另存为
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null, doc, null);
//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
null, word, null);
return saveFileName.ToString();
}
///aspx页面内容
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wordxis.aspx.cs" Inherits="wordxis" %>
<!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">
<div id="div2" style="width:80%; height:80%; overflow:auto;" runat="server"> </div>
<div>
<a href="wordxis.aspx?wd=1">连接到页面打开word</a>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
////aspx.cs文件内容
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
using System.Text;
public partial class wordxis : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ if(Request["wd"]!=null){
// WordToHtml("C:/Documents and Settings/daiyekun/桌面/型号.doc"); 方法在上面
string d = WordToHtml("C:/Documents and Settings/daiyekun/桌面/型号.doc");
StringBuilder strbur=new StringBuilder("<iframe src='" );
strbur.Append(d);
strbur.Append("' width='100%' height='100%'> </iframe>");
div2.InnerHtml = strbur.ToString();
// "<iframe src='C:/Documents and Settings/daiyekun/桌面/型号.html'> </iframe>"
}
}
///最重要的是找添加一个office组件Microsoft.Office.Interop.Word.dll 下载地址www.dllzj.com
联系客服