【问题标题】:Epplus get the column headerepplus 获取列标题
【发布时间】:2019-02-19 14:10:08
【问题描述】:

我想了解如何使用 Epplus 获取列字母。我知道 Address 将返回列字母和行号,FullAddress 将添加工作表名称,但我没有看到仅列字母的对象。

?sheet.Cells[index2, index3].Address
"J2"

?sheet.Cells[index2, index3].FormulaR1C1
""

?sheet.Cells[index2, index3].FullAddress
"'Sheet1'!J2"

?sheet.Cells[index2, index3].FullAddressAbsolute
"'Sheet1'!$J$2"

?sheet.Cells[index2, index3].Rows

【问题讨论】:

    标签: c# excel datatable epplus


    【解决方案1】:

    EPPlus 包含一个 ExcelCellAddress 类,该类具有静态方法 GetColumnLetter 来检索对应于提供的基于 1 的列索引的字母。

    public static string GetColumnLetter(int column)
    

    以下调用将返回列字母A

    String columnLetter = OfficeOpenXml.ExcelCellAddress.GetColumnLetter(1); // A
    

    【讨论】:

    • 这应该是公认的答案。这对我有帮助。谢谢!
    【解决方案2】:

    您已经知道 index3。你有这个列字母。

    public static class IntExtension
    {
        public static string ToExcelColumn(this int i)
        {
            string column = string.Empty;
    
            if (i / 26m > 1)
            {
                int letter = (int)i / 26;
                column = ((char)(65 + letter - 1)).ToString();
                i -= letter * 26;
            }
    
            column += ((char)(65 + i - 1)).ToString();
    
            return column;
        }
    }
    

    只需拨打index3.ToExcelColumn();

    【讨论】:

    • 这仅限于AZZ。递归地执行此操作会更有意义。 XFD 是 Excel 中最后(通常)可寻址的列。
    猜你喜欢
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多