仔细阅读以下代码。基本上,调用此函数是为了识别工作表的使用范围,遍历该范围并哈希社会安全号。

这是问题所在。如果创建电子表格并填充一个单元格,则该函数不会对该单元格进行哈希处理。但是,如果我填充一个以上,它将起作用。

这可能是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转换为2维的对象数组,仅是因为double只有1维。抱歉,如果我要告诉您您已经知道的东西,但是术语“ double”表示双精度,与二维数组(由object [,]定义)无关。

您可以为此情况添加一个检查,因此前几行如下所示:

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,因此代码未经测试,但应该非常接近

编辑:
敲出一个快速测试应用程序后,我可以看到UsedRange.Value2在做什么-它返回所有工作表值的数组,这就是为什么如果有多个单元格,则它是一个数组,但是对于一个单元格,它只会返回那个值(可以是任何类型)。上面的代码可以工作,但是有点麻烦。获取行数和列数的正确方法是使用:

range.Rows.Count




range.Columns.Count


如果将for循环更改为使用这2个值而不是数组边界,它将解决您的问题,并且对单行和多行都适用

关于c# - Excel.Interop的Worksheet.UsedRange属性中可能存在错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1295262/

10-13 00:58