相关资料:
http://www.cnblogs.com/5ihua/
ExcelHelper类主要是对Excel中的位置中【列】转换,如 public int Location(string location)方法传入参数“B”,则表示Excel中的第二列,即返回数字“2”,参数为“AA”,则返回27,以此类推。
而 public string Location(int location)方法相当于前一个方法的逆方法,即是将传入的列数字转换excel中对应的字母,举例:传入参数2,则该方法返回值为“B”。
下面附上源代码:
(
因在开发过程中,把位置信息存储到“程序集配置文件中”,即App.config,故对配置文件的读写也写了几个简单的方法。
优点:读写方便。
缺点:在程序中对app.config中的数据做出修改后,在配置文件中是看不出来的。
)
public class ExcelByHelper | |
{ | |
/// <summary> | |
/// 根据字母计算位置 | |
/// 例如:A:1,Z:26, AA:27, AZ:52 | |
/// </summary> | |
/// <param name="location">列位置字母</param> | |
/// <returns>对应的位置</returns> | |
public int Location(string location) | |
{ | |
int position = 0; | |
char[] @char = location.Trim().ToUpper().ToCharArray(0, location.Length); | |
for (int i = 0; i < @char.Length; i++) | |
{ | |
position += int.Parse(((@char[i] - 64) * Math.Pow(26, @char.Length - 1 - i)).ToString()); | |
} | |
return position; | |
} | |
/// <summary> | |
/// 根据位置计算出字母 | |
/// </summary> | |
/// <param name="location">位置</param> | |
/// <returns></returns> | |
public string Location(int location) | |
{ | |
string position = string.Empty; | |
if (location < 1) return position; | |
while (location > 0) | |
{ | |
location--; | |
int remainder = location % 26; | |
char digit = (char)(remainder + 65); | |
position = digit + position; | |
location = (location - remainder) / 26; | |
} | |
return position; | |
} | |
/// <summary> | |
/// 读取节点坐标 | |
/// </summary> | |
/// <param name="node">节点名</param> | |
/// <returns>坐标</returns> | |
public string GetAppSettingsValue(string node) | |
{ | |
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); | |
string msg = config.AppSettings.Settings[node].Value; | |
if (string.IsNullOrEmpty(msg)) | |
msg = string.Empty; | |
return msg; | |
} | |
/// <summary> | |
/// 更新节点 | |
/// </summary> | |
/// <param name="node">待更新节点名</param> | |
/// <param name="newvalue">更新后节点的值</param> | |
public void UpdateAppSettingsValue(string node, string newvalue) | |
{ | |
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); | |
//写入<add>元素的value | |
config.AppSettings.Settings[node].Value = newvalue; | |
//保存文件 | |
config.Save(ConfigurationSaveMode.Modified); | |
//刷新节点 | |
System.Configuration.ConfigurationManager.RefreshSection("AppSettings"); | |
} | |
/// <summary> | |
/// 添加节点 | |
/// </summary> | |
/// <param name="nodename">节点名</param> | |
/// <param name="value">节点值</param> | |
public void AddAppSettingsValue(string nodename, string value) | |
{ | |
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); | |
//增加节点 | |
config.AppSettings.Settings.Add(nodename, value); | |
//保存文件 | |
config.Save(ConfigurationSaveMode.Modified); | |
//刷新节点 | |
System.Configuration.ConfigurationManager.RefreshSection("AppSettings"); | |
} | |
/// <summary> | |
/// 移除节点 | |
/// </summary> | |
/// <param name="nodename">待移除节点名</param> | |
public void RemoveAppSettingsValue(string nodename) | |
{ | |
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); | |
//移除节点 | |
config.AppSettings.Settings.Remove(nodename); | |
//保存文件 | |
config.Save(ConfigurationSaveMode.Modified); | |
//刷新节点 | |
System.Configuration.ConfigurationManager.RefreshSection("AppSettings"); | |
} | |
} |
总结一下:字母A-Z一共26个,对应的ASCLL是65-90,其实,我们我们用到的数字就是26,65。在字母转数字方法思想就是将字母转成ASCLL码后,再作加减法。而将数字转字母时,也是将数字对26取余以及求商,将余数转化成字母,叠加成字符串即可。