EXCEL列名与数字之间转换

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SDLearn1 {
    public partial class LearnForm : Form {


        private const int AlphaSpan = 26;   // 英文字母数

        public LearnForm() {
            InitializeComponent();
        }

        private void CalcButton_Click(object sender, EventArgs e) {
            var source = this.EntryTextBox.Text;
          
            try {
                if(string.IsNullOrEmpty(source) || source.Trim() == "") {
                    MessageBox.Show("入力がありません", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                } else if(IsNumericOnly(source.Trim())) {
                    // 数字のみ

                    var result = GetColumnName(source.Trim());
                    MessageBox.Show(result);
                } else if(IsAlphaOnly(source.Trim())) {
                    // アルファベットのみ

                    var result = GetColumnNumber(source.Trim());
                    MessageBox.Show(result);
                } else {
                    // 上記以外の不正な文字を含む場合
                    MessageBox.Show("不正な文字が含まれています", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            } catch(ArgumentException ex) {
                MessageBox.Show(ex.Message, "パラメータエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
            } catch(Exception ex) {
                MessageBox.Show(ex.Message, "想定外の例外", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 全部数字か
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private bool IsNumericOnly(string source) {

            foreach(var ch in source) {
                if(ch < '0' || ch > '9') {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 全部アルファベットか
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private bool IsAlphaOnly(string source) {

            foreach(var ch in source.ToUpper()) {
                if(ch < 'A' || ch > 'Z') {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// カラム番号の取得
        /// </summary>
        /// <param name="source">カラム名を表す文字列</param>
        /// <returns>変換したカラム番号</returns>
        private string GetColumnNumber(string source) {

            var s = source.ToUpper().Trim();

            if(s.Length >= 5) {
                // 桁数が多い
                throw new ArgumentOutOfRangeException("source");
            }

            int no = 0;
            foreach(var ch in s) {
                var n = ch - 'A';

                no = no * AlphaSpan + n + 1;
            }

            return no.ToString();
        }

        /// <summary>
        /// カラム名の取得
        /// </summary>
        /// <param name="source">カラム番号を表す文字列</param>
        /// <returns>変換したカラム名</returns>
        private string GetColumnName(string source) {

            int s;
            if(!int.TryParse(source, out s)) {
                throw new ArgumentException();
            }

            if(s <= 0){
                // 0以下は対象外
                throw new ArgumentOutOfRangeException("source");
            }

            string name = "";
            while(s > 0) {
                s--;
                int rest = s % AlphaSpan;

                name = (char)(rest + 'A') + name;
                s = s / AlphaSpan;
            }

            return name;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值