Reprint: http://www.cnblogs.com/msgarden/p/5129927.html
Using system;using system.collections.generic;using system.linq;using system.text;using System.Text.Regularexpression_rs; namespace hooogle{public static class Excelconvert {#region-converted from a number to a column letter in Excel-public stat IC int Toindex (string columnName) {if (! Regex.IsMatch (Columnname.toupper (), @ "[a-z]+")) {throw new Exception ("Invalid parameter");} int index = 0; char[] chars = Columnname.toupper (). ToCharArray (); for (int i = 0; i < chars. Length; i++) {Index + = ((int) chars[i]-(int) ' A ' + 1) * (int) Math.pow (chars. Length-i-1); } return index-1; } public static string toname (int index) {if (Index < 0) {throw new Exception ("Inva Lid parameter "); } list<string> chars = new list<string> (); do {if (chars). Count > 0) index--; Chars. Insert (0, ((char) (index% + (int) ' A ')). ToString ()); index = (int) ((index-index% 26)/26); } while (index > 0); Return String.Join (String. Empty, chars. ToArray ()); } #endregion}}
C # Excel columns are converted to numbers