Find Row matching some text

Hi,

I am using aspose to enter some records to excel. However, if one cell has a particular value, should not add again.

How do I get the row and cell in worksheet which has already has given text.

Hi,

Thanks for using Aspose.Cells for .NET.

First, you need to search your data. Please see the following article for your reference.

Once, you were able to find your cell, then you can find the cell’s row or column using Cell.Row and Cell.Column properties.

Also, you can access your specific Row and Column using Worksheet.Cells.Rows and Worksheet.Cells.Columns collection.

Let us know if you still have any questions relating to Aspose.Cells for .NET, we will help you asap.

Hi,


I think before inserting the value into the cells, you need to find out if the value is already there in the cells (of the worksheet). You may use Find and Search options provided by Aspose.Cells APIs for this purpose, see the topic: http://www.aspose.com/docs/display/cellsnet/Find+or+Search+Data. See a simple code below for your reference:

Sample code:

Workbook workbook = new Workbook(“e:\test\Book1.xls”);
Worksheet sheet = workbook.Worksheets[0];

Cells cells = sheet.Cells;
//Suppose you need to add a text into the cells.
//You need to find out if this text is already there in some cells.
//You may use Find and Search options provided by Aspose.Cells APIs for this purpose.

string text = “test”;
FindOptions findOptions = new FindOptions();
findOptions.CaseSensitive = false;
findOptions.LookInType = LookInType.Values;
Aspose.Cells.Cell foundCell = cells.Find(text, null, findOptions);
//If a cell found with the value
if (foundCell != null)
{
int row = foundCell.Row;
string name = foundCell.Name;
//Your code goes here.

}
else {
//Your code goes here.
}

Thanks this works fine.

Another question, i have one row that contains Date time, is there a way to find the maximum Date value in that column and sample code for same.

Hi,

Thanks for your question.

I think, there are many date values in that row and you want to find the cell in that row which has maximum date value.

You can achieve it by iterating the row cells and comparing the date values.

Below is a sample code how to iterate the rows and find the maximum date value.

C#


int maxColumn = row.LastCell.Column;


//Iterate all columns in your row

for (int col = 0; col < maxColumn; col++)

{

//Access the cell

Cell cell = cells[row.Index, col];


if (cell.Type == CellValueType.IsDateTime)

{

DateTime dt=(DateTime)cell.Value;


if (maxDate < dt)

maxDate = dt;


}

}