Exemplo n.º 1
0
        private static List <CopiedCell> GetCopiedValues(ExcelRangeBase sourceRange, ExcelRangeBase Destination, ExcelRangeCopyOptionFlags?excelRangeCopyOptionFlags)
        {
            var worksheet = sourceRange._worksheet;
            var toRow     = sourceRange._toRow;
            var toCol     = sourceRange._toCol;
            var fromRow   = sourceRange._fromRow;
            var fromCol   = sourceRange._fromCol;

            int    i    = 0;
            object o    = null;
            byte   flag = 0;
            Uri    hl   = null;

            var excludeFormulas = (excelRangeCopyOptionFlags ?? 0 & ExcelRangeCopyOptionFlags.ExcludeFormulas) == ExcelRangeCopyOptionFlags.ExcludeFormulas;

            var                   copiedValue = new List <CopiedCell>();
            ExcelStyles           sourceStyles = worksheet.Workbook.Styles, styles = Destination._worksheet.Workbook.Styles;
            Dictionary <int, int> styleCashe   = new Dictionary <int, int>();
            bool                  sameWorkbook = Destination._worksheet.Workbook == sourceRange._worksheet.Workbook;

            var cse = new CellStoreEnumerator <ExcelValue>(worksheet._values, fromRow, fromCol, toRow, toCol);

            while (cse.Next())
            {
                var row  = cse.Row;
                var col  = cse.Column;      //Issue 15070
                var cell = new CopiedCell
                {
                    Row    = Destination._fromRow + (row - fromRow),
                    Column = Destination._fromCol + (col - fromCol),
                    Value  = cse.Value._value
                };

                if (!excludeFormulas && worksheet._formulas.Exists(row, col, ref o))
                {
                    if (o is int)
                    {
                        cell.Formula = worksheet.GetFormula(cse.Row, cse.Column);
                        if (worksheet._flags.GetFlagValue(cse.Row, cse.Column, CellFlags.ArrayFormula))
                        {
                            Destination._worksheet._flags.SetFlagValue(cse.Row, cse.Column, true, CellFlags.ArrayFormula);
                        }
                    }
                    else
                    {
                        cell.Formula = o;
                    }
                }
                if (worksheet.ExistsStyleInner(row, col, ref i))
                {
                    if (sameWorkbook)
                    {
                        cell.StyleID = i;
                    }
                    else
                    {
                        if (styleCashe.ContainsKey(i))
                        {
                            i = styleCashe[i];
                        }
                        else
                        {
                            var oldStyleID = i;
                            i = styles.CloneStyle(sourceStyles, i);
                            styleCashe.Add(oldStyleID, i);
                        }
                        cell.StyleID = i;
                    }
                }

                if (worksheet._hyperLinks.Exists(row, col, ref hl))
                {
                    cell.HyperLink = hl;
                }

                // Will just be null if no comment exists.
                cell.Comment = worksheet.Cells[cse.Row, cse.Column].Comment;

                if (worksheet._flags.Exists(row, col, ref flag))
                {
                    cell.Flag = flag;
                }
                copiedValue.Add(cell);
            }

            //Copy styles with no cell value
            var cses = new CellStoreEnumerator <ExcelValue>(worksheet._values, fromRow, fromCol, toRow, toCol);

            while (cses.Next())
            {
                if (!worksheet.ExistsValueInner(cses.Row, cses.Column))
                {
                    var row  = Destination._fromRow + (cses.Row - fromRow);
                    var col  = Destination._fromCol + (cses.Column - fromCol);
                    var cell = new CopiedCell
                    {
                        Row    = row,
                        Column = col,
                        Value  = null
                    };

                    i = cses.Value._styleId;
                    if (sameWorkbook)
                    {
                        cell.StyleID = i;
                    }
                    else
                    {
                        if (styleCashe.ContainsKey(i))
                        {
                            i = styleCashe[i];
                        }
                        else
                        {
                            var oldStyleID = i;
                            i = styles.CloneStyle(sourceStyles, i);
                            styleCashe.Add(oldStyleID, i);
                        }
                        cell.StyleID = i;
                    }
                    copiedValue.Add(cell);
                }
            }

            return(copiedValue);
        }