。 添加引用 

二。aspx 文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="word.aspx.cs" Inherits="word" %>

<!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>
        类别:
<asp:TextBox ID="txtClass" runat="server"></asp:TextBox><br />
        
&nbsp; 文件名含有关键字<asp:TextBox ID="txtFileNameKey" runat="server"></asp:TextBox><br />
        文档中含有关键字
<asp:TextBox ID="txtFileContentKey" runat="server"></asp:TextBox>&nbsp;<br />
        
&nbsp;<asp:Button ID="btnSearch" runat="server" OnClick="Button2_Click" Text="search" />
        
<asp:Label ID="txtContent" runat="server"></asp:Label><br />
      
</div>
    
</form>
</body>
</html>

三。aspx.cs 文件:

 protected void Button2_Click(object sender, EventArgs e)
    
{
        search(txtClass.Text,txtFileNameKey.Text,txtFileContentKey.Text);
    }
/// <summary>
    
/// 搜索
    
/// </summary>
    
/// <param name="strClass">类别,即表示搜索范围的根(大)文件夹(相对网站根目录)</param>
    
/// <param name="fileNameKey">文件名含有的关键字</param>
    
/// <param name="strFileContentKey">所有文档内要查找的关键字</param>

    protected void search(string strClass,string fileNameKey,string strFileContentKey)
    
{
        Word.Application app 
= new ApplicationClass();
        fileNameKey 
= "*" + fileNameKey + "*" + ".doc";
        strClass 
= Server.MapPath(""+ strClass;//获取类别所在的绝对路径
        GetFileNameList(strClass,fileNameKey);
        
//GetFileNameList(strClass);
        string[] fileNameSet =new string[al.Count];//动态创建 遍历过的文件的虚拟地址的集合
        al.CopyTo(fileNameSet,0);
        
foreach(string str in fileNameSet )
        
{
            SearchKeyInFileContent(str,strFileContentKey,app);
        }

        
if (WordContent != "")
        
{
            txtContent.Text 
= WordContent;
        }

        
else
            txtContent.Text 
= "对不起,找不到,建议您改变搜索条件";
        
object originalFormat = Missing.Value;
        
object routeDocument = Missing.Value;
        
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
        app.Quit(
ref saveChanges, ref originalFormat, ref routeDocument);//退出WINWORD进程
        app = null;
        GC.Collect();
        
//KillWordProcess();
        
       
    }
  /// <summary>
    
/// 查找函数
    
/// </summary>
    
/// <param name="strfile">文件名</param>
    
/// <param name="strFileContentKey">要查找的关键字</param>
    
/// <param name="app">要用哪个application打开</param>

    protected void SearchKeyInFileContent(string strfile,string strFileContentKey,Application app)
    
{
        
//object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
        app.DisplayAlerts = 0//防止Word产生任何可能的提示信息   
        try
        
{
            
object IsreadOnly = true;
            
object fileName=Request.MapPath(strfile);
            
object optional = Missing.Value;
            
object isvisible = false;
            
if (File.Exists(fileName.ToString()))//如果文件存在
            {
                Word.Document doc 
= app.Documents.Open(
                 
ref fileName, ref optional, ref IsreadOnly,
                 
ref optional, ref optional, ref optional,
                 
ref optional, ref optional, ref optional,
                 
ref optional, ref optional, ref optional,
                 
ref isvisible, ref optional, ref optional,
                 
ref optional);
                
                
object MissingValue = Type.Missing;
                
int i = 0, iCount = 0;
                Word.Find wfnd;
                
if (doc.Paragraphs != null && doc.Paragraphs.Count > 0)
                
{
                    iCount 
= doc.Paragraphs.Count;
                    
for (i = 1; i <= iCount; i++)
                    
{
                        wfnd 
= doc.Paragraphs[i].Range.Find;
                        wfnd.ClearFormatting();
                        wfnd.Text 
= strFileContentKey;
                        
if (wfnd.Execute(ref MissingValue, ref MissingValue,
                               
ref MissingValue, ref MissingValue,
                               
ref MissingValue, ref MissingValue,
                               
ref MissingValue, ref MissingValue,
                               
ref MissingValue, ref MissingValue,
                               
ref MissingValue, ref MissingValue,
                               
ref MissingValue, ref MissingValue,
                               
ref MissingValue))//文档存在关键字,即有找到
                        {
                            
string strFileName = Convert.ToString(fileName);
                            
int Severehost = Server.MapPath("").Length;
                            strFileName 
= strFileName.Substring(Severehost);//取相对路径
                            WordContent += "<a href=" + """ + strFileName + """ + ">" + strFileName + "</a><br />";
                            doc.Close(
ref MissingValue, ref MissingValue, ref MissingValue);//关闭当前文档,但并没并闭word程序,即进程WINWORD没关 
                            break;//分段查询,有找到马上跳出,不用再找
                        }

                    }

                }

              
            }

           
        }

        
catch
        
{

        }

        
finally
        
{
      
        }

    }
/// <summary>
    
/// 获得文件列表
    
/// </summary>
    
/// <param name="strDI">父目录 </param>
    
/// <param name="strFileNameKey">文件名关键字</param>

    public void GetFileNameList(string strDI,string strFileNameKey)
    
{
        DirectoryInfo di 
= new DirectoryInfo(strDI);
        
try
        
{
            
string strfileName;
            
foreach (FileInfo fi in di.GetFiles(strFileNameKey))
            
{
                strfileName 
= di.ToString() + "/" + fi.Name.ToString();
                strfileName 
= strfileName.Substring(19);
                al.Add(strfileName); 
//this.globalmessage.Text += "<a href=" + """ + strfileName + """ + ">" + strfileName + "</a><br />";
                
            }

        }

        
catch
        
{
            
        }

        
try
        
{
            
foreach (System.IO.DirectoryInfo di1 in di.GetDirectories())
            
{
                
string strSonDirectoryInfo = di.ToString() + '/' + di1.Name;
                GetFileNameList(strSonDirectoryInfo, strFileNameKey);

            }

        }

        
catch
        
{
            
        }

    }
四:运行结果:
posted on 2011-04-19 16:53  freedom831215  阅读(637)  评论(0编辑  收藏  举报