asp.net C#.net操纵word,实现全文检索 ,.net word全文检索

本文介绍了一个基于ASP.NET的应用程序,该程序可以搜索指定类别下的Word文档,并根据用户提供的关键字筛选文件名和文档内容。文章详细展示了aspx页面设计、aspx.cs后台逻辑处理,包括如何使用Word.Application组件来遍历和搜索文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

。 添加引用%E6%9C%AA%E5%91%BD%E5%90%8D.jpg 

二。aspx 文件:

ExpandedBlockStart.gifContractedBlock.gif<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="word.aspx.cs" Inherits="word" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>无标题页</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif    
<div>
None.gif        类别:
<asp:TextBox ID="txtClass" runat="server"></asp:TextBox><br />
None.gif        
&nbsp; 文件名含有关键字<asp:TextBox ID="txtFileNameKey" runat="server"></asp:TextBox><br />
None.gif        文档中含有关键字
<asp:TextBox ID="txtFileContentKey" runat="server"></asp:TextBox>&nbsp;<br />
None.gif        
&nbsp;<asp:Button ID="btnSearch" runat="server" OnClick="Button2_Click" Text="search" />
None.gif        
<asp:Label ID="txtContent" runat="server"></asp:Label><br />
None.gif      
</div>
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif

三。aspx.cs 文件:

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

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

InBlock.gif        
if (WordContent != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            txtContent.Text 
= WordContent;
ExpandedSubBlockEnd.gif        }

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

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

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif              
ExpandedSubBlockEnd.gif            }

InBlock.gif           
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif      
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif    
/// 获得文件列表
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="strDI">父目录 </param>
ExpandedBlockEnd.gif    
/// <param name="strFileNameKey">文件名关键字</param>

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

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
四:运行结果:%E6%9C%AA%E5%91%BD%E5%90%8D.jpg.jpg

转载于:https://www.cnblogs.com/freedom831215/archive/2011/04/19/2021286.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值