zoukankan      html  css  js  c++  java
  • 黄聪:C#中 Excel列字母与数字的转换

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.Regularexpression_rs;

    namespace Hooogle
    {
        public static class ExcelConvert
        {
            #region - 由数字转换为Excel中的列字母 -
           
            public static 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(26, chars.Length - i - 1);
                }
                return index - 1;
            }

           
            public static string ToName(int index)
            {
                if (index < 0) { throw new Exception("invalid parameter"); }

                List<string> chars = new List<string>();
                do
                {
                    if (chars.Count > 0) index--;
                    chars.Insert(0, ((char)(index % 26 + (int)'A')).ToString());
                    index = (int)((index - index % 26) / 26);
                } while (index > 0);

                return String.Join(string.Empty, chars.ToArray());
            }
            #endregion
        }
    }

  • 相关阅读:
    2020寒假 学习进度笔记4:spark使用1
    2020寒假 学习进度笔记3:Spark安装
    2020寒假 学习进度笔记2:Scala学习
    2020寒假 学习进度笔记1:Scala安装(Windows + Lunux)
    测试试卷—数据清洗
    实验6:Mapreduce实例——WordCount
    个人课程总结
    如何打jar包
    初探性能优化——2个月到4小时的性能提升(copy)推荐阅读
    《阿里巴巴Java工作手册》学习笔记
  • 原文地址:https://www.cnblogs.com/huangcong/p/1697054.html
Copyright © 2011-2022 走看看