gpt4 book ai didi

c# - 使用 excel.interop 替换 excel 中的单元格值

转载 作者:行者123 更新时间:2023-11-30 19:14:13 24 4
gpt4 key购买 nike

我想扫描 Excel 工作表,并将所有出现的社会安全号码替换为零...如果可能的话,我想使用 Excel.Interop 来执行此操作,但此时我对任何事情都持开放态度。 ..这是我的一些代码...过去几个月我一直在用头敲 table ...

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

最佳答案

问题就在这里

        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 是什么,它是您在上面定义的范围的数组表示。

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

Range 类的 Value2 属性,返回值数组。您需要做的可能是声明一个空数组并获取范围的 Value2,循环数组中的每个项目并运行正则表达式,如果找到匹配项则替换数组中的项目。然后您可以将数组设置回范围的 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);

关于c# - 使用 excel.interop 替换 excel 中的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1217000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com