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?
May the following sample code help you for your need, kindly consult it. Input and output files are attached:
Workbook workbook = new Workbook();
Worksheets worksheets = workbook.Worksheets;
string stringtoreplace = "Rock";
for (int i = 0; i < worksheets.Count; i++)
Worksheet worksheet = workbook.Worksheets[i];
Cells cells = worksheet.Cells;
Cell prevcell = null;
cell = cells.FindStringContains(stringtofind, prevcell);
if (cell == null)
string val = cell.StringValue.Replace(stringtofind, stringtoreplace);
prevcell = cell;
} while (cell != null);
Thank for the solution, but is there any means of doing this at the worksheet level rather than having to loop through every cell?
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 :)