惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
V
Vulnerabilities – Threatpost
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
G
GRAHAM CLULEY
S
Securelist
P
Palo Alto Networks Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
WordPress大学
WordPress大学
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
博客园 - 叶小钗
B
Blog RSS Feed
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
K
Kaspersky official blog
D
Docker
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
Cyberwarzone
Cyberwarzone
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
J
Java Code Geeks
Simon Willison's Weblog
Simon Willison's Weblog
T
Tenable Blog
Recent Announcements
Recent Announcements
T
Tailwind CSS Blog
H
Help Net Security
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
Jina AI
Jina AI
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
NISL@THU
NISL@THU
美团技术团队
腾讯CDC

博客园 - Manho

哪个银行信用卡最好用?哪个银行信用卡最好 顺丰快递,果然不一般! 青春-转自韩寒Sina Blog about Live Mesh DropBox 被封了. 进军企业服务器 Ubuntu准备好了吗? ubuntu 10.04 LTS 下载->安装完成 投资3个域名,不知道有没有前景啊!!! 看完8个笑话,顿悟8个人生道理 防火墙DMZ术语介绍 程序员与喝酒 ITIL的价值 摩羯座终极完美分析 Autoexecnew隐含模块中的编译错误 启动EXCEL是出现提示“EXCEL.exe产生了错误 Outlook 电子邮件附件时错误信息: " 无法创建文件 " 在右键添加EXCEL,WORD,PPT快捷方式 注册了一个域名WELAI.NET 回归博客园
C#编程实现在Excel文档中搜索文本
Manho · 2010-05-20 · via 博客园 - Manho

打开Excel的VBA帮助,查看Excel的对象模型,很容易找到完成这个功能需要的几个集合和对象:Application、Workbooks、Workbook、Worksheets还有Worksheet和Range。Application创建Excel应用,Workbooks打开Excel文档,Workbook获得Excel文档工作薄,Worksheets操作工作表集合,Worksheet获得单个工作表。 
  
  搜索的思路对应上述集合和对象,可以这样表述:要搜索的文本可能存在Excel文档当中的某个工作表上,搜索应该遍历目标Excel文件的每个工作表中的有效区域,如果找到,则退出本次搜索,如果没有找到,则继续搜索直到完成本次搜索。 
  
  跟Word对象模型不一样的是,Excel对象模型没有提供Find对象,不过没有关系,可以通过两种方法来实现,一个是通过Range对象的Find()方法来实现,另外一个比较麻烦,取得工作表Worksheet的有效区域UsedRange之后,遍历该Range对象中的所有行列。实际开发中,用第二种方法时发现了一个特别的现象,所以第二种方法也准备详细记述一下。 
  
  第一步,打开Excel文档: 

代码

 1 object filename="";
 2   object MissingValue=Type.Missing;
 3   string strKeyWord=""//指定要搜索的文本,如果有多个,则声明string[]
 4   Excel.Application ep=new Excel.ApplicationClass();
 5   Excel.Workbook ew=ep.Workbooks.Open(filename.ToString(),MissingValue,
 6   MissingValue,MissingValue,MissingValue,
 7   MissingValue,MissingValue,MissingValue,
 8   MissingValue,MissingValue,MissingValue,
 9   MissingValue,MissingValue,MissingValue,
10   MissingValue); 
11   
12   然后准备遍历Excel工作表: 
13    
14   Excel.Worksheet ews;
15   int iEWSCnt=ew.Worksheets.Count;
16   int i=0,j=0;
17   Excel.Range oRange;
18   object oText=strKeyWord.Trim().ToUpper();   
19   for(i=1;i<=iEWSCnt;i++)
20   {
21    ews=null;
22    ews=(Excel.Worksheet)ew.Worksheets[i];
23    oRange=null;
24    (Excel.Range)oRange=((Excel.Range)ews.UsedRange).Find(oText,MissingValue,MissingValue,
25    MissingValue,MissingValue,Excel.XlSearchDirection.xlNext,
26    MissingValue,MissingValue,MissingValue);
27    if (oRange!=null && oRange.Cells.Rows.Count>=1 && oRange.Cells.Columns.Count>=1)
28    {
29     MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK);
30     break;
31    }
32   } 
33 

这里要说两个值得注意的地方。一个是遍历工作表的索引,不是从0开始,而是从1开始;另外一个是Find方法的第六个参数SearchDirection,指定搜索的方向,帮助文档中说这个参数是可选项,但是我用MissingValue如论如何编译不能通过,不知什么原因,于是显式指定它的默认值xlNext。 
  
  第一种方法实现了,再看看第二种方法。这种方法除了要遍历工作表,还要对工作表使用区域的行和列进行遍历。其它一样,只对遍历说明,代码如下: 

代码

 1 bool blFlag=false;
 2   int iRowCnt=0,iColCnt=0,iBgnRow,iBgnCol;    
 3   for(m=1;m<=iEWSCnt;m++
 4   { 
 5    ews=(Excel.Worksheet)ew.Worksheets[m]; 
 6    iRowCnt=0+ews.UsedRange.Cells.Rows.Count; 
 7    iColCnt=0+ews.UsedRange.Cells.Columns.Count; 
 8    iBgnRow=(ews.UsedRange.Cells.Row>1)? 
 9    ews.UsedRange.Cells.Row-1:ews.UsedRange.Cells.Row; 
10    iBgnCol=(ews.UsedRange.Cells.Column>1)? 
11    ews.UsedRange.Cells.Column-1:ews.UsedRange.Cells.Column;    
12    for(i=iBgnRow;i<iRowCnt+iBgnRow;i++
13    { 
14     for(j=iBgnCol;j<iColCnt+iBgnCol;j++
15     { 
16      strText=((Excel.Range)ews.UsedRange.Cells[i,j]).Text.ToString(); 
17      if (strText.ToUpper().IndexOf(strKeyWord.ToUpper())>=0
18      { 
19       MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK); 
20      } 
21     } 
22    } 
23   } 
24 

显然这种方法比第一种繁琐得多,不过这里有一个关于遍历单元格的索引很特别的地方,当工作表中的使用区域UsedRange为单行单列的时候,对UsedRange中的单元格遍历起始索引值为1,为多行多列的时候,起始索引值为0,不知这是Excel程序设计者出于什么样的考虑?