Click here to Skip to main content
15,885,546 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel

Getting the Excel Range object from the Clipboard through the IStream interface

Rate me:
Please Sign up or sign in to vote.
4.83/5 (5 votes)
25 Jan 2011CPOL3 min read 37.5K   650   10  
This article demonstrates how to get the Excel Range object from the Clipboard using the CF_LINKSOURCE Clipboard format.
using System;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace AM.Interop.Excel
{
    public class RangeName
    {
        public static string GetColumnName(int columnNumber)
        {
            if (columnNumber <= 0)
                throw new ArgumentOutOfRangeException("columnNumber");

            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }

        public static int GetColumnNumber(string columnName)
        {
            int columnNumber = 0;
            int pow = 1;
            for (int i = columnName.Length - 1; i >= 0; i--)
            {
                columnNumber += (columnName[i] - 'A' + 1) * pow;
                pow *= 26;
            }

            return columnNumber;
        }

        public static string R1C1ToA1(string R1C1Name)
        {
            string[] indices = R1C1Name.Split('R', 'C');
            if (indices.Length == 3)
            {
                int columnNumber = int.Parse(indices[2]);
                return GetColumnName(columnNumber) + indices[1];
            }
            else if (indices.Length == 2 && R1C1Name.Contains("C"))
            {
                int columnNumber = int.Parse(indices[1]);
                return GetColumnName(columnNumber);
            }
            else if (indices.Length == 2 && R1C1Name.Contains("R"))
            {
                return indices[1];
            }
            else
                throw new ApplicationException("Can't parse R1C1Name");
        }

        public static string A1ToR1C1(string A1Name)
        {
            string[] indices = Regex.Split(A1Name, @"(\D+)");
            if (indices.Length == 2)
            {
                return "R" + indices[1] + "C" + GetColumnNumber(indices[0]).ToString();
            }
            else if (indices.Length == 1 && char.IsDigit(indices[0][0]))
            {
                return "R" + indices[0];
            }
            else if (indices.Length == 1 && char.IsLetter(indices[0][0]))
            {
                return "C" + GetColumnNumber(indices[0]).ToString();
            }
            else
                throw new ApplicationException("Can't parse A1Name");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions