我开发了一段代码,允许通过逐行放置一个值来削减excel中一个单元格的值,并且出现以下错误:
“索引超出范围。它不能为负,并且必须小于参数名称:index的大小”
private DataTable trasnformDT(DataTable dt1)
{
DataTable dt = dt1.Clone();
int i = 0;
foreach (DataRow row in dt1.Rows)
{
List<string> Word_countLst = row["Word_count"].ToString().Trim().TrimStart('[').TrimEnd(']').Split(',').ToList();
List<string> command_addressLst = row["command_address"].ToString().Trim().TrimStart('[').TrimEnd(']').Split(',').ToList();
List<string> response_addressLst = row["response_address"].ToString().Trim().TrimStart('[').TrimEnd(']').Split(',').ToList();
string[] Register_numberLst = row["Register_number"].ToString().Trim().TrimStart('[').TrimEnd(']').Split(',');
string[] Register_valueLst = row["Register_value"].ToString().Trim().TrimStart('[').TrimEnd(']').Split(',');
if (row["Word_count"].ToString().Trim() == "[]")
{
if (command_addressLst.Count() == response_addressLst.Count())
{
int j = 0;
foreach (string item in command_addressLst)
{
// long cacount = Int64.Parse(item);
DataRow dr = dt.NewRow();
dr.ItemArray = row.ItemArray;
dr["command_address"] = command_addressLst[j];
dr["response_address"] = response_addressLst[j];
dt.Rows.Add(dr);
j++;
}
}
}
else
{
foreach (string item in Word_countLst)
foreach (string item2 in command_addressLst)
{
long wcount = Int64.Parse(item);
long wcount2 = Int64.Parse(item2);
for (int j = 0; j < wcount; j++)
{
DataRow dr = dt.NewRow();
dr.ItemArray = row.ItemArray;
dr["Word_count"] = item;
dr["Register_number"] = Register_numberLst[j];
dr["Register_value"] = Register_valueLst[j];
dr["command_address"] = item2;
dr["response_address"] = item2;
dt.Rows.Add(dr);
}
}
}
i++;
}
return dt;
}
我想拆分word_count,register_value和register_number,但对于command_address和command_response则将其拆分,就像对word_count一样,但又要另外一个长度。但是这段代码是双重的