hi all...
Take a gander at the following code. Basically, this is a function that gets called in order to identify the used range of a worksheet, loop through that range, and hash social security numbers.
Here's the problem. If I create a spreadsheet and populate one cell, the function does not hash that cell. However, if I populate more than one, it works.
Could this be a bug in the UsedRange property? Or am I missing something?
thanks much.
Woody
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);
}