2009-08-01 113 views
2

我想掃描一個excel表格,並用零替換任何出現的社會安全號碼......我希望儘可能使用Excel.Interop來做到這一點,但我會「M開在這一點上什麼...這裏是我的一些代碼...我敲我的桌子,在過去幾個月的頭......使用excel替換excel中的單元格值.interop

 
// Get range and convert it to a string variable 
      Excel.Range _range =(Excel.Range)_excelApp.get_Range("A1:K1",Type.Missing); 

      // convert the value of our cells in our range 
      // to the string variable 
      string myString = _range.Cells.Value2.ToString();   

      // 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; 
      } 

      // save our workbook with a new name and create a backup 
      _excelWorkbook.SaveAs("Results.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, true, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

      // close workbook 
      _excelWorkbook.Close(false, Type.Missing, Type.Missing); 

      // send quit signal to app 
      _excelApp.Quit(); 

      // report success    
      MessageBox.Show("File masked successfully.", "Mask Data", MessageBoxButtons.OK); 

      // release memory 
      //System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApp); 

      // change label back to being blank 
      lblActivity.Text = ""; 
     }  

回答

6

的問題是在這裏

 string myString = _range.Cells.Value2.ToString();   

而這裏

 // 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; 
     } 

我認爲你誤解了Value2是什麼,它是你在上面定義的範圍的陣列reprentation。

Excel.Range _range =(Excel.Range)_excelApp.get_Range("A1:K1",Type.Missing); 

Value2 Range類的屬性返回一個值數組。你需要做的可能是聲明一個空數組並獲取該範圍的Value2,循環數組中的每個項並運行正則表達式,如果它發現匹配替換數組中的項。 然後您可以將數組設置回Range的Value2,它將更新單元格值。

編輯:請在下面找到

   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 <= values.GetUpperBound(0); row++) 
        for (int col = 1; col <= 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); 
+0

完美地工作。非常感謝你Ori ...你從頭痛中拯救了我的頭......大聲笑......我也完全理解你做了什麼......一種學習體驗......再次感謝! – Woody 2009-08-02 02:22:29

+0

任何時候都沒有問題:) – 2009-08-02 08:37:43

4

這裏是一些示例代碼,你可以有代碼片段的瞭解不多,但我還沒有嘗試過,因此它可能包含語法錯誤。

Excel.WorkSheet currentSheet = ApplicationInstance.ActiveSheet as Excel.Worksheet; 

foreach(Exce.Range r1 in currentSheet.UsedRange) 
{ 
    if(Regex.IsMatch(r1.Value2.ToString(), @"\b\d{3}\b\d{2}\b\d{4}")) // check r1.Value2 should not be null 
    { 
     r1.Value2 = Your_New_Value; 
    } 
} 
相關問題