Example #1
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()
        {
            var maxColumn = 0;

            // process each row
            foreach (XmlNode rowNode in WorksheetXml.SelectNodes("//d:sheetData/d:row", NameSpaceManager))
            {
                var row   = Convert.ToInt32(rowNode.Attributes.GetNamedItem("r").Value);
                var count = 0;
                // process each cell in current row
                foreach (XmlNode colNode in rowNode.SelectNodes("./d:c", NameSpaceManager))
                {
                    var colNumber = (XmlAttribute)colNode.Attributes.GetNamedItem(tempColumnNumberTag);
                    if (colNumber != null)
                    {
                        count++;
                        if (count > maxColumn)
                        {
                            maxColumn = count;
                        }
                        var col         = Convert.ToInt32(colNumber.Value);
                        var cellAddress = ExcelCell.GetColumnLetter(col) + row;
                        var 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();
            //}
        }