Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is here</p> <pre><code> string myString = _range.Cells.Value2.ToString(); </code></pre> <p>And here</p> <pre><code> // match any SSN e.g. 1236780909, 123-33-2445 if (Regex.IsMatch(myString, @"\b\d{3}\b\d{2}\b\d{4}")); { _range.Cells.Value2 = replaceSSN; } </code></pre> <p>I think you are misunderstanding what Value2 is, Its the array reprentation of the range you have defined above.</p> <pre><code>Excel.Range _range =(Excel.Range)_excelApp.get_Range("A1:K1",Type.Missing); </code></pre> <p>Value2 Property of the Range class,returns an array of values. what you need to do is perhaps declare a empty array and get the Value2 of the range, loop each item in the array and run the regex, if it finds a match replace the item in the array. Then you could set the array back to the Value2 of the Range, which will update the cells values.</p> <p>EDIT: Please find some sample code below</p> <pre><code> var excelApp = new Application(); excelApp.Workbooks.Open("c:\\Test.xls",Type.Missing,Type.Missing, Type.Missing,Type.Missing, Type.Missing,Type.Missing, Type.Missing,Type.Missing, Type.Missing,Type.Missing, Type.Missing,Type.Missing, Type.Missing,Type.Missing); var ws = excelApp.Worksheets; var worksheet =(Worksheet) ws.get_Item("Sheet1"); Range range = worksheet.UsedRange; // In the following cases Value2 returns different types // 1. the range variable points to a single cell // Value2 returns a object // 2. the range variable points to many cells // Value2 returns object[,] object[,] values = (object[,])range.Value2; for (int row = 1; row &lt;= values.GetUpperBound(0); row++) for (int col = 1; col &lt;= values.GetUpperBound(1); col++) { string value = Convert.ToString(values[row, col]); //Also used a different regex, found yours not to match on your given criteria if (Regex.IsMatch(value, @"^\d{3}-\d{2}-\d{4}$")) { range.Cells.set_Item(row,col,"0"); } } excelApp.Save("C:\\Out.xls"); excelApp.Quit(); Marshal.ReleaseComObject(worksheet); Marshal.ReleaseComObject(ws); Marshal.ReleaseComObject(excelApp); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload