Beispiel #1
0
        internal ExcelCell Clone(ExcelWorksheet added, int row, int col)
        {
            ExcelCell newCell = new ExcelCell(added, row, col);

            if (_hyperlink != null)
            {
                newCell.Hyperlink = Hyperlink;
            }
            newCell._formula         = _formula;
            newCell._formulaR1C1     = _formulaR1C1;
            newCell.IsRichText       = IsRichText;
            newCell.Merge            = Merge;
            newCell._sharedFormulaID = _sharedFormulaID;
            newCell._styleName       = _styleName;
            newCell._styleID         = _styleID;
            newCell._value           = _value;
            return(newCell);
        }
Beispiel #2
0
        private void CloneCells(ExcelWorksheet Copy, ExcelWorksheet added)
        {
            bool sameWorkbook = (Copy.Workbook == _pck.Workbook);

            bool doAdjust = _pck.DoAdjustDrawings;

            _pck.DoAdjustDrawings = false;
            added.MergedCells.List.AddRange(Copy.MergedCells.List);
            //Formulas
            foreach (IRangeID f in Copy._formulaCells)
            {
                added._formulaCells.Add(f);
            }
            //Shared Formulas
            foreach (int key in Copy._sharedFormulas.Keys)
            {
                added._sharedFormulas.Add(key, Copy._sharedFormulas[key]);
            }

            Dictionary <int, int> styleCashe = new Dictionary <int, int>();

            //Cells
            foreach (ExcelCell cell in Copy._cells)
            {
                if (sameWorkbook)   //Same workbook == same styles
                {
                    added._cells.Add(cell.Clone(added));
                }
                else
                {
                    ExcelCell addedCell = cell.Clone(added);
                    if (styleCashe.ContainsKey(cell.StyleID))
                    {
                        addedCell.StyleID = styleCashe[cell.StyleID];
                    }
                    else
                    {
                        addedCell.StyleID = added.Workbook.Styles.CloneStyle(Copy.Workbook.Styles, cell.StyleID);
                        if (cell.StyleName != "") //Named styles
                        {
                            if (!Copy.Workbook.Styles.NamedStyles.ExistsKey(cell.StyleName))
                            {
                                var ns = Copy.Workbook.Styles.CreateNamedStyle(cell.StyleName);
                                ns.StyleXfId = addedCell.StyleID;
                            }
                        }
                        styleCashe.Add(cell.StyleID, addedCell.StyleID);
                    }
                    added._cells.Add(addedCell);
                }
            }
            //Rows
            foreach (ExcelRow row in Copy._rows)
            {
                row.Clone(added);
                if (!sameWorkbook)   //Same workbook == same styles
                {
                    ExcelRow addedRow = added.Row(row.Row) as ExcelRow;
                    if (styleCashe.ContainsKey(row.StyleID))
                    {
                        addedRow.StyleID = styleCashe[row.StyleID];
                    }
                    else
                    {
                        addedRow.StyleID = added.Workbook.Styles.CloneStyle(Copy.Workbook.Styles, addedRow.StyleID);
                        if (row.StyleName != "") //Named styles
                        {
                            if (!Copy.Workbook.Styles.NamedStyles.ExistsKey(row.StyleName))
                            {
                                var ns = Copy.Workbook.Styles.CreateNamedStyle(row.StyleName);
                                ns.StyleXfId = addedRow.StyleID;
                            }
                        }
                        styleCashe.Add(row.StyleID, addedRow.StyleID);
                    }
                }
            }
            //Columns
            foreach (ExcelColumn col in Copy._columns)
            {
                col.Clone(added);
                if (!sameWorkbook)   //Same workbook == same styles
                {
                    ExcelColumn addedCol = added.Column(col.ColumnMin) as ExcelColumn;
                    if (styleCashe.ContainsKey(col.StyleID))
                    {
                        addedCol.StyleID = styleCashe[col.StyleID];
                    }
                    else
                    {
                        addedCol.StyleID = added.Workbook.Styles.CloneStyle(Copy.Workbook.Styles, addedCol.StyleID);
                        if (col.StyleName != "") //Named styles
                        {
                            if (!Copy.Workbook.Styles.NamedStyles.ExistsKey(col.StyleName))
                            {
                                var ns = Copy.Workbook.Styles.CreateNamedStyle(col.StyleName);
                                ns.StyleXfId = addedCol.StyleID;
                            }
                        }
                        styleCashe.Add(col.StyleID, addedCol.StyleID);
                    }
                }
            }
            added._package.DoAdjustDrawings = doAdjust;
        }
