首页 .NET/Web 在线读取Word文档(不能附图) ASP.NET

在线读取Word文档(不能附图) ASP.NET

作者:胡同里的砖头 围观群众:1531 更新于:2013-12-19

如下方法可在线读取Word,缺点,word只能是纯文本或表格,不能附带图片

前台放一个Label
<asp:Label ID="Label2" runat="server" />

后台:
string url = Request.QueryString["url"];
        Word2HTMLHelper helper = new Word2HTMLHelper();
        helper.Convert(Server.MapPath(url));
        url = url.Substring(0, url.IndexOf('.'));
        Label2.Text = string.Format("<iframe runat=\"server\" src=\"{0}\" id=\"iframe1\" style=\"width: 559px; height: 575px\" />", url + ".htm");

类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Microsoft.Office;

using Microsoft.Office.Interop.Word;

/// <summary>
/// Word2HTMLHelper 的摘要说明
/// </summary>
public class Word2HTMLHelper
{
    protected Microsoft.Office.Interop.Word.ApplicationClass objWord = new ApplicationClass();
    protected string strPathToUpload;
    protected string strPathToConvert;
    object fltDocFormat = 10;
    protected object missing = System.Reflection.Missing.Value;
    protected object;
    protected object isVisible = false;

    public void Convert(string strWordFullName)
    {
        //fullpath = c:\\aaa\\aa.doc

        int iIdx = strWordFullName.LastIndexOf('\\');
        object WordFullName = (object)strWordFullName;
        string strFileName = strWordFullName.Substring(iIdx + 1, strWordFullName.Length - iIdx-1);
        string strFullPath = strWordFullName.Substring(0, iIdx);

        string filename = strFileName.Substring(0, strFileName.IndexOf('.'));

        //Map-path to the folder where html to be saved
        object FileToSave = strFullPath + "\\" + filename + ".htm";

        try
        {
            //To check the file extension if it is word document or something else
            string[] strSep = strFileName.Split('.');
            int arrLength = strSep.Length - 1;
            string strExt = strSep[arrLength].ToString().ToUpper();

            if (strExt.ToUpper().Equals("DOC") || strExt.ToUpper().Equals("DOCX"))
            {
                //open the file internally in word. In the method all the parameters should be passed by object reference
                objWord.Documents.Open(ref WordFullName, ref readOnly, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref  missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,
                    ref missing, ref missing);

                //Do the background activity
                objWord.Visible = false;
                Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                oDoc.SaveAs(ref FileToSave, ref fltDocFormat, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing);
            }
            else
            {
                throw new InvalidOperationException();
            }

            //Close/quit word
            objWord.Quit(ref missing, ref missing, ref missing);
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}

  • 本文标题: 在线读取Word文档(不能附图) ASP.NET
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8