private static GrouppedBomData GroupData(IEnumerable <BomData> bomData, int index, string line, Action <string> Log) { Log($"Parsing data for {line}..."); var result = new List <BomData>(); var filteredData = bomData.Where(o => { var l = o.Lines[index]; Log($"Filtering data for line: {line}"); return(string.IsNullOrEmpty(l)); }).GroupBy(o => o.Code); foreach (var group in filteredData) { var first = group.First(); var references = string.Join(",", group.AsEnumerable().Select(o => o.Reference)); var data = new BomData { Reference = references, Code = first.Code, Type = first.Type, Description = first.Description, Value = first.Value, Count = group.Count() }; Log($"Groupped Data: {data}"); result.Add(data); } return(new GrouppedBomData(line, index, result)); }
/// <summary> /// Read bom data from 1st sheet /// </summary> /// <param name="workbook"></param> /// <param name="evaluator"></param> /// <returns></returns> private IEnumerable <BomData> ReadBom(IWorkbook workbook, IFormulaEvaluator evaluator) { var worksheet = workbook.GetSheetAt(0); var result = new List <BomData>(); var startRowIndex = FindStartRowIndex("Reference", worksheet); if (startRowIndex == -1) { return(Enumerable.Empty <BomData>()); } int rowCount = worksheet.LastRowNum; var lastColumnIndex = FindLastColumnIndex(startRowIndex, worksheet); Logger.Debug($"Last column:{lastColumnIndex}"); for (int row = startRowIndex; row <= rowCount; row++) { var rowData = worksheet.GetRow(row); var type = GetValue(rowData.GetCell(2), evaluator); var description = GetValue(rowData.GetCell(3), evaluator); var value = GetValue(rowData.GetCell(4), evaluator); if (type == SurfaceType) { description = $"{description} {value}"; value = string.Empty; } var data = new BomData { Reference = GetValue(rowData.GetCell(0), evaluator), Code = GetValue(rowData.GetCell(1), evaluator), Type = type, Description = description, Value = value }; for (int columnIndex = 5; columnIndex < lastColumnIndex + 1; columnIndex++) { data.Lines.Add(GetValue(rowData.GetCell(columnIndex), evaluator)); } Logger.Debug($"Read data: {data}"); result.Add(data); } var header = result.First(); var aboveRow = worksheet.GetRow(startRowIndex - 1); for (int columnIndex = 5; columnIndex < lastColumnIndex + 1; columnIndex++) { var line = aboveRow.GetCell(columnIndex)?.StringCellValue; header.Lines[columnIndex - 5] = $"({line}){header.Lines[columnIndex - 5]}"; } return(result); }