本文可以当作是Excel列名与对应的索引(索引从0开始)转化工具。Excel列比如A,列的索引为0,AA列的索引是26.
Excel的行数和列数是有极限的。
Excel表格不同版本最大行数和列数都会不一样。 Excel 2003版:列数最大256(IV,2的8次方)列,行数最大65536(2的16次方)行;
Excel 2007版:列数最大16384(XFD,2的14次方),行数最大1048576(2的20次方);
Excel 2013版:列数最大16384(XFD,2的14次方),行数最大1048576(2的20次方);
Excel行和列的表示方法:例如2003版行用数字1-65536表示;列用英文字母A-Z,AA-AZ,BA-BZ,...,IA-IV表示,共256列。 获取Excel最大行和最大列的方法: 1、通过快捷键Ctrl+↓(向下键)查看最大行号。
2、通过快捷键Ctrl+→ (向右键)查看最大列号。
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks;
namespace ExcelColumnIndexDemo {
/// <summary> /// 查询本机Excel版本以及相关信息 /// </summary> public class ExcelVersionUtil { /// <summary> /// 判断本机是否安装了Excel /// </summary> /// <returns></returns> public static bool IsExcelInstalled() { Type type = Type.GetTypeFromProgID("Excel.Application"); return type != null; }
/// <summary> /// 获取本机Excel版本号 /// </summary> /// <returns></returns> public static double GetExcelVersion() { //如果未安装Excel,则直接返回0 if (!IsExcelInstalled()) { return 0; } Type type = Type.GetTypeFromProgID("Excel.Application"); object objExcel = Activator.CreateInstance(type); if (objExcel == null) { return 0; } //返回版本信息 System.String object versionReturn = type.InvokeMember("Version", BindingFlags.GetProperty, null, objExcel, null); //Console.WriteLine(versionReturn.GetType()); double version = Convert.ToDouble(versionReturn); return version; }
/// <summary> /// 获取Excel版本描述 /// </summary> /// <returns></returns> public static string GetExcelVersionString() { double version = GetExcelVersion(); if (version == 0) { return "没有安装Excel 或者 无法识别的Excel版本"; } if (version >= 14) { return "Excel 2010或以上"; } else if (version >= 12) { return "Excel 2007"; } else if (version >= 11) { return "Excel 2003"; } else if (version >= 10) { return "Excel XP"; } else if (version >= 9) { return "Excel 2000"; } else if (version >= 8) { return "Excel 97"; } else if (version >= 7) { return "Excel 95"; } else { return "未知的Excel版本:" + version; } } } }
源程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks;
namespace ExcelColumnIndexDemo { /// <summary> /// Excel列的索引与列名之间的转化 /// </summary> public class ExcelColumnConverter { /// <summary> /// 列名转化为列的索引【索引从0开始】 /// </summary> /// <param name="columnName">Excel的列名,如 AA</param> /// <returns>返回列的索引 如 26。返回-1说明是非法的列名</returns> public static int ColumnNameToInt(string columnName) { Regex regex = new Regex("^[A-Z]{1,3}$"); if (!regex.IsMatch(columnName)) { return -1; } int sum = 0; for (int i = columnName.Length - 1; i >= 0; i--) { char c = columnName[i]; //A代表1,B代表2,...Z代表26 int mappingValue = c - 64; sum += mappingValue * (int)Math.Pow(26, columnName.Length - 1 - i); } //索引需要减去1 sum = sum - 1; return sum; }
/// <summary> /// 索引转列名【索引从0开始】 /// </summary> /// <param name="columnIndex">列的索引,如 27</param> /// <returns>返回列名 如 AB</returns> public static string ColumnIndexToName(int columnIndex) { if (columnIndex < 0 || columnIndex >= 16384) { //txtColumnIndex2.Focus(); //MessageBox.Show("Excel的列索引只能输入数字【0-16383】之间", "输入错误"); return string.Empty; } columnIndex = columnIndex + 1; //除基(26)取余,按照逆序拼接 Stack<char> stack = new Stack<char>(); while (columnIndex != 0) { if (columnIndex % 26 == 0) { stack.Push('Z'); columnIndex = columnIndex / 26 - 1; } else { stack.Push((char)(columnIndex % 26 + 64)); columnIndex = columnIndex / 26; } } string columnName = string.Join("", stack); return columnName; } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions;
namespace ExcelColumnIndexDemo { public partial class FormExcelColumnIndex : Form { public FormExcelColumnIndex() { InitializeComponent();
rtxtInfo.ForeColor = Color.Red; rtxtInfo.Text = @"Excel的行数和列数是有极限的。 Excel表格不同版本最大行数和列数都会不一样。比如: Excel 2003版:列数最大256(IV,2的8次方)列,行数最大65536(2的16次方)行; Excel 2007版:列数最大16384(XFD,2的14次方),行数最大1048576(2的20次方); Excel 2013版:列数最大16384(XFD,2的14次方),行数最大1048576(2的20次方)。 本文演示列名与索引之间的转化(26进制,索引从0开始)..."; }
private void FormExcelColumnIndex_Load(object sender, EventArgs e) { List<string> list = new List<string>(); list.Add("Excel 2010或以上"); list.Add("Excel 2007"); list.Add("Excel 2003"); list.Add("Excel XP"); list.Add("Excel 2000"); list.Add("Excel 97"); list.Add("Excel 95"); list.Add("没有安装Excel 或者 无法识别的Excel版本"); BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = list; cboExcelVersion.DataSource = bindingSource; string excelVersion = ExcelVersionUtil.GetExcelVersionString(); for (int i = 0; i < cboExcelVersion.Items.Count; i++) { if (cboExcelVersion.Items[i].ToString() == excelVersion) { cboExcelVersion.SelectedIndex = i; break; } } }
private void btnConvert_Click(object sender, EventArgs e) { txtColumnIndex.Clear(); string columnName = txtColumnName.Text.Trim(); int columnIndex = ExcelColumnConverter.ColumnNameToInt(columnName); if (columnIndex == -1) { txtColumnName.Focus(); MessageBox.Show("Excel的列名只能输入字母【A-Z】1到3个", "输入错误"); return; } txtColumnIndex.Text = columnIndex.ToString(); if (rtxtInfo.TextLength >= 102400) { rtxtInfo.Clear(); } rtxtInfo.AppendText($"\n列名:【{columnName}】,转化为索引:【{columnIndex}】,当前Excel表格的第【{columnIndex + 1}】列"); }
private void btnConvert2_Click(object sender, EventArgs e) { txtColumnName2.Clear(); int columnIndex; if (!int.TryParse(txtColumnIndex2.Text, out columnIndex)) { txtColumnIndex2.Focus(); MessageBox.Show("请输入数字", "输入错误"); return; } string columnName = ExcelColumnConverter.ColumnIndexToName(columnIndex); if (string.IsNullOrEmpty(columnName)) { txtColumnIndex2.Focus(); MessageBox.Show("Excel的列索引只能输入数字【0-16383】之间", "输入错误"); return; } txtColumnName2.Text = columnName; if (rtxtInfo.TextLength >= 102400) { rtxtInfo.Clear(); } rtxtInfo.AppendText($"\n索引:【{columnIndex}】,转化为列名:【{columnName}】,当前Excel表格的第【{columnIndex + 1}】列"); } } }