user5843610
user5843610

Reputation:

Epplus get the column header

I would like to understand how to get the column letter using Epplus. I know that Address will return the column letter and row number and FullAddress will add the sheet name but I don't see an object for just the column letter.

?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

Upvotes: 8

Views: 9245

Answers (2)

pfx
pfx

Reputation: 23244

EPPlus contains a ExcelCellAddress class which has the static method GetColumnLetter to retrieve the letter corresponding to the supplied 1-based column index.

public static string GetColumnLetter(int column)

The following call will return column letter A.

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

Upvotes: 22

DanB
DanB

Reputation: 2124

You already know index3. You have the column letter with this.

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;
    }
}

Simply call index3.ToExcelColumn();

Upvotes: -2

Related Questions