C programming to achieve Excel document search text content method and thinking

  • 2020-05-17 06:14:49
  • OfStack

Open Excel's VBA help and view Excel's object model. It is easy to find several collections and objects needed to accomplish this functionality: Application, Workbooks, Workbook, Worksheets, Worksheet, and Range. Application creates an Excel application, Workbooks opens an Excel document, Workbook gets an Excel document workbook, Worksheets operates on a collection of worksheets, and Worksheet gets a single worksheet.
Search the thinking of corresponding to the collection and the object can be this statement: to search the text of the possible Excel documentation of a work table, should search traversal target Excel file effective area in each worksheet, if found, retreat to the search, if not found, will continue to search until complete the search.
Unlike the Word object model, the Excel object model does not provide Find objects, but that's ok. It can be implemented in two ways. One is through the Find() method of the Range object, and the other one is troublesome. In the actual development, a special phenomenon was found when using the second method, so the second method is also going to be described in detail.
Step 1, open the Excel document:

  object filename=""; 
  object MissingValue=Type.Missing; 
  string strKeyWord=""; // Specifies the text to search for and, if multiple, declares string[] 
  Excel.Application ep=new Excel.ApplicationClass(); 
  Excel.Workbook ew=ep.Workbooks.Open(filename.ToString(),MissingValue, 
   MissingValue,MissingValue,MissingValue, 
   MissingValue,MissingValue,MissingValue, 
   MissingValue,MissingValue,MissingValue, 
   MissingValue,MissingValue,MissingValue, 
   MissingValue); 

Then prepare to traverse the Excel worksheet:

  Excel.Worksheet ews; 
  int iEWSCnt=ew.Worksheets.Count; 
  int i=0,j=0; 
  Excel.Range oRange; 
  object oText=strKeyWord.Trim().ToUpper(); 
   
  for(i=1;i<=iEWSCnt;i++) 
  { 
   ews=null; 
   ews=(Excel.Worksheet)ew.Worksheets[i]; 
   oRange=null; 
   (Excel.Range)oRange=((Excel.Range)ews.UsedRange).Find( 
   oText,MissingValue,MissingValue, 
   MissingValue,MissingValue,Excel.XlSearchDirection.xlNext, 
   MissingValue,MissingValue,MissingValue); 
   if (oRange!=null && oRange.Cells.Rows.Count>=1 && oRange.Cells.Columns.Count>=1) 
   { 
   MessageBox.Show(" The document contains the specified keyword! "," The search results ",MessageBoxButtons.OK); 
   break; 
   } 
  } 

There are two caveats here. One is the index that traverses the worksheet, starting not at 0, but at 1; The other one is the sixth parameter of the Find method, SearchDirection, which specifies the direction of the search. The help document says that this parameter is optional, but I explicitly specify its default value, xlNext, because I can't compile it with MissingValue for some reason.
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 region. For the other 1, it only explains the traversal. The code is as follows:

  bool blFlag=false; 
  int iRowCnt=0,iColCnt=0,iBgnRow,iBgnCol;  
  for(m=1;m<=iEWSCnt;m++) 
  { 
   ews=(Excel.Worksheet)ew.Worksheets[m]; 
   iRowCnt=0+ews.UsedRange.Cells.Rows.Count; 
   iColCnt=0+ews.UsedRange.Cells.Columns.Count; 
   iBgnRow=(ews.UsedRange.Cells.Row>1)? 
   ews.UsedRange.Cells.Row-1:ews.UsedRange.Cells.Row; 
   iBgnCol=(ews.UsedRange.Cells.Column>1)? 
   ews.UsedRange.Cells.Column-1:ews.UsedRange.Cells.Column; 
   
   for(i=iBgnRow;i 
   { 
   for(j=iBgnCol;j 
   { 
   strText=((Excel.Range)ews.UsedRange.Cells[i,j]).Text.ToString(); 
   if (strText.ToUpper().IndexOf(strKeyWord.ToUpper())>=0) 
   { 
   MessageBox.Show(" The document contains the specified keyword! "," The search results ",MessageBoxButtons.OK); 
   } 
   } 
   } 
  } 

Obviously this method more than 1 kind of complicated, but there is one index of traverse cell is very special, when the work table UsedRange for single line, the use of area of UsedRange cell traverse the starting index value is 1, multiple lines many columns, then start index value is 0, I do not know this is Excel programmer for the sake of what?

Related articles: