We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replacing cell value problem

Hello,



I am using Workbook.Replace(string placeHolder, string newValue) to replace the string value of a cell. HoweverI onyl wish to replace part of the cell’s value. Does the placeholder string have to match the entire content of a cell? Is it possible to replace part of the string value of a cell or a regular expression match in a cell?

Thanks.

Hi,

May the following sample code help you for your need, kindly consult it. Input and output files are attached:

Sample code:

Workbook workbook = new Workbook();
workbook.Open(@"f:\test\FindStringContains1.xls", FileFormatType.Default);
Worksheets worksheets = workbook.Worksheets;
string stringtofind="Test";
string stringtoreplace = "Rock";
for (int i = 0; i < worksheets.Count; i++)
{

Worksheet worksheet = workbook.Worksheets[i];
Cells cells = worksheet.Cells;
Cell cell;
Cell prevcell = null;

do
{
cell = cells.FindStringContains(stringtofind, prevcell);
if (cell == null)
break;
string val = cell.StringValue.Replace(stringtofind, stringtoreplace);
cell.PutValue(val);
prevcell = cell;

} while (cell != null);

}
workbook.Save("f:\\test\\FindStringContains2_out.xls");

Thank you.

Thank for the solution, but is there any means of doing this at the worksheet level rather than having to loop through every cell?

Hi,

Well, it's already at worksheet level where you can scan through a worksheet's cells to search the text in cells and replace with your desired text. And, I think it's not too complicated to be implemented either :)

Thank you.