Excel.Interop的Worksheet.UsedRange属性可能的错误?

采取下面的代码gander。 基本上,这是一个被调用的函数,用来识别工作表的使用范围,遍历该范围以及哈希社会安全号码。

这是问题。 如果我创build电子表格并填充一个单元格,该函数不会散列该单元格。 但是,如果我填充多个,它的工作。

这可能是UsedRange属性中的一个错误? 还是我错过了什么?

非常感谢。

伍迪

try { foreach (Excel.Worksheet ws in _excelWorksheet) { // Grab the range of numbers that populates the XLS. Excel.Range range = ws.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++) { // Convert values to strings. string value = Convert.ToString(values[row, col]); // Mask the value that we retrieved and store it in a variable. switch (_mdOptions.cbobxEncryption.Text) { case "MD5": { replaceSSN = SHA2Hash.ComputeHash(value, "MD5", null); break; } case "SHA256": { replaceSSN = SHA2Hash.ComputeHash(value, "SHA256", null); break; } default: { replaceSSN = SHA2Hash.ComputeHash(value, "SHA256", null); break; } } // Match expressions to sensitive data and replace with the hash // value. if (Regex.IsMatch(value, @"\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b")) { range.Cells.set_Item(row, col, replaceSSN); } else if (Regex.IsMatch(value, @"\b[0-9]{3}[0-9]{2}[0-9]{4}\b")) { range.Cells.set_Item(row, col, replaceSSN); } } } } catch (Exception) { // This is here because of a non-fatal error that we're getting with // Windows 7 during the hashing phase. Everything still works, // it's just that this error keeps popping up. // REVIEW FOR WINDOWS 7 COMPATABILITY ; //MessageBox.Show("There was a major error. Please restart the program."); //MessageBox.Show(@"Exception: " + ee); } // Pull the hashed password from the registry and add it to the SaveAs //string saveAsPassword = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data", "Password", "")); /* _excelWorkbook.SaveAs("Results.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing); */ // Report success. MessageBox.Show("File masked successfully.", "Mask Data", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); // Shutdown instance of Excel. //_excelApp.Quit(); // Release memory. GC.Collect(); GC.WaitForPendingFinalizers(); Marshal.ReleaseComObject(_excelWorkbook); Marshal.ReleaseComObject(_excelApp); } 

如果仅仅因为double只有1维,则不能将double转换为2维的对象数组。 道歉,如果我告诉你一些你已经知道,但'双'的意思是双精度,并没有任何关系与2维数组(由对象[,]定义)。

你可以简单地添加一个检查这个案例,所以前几行看起来像:

 XLS.Excel.Range range = ws.UsedRange; object[,] values; if (range.Value2.GetType().IsArray) { values = (object[,])range.Value2; } else { values = new object[2,2]; values[1,1] = range.Value2; } 

我没有在这台机器上的VS,所以代码是未经testing,但应该是非常接近

编辑:敲了一个快速testing应用程序,我可以看到什么UsedRange.Value2正在做 – 它返回所有表单值的数组,这就是为什么如果有超过1个单元格它是一个数组,但是对于一个单元格,它将只是返回该值(可以是任何types)。 上面的代码将工作,但是有点黑客。 获取行数和列数的正确方法是使用:

 range.Rows.Count 

 range.Columns.Count 

如果你改变你的循环使用这两个值而不是数组边界它将解决你的问题,并将工作的单行和多行

如代码中的注释所述,Value2属性为多个单元格的单个单元格或对象[,]返回单个文本/数字/逻辑/错误值。