C # programming to search for text in an Excel document

Source: Internet
Author: User
Open the Excel VBA help to view the EXCEL object model. It is easy to find several sets and objects required to complete this function: application, workbooks, workbook, worksheets, and worksheet and range. The application creates an Excel application, and workbooks opens the Excel document. The workbook obtains the workbook of the Excel document, and the worksheets operation worksheet set. The Worksheet obtains a single worksheet.
   
The search idea corresponds to the above set and object, which can be expressed as follows: the text to be searched may exist on a worksheet in an Excel document. The search should traverse the valid areas in each worksheet of the target Excel file, if it is found, this search is exited. If it is not found, the search continues until the search is completed.
   
Unlike the word object model, the EXCEL object model does not provide the find object, but it does not matter. It can be achieved through two methods. One is through the find () of the range object () method To achieve this. In addition, it is troublesome to retrieve the valid zone usedrange of the worksheet and traverse all the rows and columns in the range object. In actual development, a special phenomenon is found when the second method is used, so the second method is also prepared to be described in detail.
   
Step 1: Open the Excel document: Code

1 object filename = "";
2 object missingvalue = type. missing;
3 string strkeyword = ""; // specify the text to be searched. If there are multiple, declare string []
4 Excel. Application Ep = new excel. applicationclass ();
5 excel. Workbook ew = ep. workbooks. Open (filename. tostring (), missingvalue,
6 missingvalue,
7 missingvalue,
8 missingvalue,
9 missingvalue,
10 missingvalue );
11
12. Then, traverse the Excel Worksheet:
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 ("the document contains the specified keyword! "," Search result ", messageboxbuttons. OK );
30 break;
31}
32}
33

Here are two points worth attention. One is to traverse the index of the worksheet, not starting from 0, but starting from 1; the other is the sixth parameter searchdirection of the find method, specifying the search direction, this parameter is optional in the help document, but I use missingvalue to explain how to compile it. For some reason, I explicitly specify its default value xlnext.
  
The first method is implemented. Let's look at the second method. In addition to traversing the worksheet, This method also traverses the rows and columns of the worksheet using the area. The Code is as follows:

 

Code

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 ("the document contains the specified keyword! "," Search result ", messageboxbuttons. OK );
20}
21}
22}
23}
24

 

Obviously, this method is much more complicated than the first one, but here there is a very special thing about the index for Traversing cells. When usedrange In the worksheet is a single row column, the starting index value for cell traversal in usedrange is 1. When there are multiple rows and multiple columns, the starting index value is 0. I wonder what kind of consideration this is for Excel programmers?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.