Exemple #1
0
        /// <summary>
        /// Adds numeric cell identifiers so that it is easier to work out position of cells
        /// Private method, for internal use only!
        /// </summary>
        private void AddNumericCellIDs()
        {
            // process each row
            foreach (XmlNode rowNode in WorksheetXml.SelectNodes("//d:sheetData/d:row", NameSpaceManager))
            {
                // remove the spans attribute.  Excel simply recreates it when the file is opened.
                XmlAttribute attr = (XmlAttribute)rowNode.Attributes.GetNamedItem("spans");
                if (attr != null)
                {
                    rowNode.Attributes.Remove(attr);
                }

                int row = Convert.ToInt32(rowNode.Attributes.GetNamedItem("r").Value);
                // process each cell in current row
                foreach (XmlNode colNode in rowNode.SelectNodes("./d:c", NameSpaceManager))
                {
                    XmlAttribute cellAddressAttr = (XmlAttribute)colNode.Attributes.GetNamedItem("r");
                    if (cellAddressAttr != null)
                    {
                        string cellAddress = cellAddressAttr.Value;

                        int col = ExcelCell.GetColumnNumber(cellAddress);
                        attr = WorksheetXml.CreateAttribute(tempColumnNumberTag);
                        if (attr != null)
                        {
                            attr.Value = col.ToString();
                            colNode.Attributes.Append(attr);
                            // remove all cell Addresses like A1, A2, A3 etc.
                            colNode.Attributes.Remove(cellAddressAttr);
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Inserts a new row into the spreadsheet.  Existing rows below the insersion position are
        /// shifted down.  All formula are updated to take account of the new row.
        /// </summary>
        /// <param name="position">The position of the new row</param>
        public void InsertRow(int position)
        {
            // create the new row element
            var rowElement = WorksheetXml.CreateElement("row", ExcelPackage.schemaMain);

            rowElement.Attributes.Append(WorksheetXml.CreateAttribute("r"));
            rowElement.Attributes["r"].Value = position.ToString();

            var sheetDataNode = WorksheetXml.SelectSingleNode("//d:sheetData", NameSpaceManager);

            if (sheetDataNode != null)
            {
                var     renumberFrom         = 1;
                var     nodes                = sheetDataNode.ChildNodes;
                var     nodeCount            = nodes.Count;
                XmlNode insertAfterRowNode   = null;
                var     insertAfterRowNodeID = 0;
                for (var i = 0; i < nodeCount; i++)
                {
                    var currentRowID = int.Parse(nodes[i].Attributes["r"].Value);
                    if (currentRowID < position)
                    {
                        insertAfterRowNode   = nodes[i];
                        insertAfterRowNodeID = i;
                    }
                    if (currentRowID >= position)
                    {
                        renumberFrom = currentRowID;
                        break;
                    }
                }

                // update the existing row ids
                for (var i = insertAfterRowNodeID + 1; i < nodeCount; i++)
                {
                    var currentRowID = int.Parse(nodes[i].Attributes["r"].Value);
                    if (currentRowID >= renumberFrom)
                    {
                        nodes[i].Attributes["r"].Value = Convert.ToString(currentRowID + 1);

                        // now update any formula that are in the row
                        var formulaNodes = nodes[i].SelectNodes("./d:c/d:f", NameSpaceManager);
                        foreach (XmlNode formulaNode in formulaNodes)
                        {
                            formulaNode.InnerText = ExcelCell.UpdateFormulaReferences(formulaNode.InnerText,
                                                                                      1,
                                                                                      0,
                                                                                      position,
                                                                                      0);
                        }
                    }
                }

                // now insert the new row
                if (insertAfterRowNode != null)
                {
                    sheetDataNode.InsertAfter(rowElement, insertAfterRowNode);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Replaces the numeric cell identifiers we inserted with AddNumericCellIDs with the traditional
        /// A1, A2 cell identifiers that Excel understands.
        /// Private method, for internal use only!
        /// </summary>
        private void ReplaceNumericCellIDs()
        {
            int maxColumn = 0;

            // process each row
            foreach (XmlNode rowNode in WorksheetXml.SelectNodes("//d:sheetData/d:row", NameSpaceManager))
            {
                int row   = Convert.ToInt32(rowNode.Attributes.GetNamedItem("r").Value);
                int count = 0;
                // process each cell in current row
                foreach (XmlNode colNode in rowNode.SelectNodes("./d:c", NameSpaceManager))
                {
                    XmlAttribute colNumber = (XmlAttribute)colNode.Attributes.GetNamedItem(tempColumnNumberTag);
                    if (colNumber != null)
                    {
                        count++;
                        if (count > maxColumn)
                        {
                            maxColumn = count;
                        }
                        int          col         = Convert.ToInt32(colNumber.Value);
                        string       cellAddress = ExcelCell.GetColumnLetter(col) + row.ToString();
                        XmlAttribute attr        = WorksheetXml.CreateAttribute("r");
                        if (attr != null)
                        {
                            attr.Value = cellAddress;
                            // the cellAddress needs to be the first attribute, otherwise Excel complains
                            if (colNode.Attributes.Count == 0)
                            {
                                colNode.Attributes.Append(attr);
                            }
                            else
                            {
                                colNode.Attributes.InsertBefore(attr, (XmlAttribute)colNode.Attributes.Item(0));
                            }
                        }
                        // remove all numeric cell addresses added by AddNumericCellIDs
                        colNode.Attributes.Remove(colNumber);
                    }
                }
            }

            // process each row and add the spans attribute
            // TODO: Need to add proper spans handling.
            //foreach (XmlNode rowNode in XmlDoc.SelectNodes("//d:sheetData/d:row", NameSpaceManager))
            //{
            //  // we must add or update the "spans" attribute of each row
            //  XmlAttribute spans = (XmlAttribute)rowNode.Attributes.GetNamedItem("spans");
            //  if (spans == null)
            //  {
            //    spans = XmlDoc.CreateAttribute("spans");
            //    rowNode.Attributes.Append(spans);
            //  }
            //  spans.Value = "1:" + maxColumn.ToString();
            //}
        }
Exemple #4
0
        /// <summary>
        /// Inserts conditional formatting for the cell range.
        /// Currently only supports the dataBar style.
        /// </summary>
        /// <param name="startCell"></param>
        /// <param name="endCell"></param>
        /// <param name="color"></param>
        public void CreateConditionalFormatting(ExcelCell startCell, ExcelCell endCell, string color)
        {
            XmlNode formatNode = WorksheetXml.SelectSingleNode("//d:conditionalFormatting", NameSpaceManager);

            if (formatNode == null)
            {
                formatNode = WorksheetXml.CreateElement("conditionalFormatting", ExcelPackage.schemaMain);
                XmlNode prevNode = WorksheetXml.SelectSingleNode("//d:mergeCells", NameSpaceManager);
                if (prevNode == null)
                {
                    prevNode = WorksheetXml.SelectSingleNode("//d:sheetData", NameSpaceManager);
                }
                WorksheetXml.DocumentElement.InsertAfter(formatNode, prevNode);
            }
            XmlAttribute attr = formatNode.Attributes["sqref"];

            if (attr == null)
            {
                attr = WorksheetXml.CreateAttribute("sqref");
                formatNode.Attributes.Append(attr);
            }
            attr.Value = string.Format("{0}:{1}", startCell.CellAddress, endCell.CellAddress);

            XmlNode node = formatNode.SelectSingleNode("./d:cfRule", NameSpaceManager);

            if (node == null)
            {
                node = WorksheetXml.CreateElement("cfRule", ExcelPackage.schemaMain);
                formatNode.AppendChild(node);
            }

            attr = node.Attributes["type"];
            if (attr == null)
            {
                attr = WorksheetXml.CreateAttribute("type");
                node.Attributes.Append(attr);
            }
            attr.Value = "dataBar";

            attr = node.Attributes["priority"];
            if (attr == null)
            {
                attr = WorksheetXml.CreateAttribute("priority");
                node.Attributes.Append(attr);
            }
            attr.Value = "1";

            // the following is poor code, but just an example!!!
            XmlNode databar = WorksheetXml.CreateElement("databar", ExcelPackage.schemaMain);

            node.AppendChild(databar);

            XmlNode child = WorksheetXml.CreateElement("cfvo", ExcelPackage.schemaMain);

            databar.AppendChild(child);
            attr = WorksheetXml.CreateAttribute("type");
            child.Attributes.Append(attr);
            attr.Value = "min";
            attr       = WorksheetXml.CreateAttribute("val");
            child.Attributes.Append(attr);
            attr.Value = "0";

            child = WorksheetXml.CreateElement("cfvo", ExcelPackage.schemaMain);
            databar.AppendChild(child);
            attr = WorksheetXml.CreateAttribute("type");
            child.Attributes.Append(attr);
            attr.Value = "max";
            attr       = WorksheetXml.CreateAttribute("val");
            child.Attributes.Append(attr);
            attr.Value = "0";

            child = WorksheetXml.CreateElement("color", ExcelPackage.schemaMain);
            databar.AppendChild(child);
            attr = WorksheetXml.CreateAttribute("rgb");
            child.Attributes.Append(attr);
            attr.Value = color;
        }