Пример #1
0
        public override MemoryStream Generate(List <Item> items, List <Model.Action> actions, List <Rests> rests)
        {
            using (XLWorkbook book = new XLWorkbook(pathToTemplate))
            {
                IXLWorksheet         worksheet = book.Worksheets.Skip(1).First();
                IEnumerable <IXLRow> dataRows  = worksheet.Rows().Where(r => r.RowNumber() >= firstDataRowIndex);
                dataRows.ForEach(dr => dr.Delete());
                IXLRow currentDataRow = GetFirstDataRow(worksheet.Rows());
                currentDataRow.InsertRowsBelow(items.Count);
                IEnumerable <IGrouping <string, Model.Action> > grouppedActions = actions.GroupBy(x => x.BarCode);

                int counter   = 1;
                var bunches   = BuildBunches(items);
                var lastBunch = bunches.Last();
                foreach (var bunch in bunches)
                {
                    var firstRowInBunch = currentDataRow;
                    foreach (Item item in bunch)
                    {
                        long? quantity  = grouppedActions.FirstOrDefault(x => x.Key == item.Code)?.Sum(x => x.Quantity);
                        Rests itemRests = rests.FirstOrDefault(x => x.Code == item.Code);
                        SetRowData(worksheet, currentDataRow, item, itemRests, quantity, counter++);
                        currentDataRow = currentDataRow.RowBelow();
                    }
                    var restsInBunch = rests.Where(x => bunch.Any(b => b.Code == x.Code));
                    currentDataRow = SetSummary(worksheet, currentDataRow, bunch, restsInBunch, grouppedActions);
                    if (bunch != lastBunch)
                    {
                        currentDataRow.AddHorizontalPageBreak();
                        currentDataRow = currentDataRow.RowBelow();
                    }
                    worksheet.PageSetup.PrintAreas.Add(firstRowInBunch.Cell("A").Address, currentDataRow.Cell("X").Address);
                }
                currentDataRow = SetSummary(worksheet, currentDataRow, items, rests, grouppedActions);
                MemoryStream result = new MemoryStream();
                book.SaveAs(result);
                result.Position = 0;
                return(result);
            }
        }