Пример #1
0
        internal static T GetOrCreateBefore <T>(this OpenXmlCompositeElement element, OpenXmlElement before) where T : OpenXmlElement, new()
        {
            if (!element.Has <T>())
            {
                element.InsertBefore(new T(), before);
            }

            return(element.Elements <T>().First());
        }
Пример #2
0
        public Excel.Row GetRow(OpenXmlCompositeElement sheetData, int r, bool check, Excel.Row cloning)
        {
            Excel.Row rez = null;
            if (check)
            {
                foreach (OpenXmlCompositeElement row in sheetData)
                {
                    rez = row as Excel.Row;
                    if (rez != null && rez.RowIndex != null && rez.RowIndex.Value == r)
                    {
                        break;
                    }
                }
            }
            if (rez == null || rez.RowIndex.Value != r)
            {
                if (cloning != null)
                {
                    Excel.Row parent = null;
                    if (!check)
                    {
                        foreach (OpenXmlCompositeElement row in sheetData)
                        {
                            rez = row as Excel.Row;
                            if (rez != null && rez.RowIndex != null && rez.RowIndex.Value == r)
                            {
                                parent = rez;
                                break;
                            }
                        }
                    }

                    rez = CloneRow(cloning, r);

                    if (parent != null)
                    {
                        parent.RowIndex = parent.RowIndex.Value + 1;
                        sheetData.InsertBefore <Excel.Row>(rez, parent);
                    }
                    else
                    {
                        sheetData.Append(rez);
                    }
                }
                else
                {
                    rez = new Excel.Row()
                    {
                        RowIndex = (uint)r
                    };
                    sheetData.Append(rez);
                }
            }
            return(rez);
        }
Пример #3
0
        public Excel.Cell GetCell(OpenXmlCompositeElement row, object value, int c, int r, uint styleIndex, StringKeyList sharedStrings)
        {
            string reference = Helper.IntToChar(c) + r.ToString();

            Excel.Cell cell = null;

            if (row != null)
            {
                foreach (var rowCell in row.Elements <Excel.Cell>())
                {
                    var cref = CellReference.Parse(rowCell.CellReference.Value);
                    if (cref.Col == c)
                    {
                        cell = rowCell;
                        break;
                    }
                    else if (cref.Col > c)
                    {
                        cell = new Excel.Cell()
                        {
                            CellReference = reference, StyleIndex = styleIndex
                        };
                        row.InsertBefore <Excel.Cell>(cell, rowCell);
                        break;
                    }
                }
            }
            if (cell == null || cell.CellReference.Value != reference)
            {
                cell = new Excel.Cell()
                {
                    CellReference = reference, StyleIndex = styleIndex
                };
                if (row != null)
                {
                    row.AppendChild <Excel.Cell>(cell);
                }
            }

            WriteCell(cell, value, sharedStrings);

            return(cell);
        }