我有一个数据表,我从一个Excel文件创建。这工作正常。但是,最近我有一个规格变化,需要替换表中第0列的值。我无法让我的IF语句触发。这可能是我忽略的简单东西。基本上,如果它是行中的第一列,我想把一个特定的值,我拉到其他地方(我拉细胞的颜色和放入RGB值)。如果它是该行中的任何其他列,请照常继续读取数据。 请参阅下面的代码:C# - 如何检查数据表中是否为列0?
DataTable tbl = new DataTable();
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
//var cellColor = ws.Cells[firstRowCell].Style.Fill.BackgroundColor.LookupColor();
}
//var startRow = hasHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var currentRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
DataRow row = tbl.Rows.Add();
for (var i = 0; i < tbl.Columns.Count; i++)
{
//This is my problem. I am not seeing what needs to be in here.
if (tbl.Columns.????????)
{
row[i] = colorTable[rowNum];
}
else
{
row[i] = currentRow[rowNum, i + 1].Text;
}
}
}
你不能只使用'if(i == 0)'? – stuartd