2 回答

TA贡献1862条经验 获得超7个赞
根据评论,我会避免使用这种LoadFromCollection方法并采用传统方法,for因为您所做的太具体了。而且我也会避免尝试合并单元格,因为这会使事情不必要地复杂化。这应该这样做:
var data = saaOperators
.Where(t => t.Identifier.StartsWith(worksheet.Name) || (t.Identifier.StartsWith("W") && worksheet.Name.Equals("BIR")))
.OrderBy(x => x.Identifier)
.ToList();
var r = 2;
worksheet.Cells[r, 1].Value = "Identifier";
worksheet.Cells[r, 2].Value = "Name";
worksheet.Cells[r, 3].Value = "Profile";
worksheet.Cells[r, 4].Value = "Unit";
r++;
for (var i = 0; i < data.Count; i++)
{
var op = data[i];
worksheet.Cells[r + i, 1].Value = op.Identifier;
worksheet.Cells[r + i, 2].Value = op.Name;
worksheet.Cells[r + i, 3].Value = op.Profile;
if (!op.Units.Any())
continue;
for (var j = 0; j < op.Units.Count; j++)
worksheet.Cells[r + i + j, 4].Value = op.Units[j];
r += op.Units.Count - 1;
}

TA贡献1982条经验 获得超2个赞
这是一种方法:
给定一个List<SaaOperator>或其他IEnumerable<SaaOperator>:
foreach (var saaOperator in saaOperators)
{
string[] valuesToDisplay;
for (var x = 0; x < saaOperator.Units.Count; x++)
{
if (x == 0) // show all the properties
valuesToDisplay = new[]
{saaOperator.Identifier,
saaOperator.Name,
saaOperator.Profile,
saaOperator.Units[x]};
else // after the first unit, don't repeat the other properties
valuesToDisplay = new[]
{"", "", "", saaOperator.Units[x]};
}
}
换一种说法,
遍历列表中的所有项目
对于每个项目,遍历
Units
列表中的所有值。如果它是第一项,则显示所有属性。如果不是第一项,则只显示单位。
- 2 回答
- 0 关注
- 164 浏览
添加回答
举报