仔细阅读以下代码。基本上,调用此函数是为了识别工作表的使用范围,遍历该范围并哈希社会安全号。
这是问题所在。如果创建电子表格并填充一个单元格,则该函数不会对该单元格进行哈希处理。但是,如果我填充一个以上,它将起作用。
这可能是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个值而不是数组边界,它将解决您的问题,并且对单行和多行都适用
Microsoft Excel 2010和Python中的受保护视图 - python这里没有代码示例。刚遇到Microsoft Excel 2010的问题,我在linux上有一个python脚本,可从csv文件中提取数据,将数据推送到excel,然后将该文件作为附件通过电子邮件发送到特定的电子邮件地址。我的问题是我在excel文件中使用公式,并且在它第一次打开时进入“ Protected View”。在单击“启用编辑”后,我的公式才会加载。…
Excel POI:当Excel少于135行时,“我们在…中发现了某些内容的问题” - java使用POI创建Excel时,我的行为很奇怪。每当我的行数少于〜135时,我将无法打开Excel,但出现以下异常:如果我有更多行,则可以毫无问题地打开Excel。这是代码,我如何创建工作表:val out = new ByteArrayOutputStream() try { wb = createWorkbook() wb.writeToOutputStre…
如何在熊猫数据框创建的Excel工作表中向表添加边框? - pythonimport pandas as pd table =[[1,2,3,4],[11,12,13,14],["Pass","Fail","Pass","Fail"]] df = pd.DataFrame(table) df = df.transpose() headers=[…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
熊猫无法打开此Excel文件 - python我正在尝试使用python熊猫打开Excel文件。代码很简单,如下所示;import pandas as pd df = pd.read_excel('../TestXLWings.xlsm', sheetname="TestSheet") 我下面有一个错误;Traceback (most recent call la…