Beispiel #3
0
        private void SetStyleAddress(StyleBase sender, Style.StyleChangeEventArgs e, ExcelAddressBase address, ExcelWorksheet ws, ref Dictionary <int, int> styleCashe)
        {
            if (address.Start.Column == 0 || address.Start.Row == 0)
            {
                throw (new Exception("error address"));
            }
            //Columns
            else if (address.Start.Row == 1 && address.End.Row == ExcelPackage.MaxRows)
            {
                ExcelColumn column;
                //Get the startcolumn
                ulong colID = ExcelColumn.GetColumnID(ws.SheetID, address.Start.Column);
                if (!ws._columns.ContainsKey(colID))
                {
                    column = ws.Column(address.Start.Column);
                }
                else
                {
                    column = ws._columns[colID] as ExcelColumn;
                }

                var index = ws._columns.IndexOf(colID);
                while (column.ColumnMin <= address.End.Column)
                {
                    if (column.ColumnMax > address.End.Column)
                    {
                        var newCol = ws.CopyColumn(column, address.End.Column + 1, column.ColumnMax);
                        column.ColumnMax = address.End.Column;
                    }

                    if (styleCashe.ContainsKey(column.StyleID))
                    {
                        column.StyleID = styleCashe[column.StyleID];
                    }
                    else
                    {
                        ExcelXfs st    = CellXfs[column.StyleID];
                        int      newId = st.GetNewID(CellXfs, sender, e.StyleClass, e.StyleProperty, e.Value);
                        styleCashe.Add(column.StyleID, newId);
                        column.StyleID = newId;
                    }

                    index++;
                    if (index >= ws._columns.Count)
                    {
                        break;
                    }
                    else
                    {
                        column = (ws._columns[index] as ExcelColumn);
                    }
                }

                if (column._columnMax < address.End.Column)
                {
                    var newCol = ws.Column(column._columnMax + 1) as ExcelColumn;
                    newCol._columnMax = address.End.Column;

                    if (styleCashe.ContainsKey(newCol.StyleID))
                    {
                        newCol.StyleID = styleCashe[newCol.StyleID];
                    }
                    else
                    {
                        ExcelXfs st    = CellXfs[column.StyleID];
                        int      newId = st.GetNewID(CellXfs, sender, e.StyleClass, e.StyleProperty, e.Value);
                        styleCashe.Add(newCol.StyleID, newId);
                        newCol.StyleID = newId;
                    }

                    //column._columnMax = address.End.Column;
                }

                //Set for individual cells in the spann. We loop all cells here since the cells are sorted with columns first.
                foreach (ExcelCell cell in ws._cells)
                {
                    if (cell.Column >= address.Start.Column &&
                        cell.Column <= address.End.Column)
                    {
                        if (styleCashe.ContainsKey(cell.StyleID))
                        {
                            cell.StyleID = styleCashe[cell.StyleID];
                        }
                        else
                        {
                            ExcelXfs st    = CellXfs[cell.StyleID];
                            int      newId = st.GetNewID(CellXfs, sender, e.StyleClass, e.StyleProperty, e.Value);
                            styleCashe.Add(cell.StyleID, newId);
                            cell.StyleID = newId;
                        }
                    }
                }
            }
            //Rows
            else if (address.Start.Column == 1 && address.End.Column == ExcelPackage.MaxColumns)
            {
                for (int rowNum = address.Start.Row; rowNum <= address.End.Row; rowNum++)
                {
                    ExcelRow row = ws.Row(rowNum);
                    if (row.StyleID == 0 && ws._columns.Count > 0)
                    {
                        //TODO: We should loop all columns here and change each cell. But for now we take style of column A.
                        foreach (ExcelColumn column in ws._columns)
                        {
                            row.StyleID = column.StyleID;
                            break;  //Get the first one and break.
                        }
                    }
                    if (styleCashe.ContainsKey(row.StyleID))
                    {
                        row.StyleID = styleCashe[row.StyleID];
                    }
                    else
                    {
                        ExcelXfs st    = CellXfs[row.StyleID];
                        int      newId = st.GetNewID(CellXfs, sender, e.StyleClass, e.StyleProperty, e.Value);
                        styleCashe.Add(row.StyleID, newId);
                        row.StyleID = newId;
                    }
                }

                //Get Start Cell
                ulong rowID = ExcelRow.GetRowID(ws.SheetID, address.Start.Row);
                int   index = ws._cells.IndexOf(rowID);

                index = ~index;
                while (index < ws._cells.Count)
                {
                    var cell = ws._cells[index] as ExcelCell;
                    if (cell.Row > address.End.Row)
                    {
                        break;
                    }
                    if (styleCashe.ContainsKey(cell.StyleID))
                    {
                        cell.StyleID = styleCashe[cell.StyleID];
                    }
                    else
                    {
                        ExcelXfs st    = CellXfs[cell.StyleID];
                        int      newId = st.GetNewID(CellXfs, sender, e.StyleClass, e.StyleProperty, e.Value);
                        styleCashe.Add(cell.StyleID, newId);
                        cell.StyleID = newId;
                    }
                    index++;
                }
            }
            else             //Cellrange
            {
                for (int col = address.Start.Column; col <= address.End.Column; col++)
                {
                    for (int row = address.Start.Row; row <= address.End.Row; row++)
                    {
                        ExcelCell cell = ws.Cell(row, col);
                        if (styleCashe.ContainsKey(cell.StyleID))
                        {
                            cell.StyleID = styleCashe[cell.StyleID];
                        }
                        else
                        {
                            ExcelXfs st    = CellXfs[cell.StyleID];
                            int      newId = st.GetNewID(CellXfs, sender, e.StyleClass, e.StyleProperty, e.Value);
                            styleCashe.Add(cell.StyleID, newId);
                            cell.StyleID = newId;
                        }
                    }
                }
            }
        }