打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
把word在页面上打开显示

盲目了把一天,终于在网上找到了一个解决方法,但是速度不是很乐观:思路是:先将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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
asp.net word ecxel类型文件在线预览
关于 c#把word文档转换成html后取出html文件中的内容的问题【中国IT软件知识库...
C#中实现VB中的CreateObject方法.
C#的几个实用动态方法
利用word将PDF转换DOC文件的方法 --电脑高手
(C#)利用反射动态调用类成员-程序开发-红黑联盟
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服