SoFunction
Updated on 2025-03-06

Methods and ideas for C# programming to implement text content in Excel documents

Open Excel's VBA Help and view Excel's object model. It is easy to find several collections and objects needed to complete this function: Application, Workbooks, Workbooks, Worksheets, Worksheets, Worksheets, Worksheets, and Worksheets and Range. Application creates Excel applications, Workbooks opens Excel documents, Workbooks gets Excel document workbooks, Worksheets operates worksheet collections, Worksheets gets a single worksheet.
The search idea corresponds to the above set and object. It can be expressed in this way: the text to be searched may exist on a worksheet in the Excel document. The search should traverse the valid area in each worksheet of the target Excel file. If it is found, exit this search. If it is not found, continue to search until the search is completed.
Unlike the Word object model, the Excel object model does not provide a Find object, but it does not matter. It can be implemented in two ways. One is achieved through the Find() method of the Range object, and the other is more troublesome. After obtaining the effective area of ​​the Worksheet Worksheet, usedRange, iterates through all rows and columns in the Range object. In actual development, a special phenomenon was discovered when using the second method, so the second method is also prepared to be described in detail.
The first step is to open the Excel document:
Copy the codeThe code is as follows:

object filename="";
object MissingValue=;
string strKeyWord=""; //Specify the text to search. If there are multiple, declare string[]
ep=new ();
ew=((),MissingValue,
MissingValue,MissingValue,MissingValue,
MissingValue,MissingValue,MissingValue,
MissingValue,MissingValue,MissingValue,
MissingValue,MissingValue,MissingValue,
MissingValue);

Then prepare to traverse the Excel worksheet:
Copy the codeThe code is as follows:

ews;
int iEWSCnt=;
int i=0,j=0;
oRange;
object oText=().ToUpper();

for(i=1;i<=iEWSCnt;i++)
{
ews=null;
ews=()[i];
oRange=null;
()oRange=(()).Find(
oText,MissingValue,MissingValue,
MissingValue,MissingValue,,
MissingValue,MissingValue,MissingValue);
if (oRange!=null && >=1 && >=1)
{
("The document contains the specified keyword!","Search results",);
break;
}
}

Here are two things worth noting. One is to traverse the index of the worksheet, not starting from 0, but starting from 1; the other 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 use MissingValue for the reason that it cannot be passed. So I explicitly specify its default value xlNext.
The first method is implemented, let’s take a look at the second method. In addition to traversing the worksheet, this method also traverses the rows and columns of the worksheet's use area. The others are just explained on the traversal, and the code is as follows:
Copy the codeThe code is as follows:

bool blFlag=false;
int iRowCnt=0,iColCnt=0,iBgnRow,iBgnCol; 
for(m=1;m<=iEWSCnt;m++)
{
ews=()[m];
iRowCnt=0+;
iColCnt=0+;
iBgnRow=(>1)?
-1:;
iBgnCol=(>1)?
-1:;

for(i=iBgnRow;i
{
for(j=iBgnCol;j
{
strText=(()[i,j]).();
if (().IndexOf(())>=0)
{
("The document contains the specified keyword!","Search results",);
}
}
}
}

Obviously this method is much more complicated than the first one, but here is a very special place about traversing the index of cells. When the usedRange in the worksheet is a single row and a single column, the starting index value for traversing the cells in UsedRange is 1, and when there are multiple rows and columns, the starting index value is 0. I wonder what the Excel programmer considers?