Пример #1
0
        /// <summary>
        /// Converts the <see cref="Telerik.Windows.Documents.Spreadsheet.Model.Workbook">Telerik DPL Workbook Document</see> to a Workbook.
        /// </summary>
        /// <param name="document">The source document</param>
        /// <returns>A Workbook populated with the data from the source document</returns>
        public static Workbook FromDocument(Document document)
        {
            var workbook = new Workbook();

            workbook.ActiveSheet = document.ActiveSheet.Name;

            foreach (var documentWorksheet in document.Worksheets)
            {
                var sheet = workbook.AddSheet();

                sheet.Name = documentWorksheet.Name;

                sheet.ActiveCell = NameConverter.ConvertCellIndexToName(documentWorksheet.ViewState.SelectionState.ActiveCellIndex);

                var selection = documentWorksheet.ViewState.SelectionState.SelectedRanges.First();
                sheet.Selection = NameConverter.ConvertCellRangeToName(selection.FromIndex, selection.ToIndex);

                sheet.Columns = GetColumns(documentWorksheet).ToList();

                sheet.Rows = documentWorksheet.ImportRows().ToList();

                sheet.MergedCells = GetMergedCells(documentWorksheet).ToList();

                var pane = documentWorksheet.ViewState.Pane;
                if (pane != null && pane.State == PaneState.Frozen)
                {
                    sheet.FrozenRows = pane.TopLeftCellIndex.RowIndex;
                    sheet.FrozenColumns = pane.TopLeftCellIndex.ColumnIndex;
                }

                sheet.Sort = GetSorting(documentWorksheet);
            }

            return workbook;
        }
Пример #2
0
        /// <summary>
        /// Converts the Workbook to a <see cref="Telerik.Windows.Documents.Spreadsheet.Model.Workbook">Telerik DPL Workbook Document</see>.
        /// </summary>
        /// <returns></returns>
        public Document ToDocument()
        {
            var document = new Document();
            document.History.IsEnabled = false;

            using (new UpdateScope(document.SuspendLayoutUpdate, document.ResumeLayoutUpdate))
            {
                foreach (var sheet in Sheets.GetOrDefault())
                {
                    var documentSheet = document.Worksheets.Add();

                    if (!string.IsNullOrEmpty(sheet.Name))
                    {
                        documentSheet.Name = sheet.Name;
                    }

                    if (sheet.Name == ActiveSheet)
                    {
                        document.ActiveWorksheet = documentSheet;
                    }

                    documentSheet.ViewState.SelectionState = CreateSelectionState(sheet, documentSheet);

                    foreach (var row in sheet.Rows.GetOrDefault())
                    {
                        SetCells(row, documentSheet);

                        if (row.Height > 0)
                        {
                            documentSheet.Rows[row.Index.Value].SetHeight(new RowHeight(row.Height.Value, true));
                        }
                    }

                    foreach (var column in sheet.Columns.GetOrDefault())
                    {
                        if (ColumnsPropertyBag.WidthProperty.DefaultValue.Value != column.Width)
                        {
                            documentSheet.Columns[column.Index.Value].SetWidth(new ColumnWidth(column.Width.Value, true));
                        }
                    }

                    foreach (var mergedRange in sheet.MergedCells.GetOrDefault())
                    {
                        documentSheet.Cells.GetCellSelection(mergedRange).Merge();
                    }

                    if (sheet.FrozenColumns.GetValueOrDefault() > 0 || sheet.FrozenRows.GetValueOrDefault() > 0)
                    {
                        documentSheet.ViewState.FreezePanes(sheet.FrozenRows.GetValueOrDefault(), sheet.FrozenColumns.GetValueOrDefault());
                    }

                    SetSortState(documentSheet, sheet.Sort);
                }

                if (document.Worksheets.Count > 0)
                {
                    document.ActiveWorksheet = document.Worksheets[0];
                }
            }

            document.History.IsEnabled = true;

            return document;
        }