如何将列号(例如 127)转换为 Excel 列(例如 AA) [英] How to convert a column number (e.g. 127) into an Excel column (e.g. AA)

查看:44
本文介绍了如何将列号(例如 127)转换为 Excel 列(例如 AA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C# 中将数字转换为 Excel 列名,而不使用直接从 Excel 中获取值的自动化.

How do you convert a numerical number to an Excel column name in C# without using automation getting the value directly from Excel.

Excel 2007 的可能范围是 1 到 16384,这是它支持的列数.结果值应采用 excel 列名的形式,例如A、AA、AAA 等

Excel 2007 has a possible range of 1 to 16384, which is the number of columns that it supports. The resulting values should be in the form of excel column names, e.g. A, AA, AAA etc.

推荐答案

我是这样做的:

private string GetExcelColumnName(int columnNumber)
{
    string columnName = "";

    while (columnNumber > 0)
    {
        int modulo = (columnNumber - 1) % 26;
        columnName = Convert.ToChar('A' + modulo) + columnName;
        columnNumber = (columnNumber - modulo) / 26;
    } 

    return columnName;
}

这篇关于如何将列号(例如 127)转换为 Excel 列(例如 AA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