C # Excel columns are converted to numbers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.