EXCEL列名与数字之间转换
来源:互联网 发布:手机赌博软件开发 编辑:程序博客网 时间:2024/05/14 13:34
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;
}
}
}
- EXCEL列名与数字之间转换
- Excel 列名与列号之间的相互转换
- Excel列名与数字的相互转换
- -------------------------数字转换成EXCEL列名---------------------------------
- 字母和数字的转换——Excel列名
- 字母和数字的转换——Excel列名
- excel列名和序列之间的转换
- 【算法】Excel中的列名与数字的对应
- Python Excel 列名转换
- Excel列名与uint的相互转换
- Sheet列号与数字之间的相互转换
- C#中 Excel列字母与数字的转换
- leetcode excel表格列标题数字与字符串的转换
- Matlab和Excel交互——数字和列名转换
- 数字转换成excel列名(10进制到Excel的26进制的转换函数)
- 使用VBA对Excel中列名称与列号之间进行转换
- C#实现Excel列索引与相应的列字母之间的转换
- VBA 代表列名转换:数字转换成字母
- vim 教程
- HDU 1556 - Color the ball
- MySQL数据库视图
- jsp---->监听器
- asp.net c# mysql存储过程
- EXCEL列名与数字之间转换
- DB_LINK
- 关于短延迟 sleep usleep nanosleep select
- crosstool-ng 编译出错Missing: 'i686-pc-linux-gnu-gcj'or 'i686-pc-linux-gnu-gcj' or 'gcj' : either needed
- 人件--读书笔记4
- HDU 1006
- 分布式系统的理解
- Oralce SQL语言常用函数(三)日期函数
- s3c2440的lcd原理及编程是实现