Ejemplo n.º 1
0
        public async Task ExportToDocumentAsync(IDocumentGenerator documentGenerator, string path, IReadOnlyCollection <string> selectedCultures, IReadOnlyCollection <Project> selectedProjects, IStatusProgress progress, CancellationToken cancellationToken)
        {
            progress.Report(StatusRes.GettingProjectsResources);
            SolutionResources solutionResources = await GetSolutionResourcesAsync(selectedCultures, selectedProjects, progress, cancellationToken);

            progress.Report(StatusRes.PreparingResourcesToExport);

            var cultures = selectedCultures.Select(CultureInfo.GetCultureInfo)
                           .ToDictionary(
                cult => cult.Name,
                cult => cult.Name == InvariantCultureId ? InvariantCultureDisplayName : cult.Name.ToUpper()
                );

            var culturesOrder = new List <string>(cultures.Count)
            {
                InvariantCultureId
            };

            culturesOrder.AddRange(cultures.Where(cult => cult.Key != InvariantCultureId).OrderBy(cult => cult.Value).Select(cult => cult.Key));

            var header = new HeaderModel
            {
                Columns = new List <ColumnModel>(1)
                {
                    new ColumnModel {
                        Title = ExcelRes.ResourceKey
                    }
                }
                .Concat(culturesOrder.Select(cultureId => cultures[cultureId]).Select(headerName => new ColumnModel {
                    Title = headerName
                }))
                .Concat(new List <ColumnModel>(1)
                {
                    new ColumnModel {
                        Title = ExcelRes.Comment
                    }
                })
                .ToList()
            };

            IReadOnlyList <ResGroupModel <ResExcelModel> > groups = solutionResources
                                                                    .ProjectResources.Select(proj => new ResGroupModel <ResExcelModel>
            {
                GroupTitle = proj.ProjectName,
                Tables     = proj.Resources.Select(res =>
                {
                    var neutralResources    = res.Value[InvariantCultureId].StringResources;
                    List <string> keysOrder = neutralResources.Keys.OrderBy(key => key).ToList();

                    List <RowModel <ResExcelModel> > rows = keysOrder.Select(
                        resKey => new RowModel <ResExcelModel>
                    {
                        Model = new ResExcelModel(resKey,
                                                  culturesOrder.Select(cultureId => res.Value[cultureId]).Select(resData => resData.StringResources[resKey].Value).ToList(),
                                                  res.Value[InvariantCultureId].StringResources[resKey].Comment)
                    })
                                                            .Where(r => r.Model.ResourceValues.Count != 0)
                                                            .ToList();

                    var tableModel = new ResTableModel <ResExcelModel>
                    {
                        TableTitle = res.Key,
                        Header     = header,
                        Rows       = rows
                    };

                    cancellationToken.ThrowIfCancellationRequested();

                    return(tableModel);
                })
                             .Where(table => table.Rows.Count != 0)
                             .ToList()
            })
                                                                    .Where(res => res.Tables.Count != 0)
                                                                    .ToList();

            await documentGenerator.ExportToDocumentAsync(path, groups, progress, cancellationToken);
        }
