Пример #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()
        {
            int maxColumn = 0;

            // process each row
            foreach (var rowNode in WorksheetXml.XPathSelectElements("//d:sheetData/d:row", NameSpaceManager))
            {
                int row   = Convert.ToInt32(rowNode.AttributeValue("r"));
                int count = 0;
                // process each cell in current row
                foreach (var colNode in rowNode.XPathSelectElements("./d:c", NameSpaceManager))
                {
                    XAttribute colNumber = (XAttribute)colNode.Attribute(tempColumnNumberTag);
                    if (colNumber != null)
                    {
                        count++;
                        if (count > maxColumn)
                        {
                            maxColumn = count;
                        }
                        int        col         = Convert.ToInt32(colNumber.Value);
                        string     cellAddress = ExcelCell.GetColumnLetter(col) + row.ToString();
                        XAttribute attr        = new XAttribute("r", "");
                        if (attr != null)
                        {
                            attr.Value = cellAddress;
                            // the cellAddress needs to be the first attribute, otherwise Excel complains
                            if (!colNode.Attributes().Any())
                            {
                                colNode.Add(attr);
                            }
                            else
                            {
                                //var firstAttr =colNode.Attributes().First();
                                colNode.AddFirst(attr);
                            }
                        }
                        // remove all numeric cell addresses added by AddNumericCellIDs
                        colNumber.Remove();
                    }
                }
            }

            // process each row and add the spans attribute
            // TODO: Need to add proper spans handling.
            //foreach (var rowNode in XmlDoc.SelectNodes("//d:sheetData/d:row", NameSpaceManager))
            //{
            //  // we must add or update the "spans" attribute of each row
            //  XAttribute spans = (XAttribute)rowNode.Attributes.GetNamedItem("spans");
            //  if (spans == null)
            //  {
            //    spans = XmlDoc.CreateAttribute("spans");
            //    rowNode.Add(spans);
            //  }
            //  spans.Value = "1:" + maxColumn.ToString();
            //}
        }
Пример #2
0
        /// <summary>
        /// Get a range reference in Excel format
        /// e.g. GetRange("sheet", 1,5,10,2) => "sheet!$J$1:$K$5"
        /// </summary>
        /// <param name="worksheet"></param>
        /// <param name="startRow"></param>
        /// <param name="rowCount">Number of rows to include, >=1</param>
        /// <param name="startCol"></param>
        /// <param name="colCount">Number of columns to include, >=1</param>
        /// <returns></returns>
        public static String GetRangeRef(String worksheet,
                                         int startRow, int rowCount, int startCol, int colCount)
        {
            // Be tolerant
            if (rowCount <= 0)
            {
                rowCount = 1;
            }
            if (colCount <= 0)
            {
                colCount = 1;
            }

            return(worksheet + "!" +
                   "$" + ExcelCell.GetColumnLetter(startCol) + "$" + startRow + ":" +
                   "$" + ExcelCell.GetColumnLetter(startCol + colCount - 1) + "$" + (startRow + rowCount - 1));
        }