乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > excel服务器-Excel开发之旅(四)——ExcelHelper

excel服务器-Excel开发之旅(四)——ExcelHelper

作者:乔山办公网日期:

返回目录:excel表格制作

相关资料: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


{


///


/// 根据字母计算位置


/// 例如:A:1,Z:26, AA:27, AZ:52


///


/// 列位置字母


/// 对应的位置


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;


}


///


/// 根据位置计算出字母


///


/// 位置


///


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;


}


#region App.config文件读取和写入操作


///


/// 读取节点坐标


///


/// 节点名


/// 坐标


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;


}


///


/// 更新节点


///


/// 待更新节点名


/// 更新后节点的值


public void UpdateAppSettingsValue(string node, string newvalue)


{


Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);


//写入元素的value


config.AppSettings.Settings[node].Value = newvalue;


//保存文件


config.Save(ConfigurationSaveMode.Modified);


//刷新节点


System.Configuration.ConfigurationManager.RefreshSection("AppSettings");


}


///


/// 添加节点


///


/// 节点名


/// 节点值


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");


}


///


/// 移除节点


///


/// 待移除节点名


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");


}


#endregion


}


总结一下:字母A-Z一共26个,对应的ASCLL是65-90,其实,我们我们用到的数字就是26,65。在字母转数字方法思想就是将字母转成ASCLL码后,再作加减法。而将数字转字母时,也是将数字对26取余以及求商,将余数转化成字母,叠加成字符串即可。


相关阅读

  • excel培训-Excel里不得不学的批量操作

  • 乔山办公网excel表格制作
  • Excel中的批量操作绝对是每个职场人都要学会的技能。简单几下操作完成原先需要几个小时的工作量。批量数字大小写转换财务或者很多岗位的行政人员常会遇见需要将阿拉伯数字转换为
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网