循环遍历数据行并更新单元格
Loop through datarow and update cell
我有以下代码:
foreach (DataRow row in table.Rows)
{
//cell cannot contain any of the following characters: :\/?*[]'
}
我事先不知道列的名称,或者我可以这样做:
if (row["Name"].ToString() == "SomeName").
我喜欢做的是,如果任何单元格包含任何字符 :/?*[]' 我想用空字符串替换它。
你可以试试:
foreach (DataRow row in table.Rows)
{
// Loop through the cell values
foreach (object item in row.ItemArray)
{
// Get the cell value
string cellValue = item.ToString();
// Remove unwanted characters
cellValue = cellValue.Replace(":","");
cellValue = cellValue.Replace("/","");
cellValue = cellValue.Replace("?","");
cellValue = cellValue.Replace("*","");
cellValue = cellValue.Replace("[","");
cellValue = cellValue.Replace("]","");
cellValue = cellValue.Replace("'","");
{
}
这可能不是最好的方法,但如果您赶时间,应该可行
您可以尝试使用 Regex.Replace
和 :|\/|\?|\*|\[|\]|'"
将字符替换为空字符串。
Regex regex = new Regex(":|\/|\?|\*|\[|\]|'");
foreach (DataRow row in table.Rows)
{
foreach (object item in row.ItemArray)
{
var result = regex.Replace(item.ToString(), string.Empty);
}
}
Regex regex = new Regex(":|\/|\?|\*|\[|\]|'");
var result = regex.Replace("2:76000?0178'12?0[0]8", string.Empty);
结果
276000017812008
我有以下代码:
foreach (DataRow row in table.Rows)
{
//cell cannot contain any of the following characters: :\/?*[]'
}
我事先不知道列的名称,或者我可以这样做:
if (row["Name"].ToString() == "SomeName").
我喜欢做的是,如果任何单元格包含任何字符 :/?*[]' 我想用空字符串替换它。
你可以试试:
foreach (DataRow row in table.Rows)
{
// Loop through the cell values
foreach (object item in row.ItemArray)
{
// Get the cell value
string cellValue = item.ToString();
// Remove unwanted characters
cellValue = cellValue.Replace(":","");
cellValue = cellValue.Replace("/","");
cellValue = cellValue.Replace("?","");
cellValue = cellValue.Replace("*","");
cellValue = cellValue.Replace("[","");
cellValue = cellValue.Replace("]","");
cellValue = cellValue.Replace("'","");
{
}
这可能不是最好的方法,但如果您赶时间,应该可行
您可以尝试使用 Regex.Replace
和 :|\/|\?|\*|\[|\]|'"
将字符替换为空字符串。
Regex regex = new Regex(":|\/|\?|\*|\[|\]|'");
foreach (DataRow row in table.Rows)
{
foreach (object item in row.ItemArray)
{
var result = regex.Replace(item.ToString(), string.Empty);
}
}
Regex regex = new Regex(":|\/|\?|\*|\[|\]|'");
var result = regex.Replace("2:76000?0178'12?0[0]8", string.Empty);
结果
276000017812008