使用 Epplus 生成带有嵌套列表的 xlsx

我正在使用 EPPlus 生成 xlsx 文档。


我下面的模型包括一个List<string>,这就是事情对我来说变得复杂的地方:


                var tableBody = worksheet.Cells["B2:B2"].LoadFromCollection(

                    from f in saaOperators.OrderBy(x => x.Identifier).Where(t => t.Identifier.StartsWith(worksheet.Name) ||


                    (t.Identifier.StartsWith("W") && worksheet.Name.Equals("BIR")))


                    from u in f.Units


                    select new { f.Identifier, f.Name, f.Profile, u }, true);

如果我这样做select new {f.Identifier, f.Name, f.Profile, f.Units },它只返回Units列表中的第一项。


如果我正在select new { f.Identifier, f.Name, f.Profile, u }为其中的每个项目做,Units它会创建带有重复项的新行Identifier,Name并且Profile。


这是我的模型类:


public class SaaOperator

{

    public string Identifier { get; set; }

    public string Name { get; set; }

    public string Profile { get; set; }

    public List<string> Units { get; set; } = new List<string>();

}

识别具有相同值的单元格并合并它们的正确方法Identifier是Name什么Profile?


例如,在下面的屏幕截图中,我需要合并B3:B4, B5:B6, C3:C4, C5:C6, D3:D4,D5:D6.


我知道我可以使用worksheet.Cells["B3:B4"].Merge = true;,但我需要一种方法来以编程方式识别具有相同值的开始和结束单元格。


鸿蒙传说
浏览 167回答 2
2回答

牧羊人nacy

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

临摹微笑

这是一种方法:给定一个List<SaaOperator>或其他IEnumerable<SaaOperator>:foreach (var saaOperator in saaOperators){&nbsp; &nbsp; string[] valuesToDisplay;&nbsp; &nbsp; for (var x = 0; x < saaOperator.Units.Count; x++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (x == 0) // show all the properties&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valuesToDisplay = new[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {saaOperator.Identifier,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saaOperator.Name,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saaOperator.Profile,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saaOperator.Units[x]};&nbsp; &nbsp; &nbsp; &nbsp; else // after the first unit, don't repeat the other properties&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valuesToDisplay = new[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {"", "", "", saaOperator.Units[x]};&nbsp; &nbsp; }}换一种说法,遍历列表中的所有项目对于每个项目,遍历Units列表中的所有值。如果它是第一项,则显示所有属性。如果不是第一项,则只显示单位。
打开App,查看更多内容
随时随地看视频慕课网APP