Ejemplo n.º 2
0
        private IReadOnlyList <ResGroupModel <TModel> > ImportFromDocument <TModel>(string path) where TModel : IRowModel, new()
        {
            using (ExcelPackage package = new ExcelPackage(new FileInfo(path)))
            {
                var workbook = package.Workbook;

                var dataList = new List <ResGroupModel <TModel> >();

                foreach (ExcelWorksheet worksheetPart in workbook.Worksheets)
                {
                    var tables   = new List <ResTableModel <TModel> >();
                    var newGroup = new ResGroupModel <TModel> {
                        GroupTitle = worksheetPart.Name, Tables = tables
                    };

                    var cells  = worksheetPart.Cells.Select(cell => new { cell.Start.Row, cell.Start.Column, Value = (string)cell.Value }).ToList();
                    int maxRow = cells.Max(c => c.Row);

                    string[][] cellsArray = new string[maxRow][];

                    foreach (var row in cells.GroupBy(c => c.Row))
                    {
                        cellsArray[row.Key - 1] = row.OrderBy(c => c.Column).Select(c => c.Value).ToArray();
                    }

                    for (int i = 0; i < cellsArray.Length; i++)
                    {
                        var table = new ResTableModel <TModel>();

                        table.TableTitle = cellsArray[i][0];

                        i           += 2;
                        table.Header = new HeaderModel {
                            Columns = cellsArray[i].Select(c => new ColumnModel {
                                Title = c
                            }).ToList()
                        };

                        var      rows = new List <RowModel <TModel> >();
                        string[] rowsArray;
                        while (++i < cellsArray.Length && (rowsArray = cellsArray[i]) != null)
                        {
                            rows.Add(new RowModel <TModel>
                            {
                                Model = new TModel
                                {
                                    DataList = rowsArray.Select(a => new CellModel {
                                        DataString = a
                                    }).ToList()
                                }
                            });
                        }

                        table.Rows = rows;
                    }

                    dataList.Add(newGroup);
                }

                return(dataList);
            }
        }
Ejemplo n.º 3
0
        private Task <ResGroupModel <TModel> > ProcessGroupAsync <TModel>(WorksheetEntry worksheet, IEnumerable <CellEntry> cells, CancellationToken cancellationToken) where TModel : IRowModel, new()
        {
            return(Task.Run(() =>
            {
                List <string>[] rows = new List <string> [(int)worksheet.Rows];

                foreach (CellEntry cellEntry in cells)
                {
                    var cellsList = rows[cellEntry.Row - 1];
                    if (cellsList == null)
                    {
                        cellsList = new List <string>();
                        rows[cellEntry.Row - 1] = cellsList;
                    }

                    InsertElementAt(cellsList, (int)cellEntry.Column - 1, SheetCellInfo.GetValue(cellEntry));
                }

                var rowsEnumerator = rows.AsEnumerable().GetEnumerator();

                ResGroupModel <TModel> group = new ResGroupModel <TModel>
                {
                    GroupTitle = worksheet.Title.Text,
                };

                List <ResTableModel <TModel> > tables = new List <ResTableModel <TModel> >();

                int colsCount = (int)worksheet.Cols;

                List <string> row;
                while (rowsEnumerator.MoveNext())
                {
                    //Title row.
                    row = rowsEnumerator.Current;
                    if (row == null)
                    {
                        break;
                    }

                    var newTable = new ResTableModel <TModel> {
                        TableTitle = row[0]
                    };

                    //Space row.
                    rowsEnumerator.MoveNext();

                    //Header
                    rowsEnumerator.MoveNext();
                    row = rowsEnumerator.Current;

                    newTable.Header = new HeaderModel {
                        Columns = row.Select(r => new ColumnModel {
                            Title = r
                        }).ToList()
                    };

                    var rowsList = new List <RowModel <TModel> >();

                    while (rowsEnumerator.MoveNext())
                    {
                        row = rowsEnumerator.Current;
                        if (row == null)
                        {
                            break;
                        }

                        if (row.Count != colsCount)
                        {
                            //Adding empty cells.
                            for (int i = colsCount - 1; i >= 0; i--)
                            {
                                var elem = row.ElementAtOrDefault(i);
                                if (elem == null)
                                {
                                    InsertElementAt(row, i, string.Empty);
                                    if (row.Count == colsCount)
                                    {
                                        break;
                                    }
                                }
                            }
                        }

                        rowsList.Add(new RowModel <TModel> {
                            Model = new TModel {
                                DataList = row.Select(c => new CellModel {
                                    Model = c
                                }).ToList()
                            }
                        });
                    }

                    newTable.Rows = rowsList;
                    tables.Add(newTable);
                }

                group.Tables = tables;

                return group;
            }, cancellationToken));
        }