如何实现从索引列的自我命名? [英] How to implement column self-naming from its index?

查看:113
本文介绍了如何实现从索引列的自我命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够给我的实例细胞类,而命名这样的名称为A,B的细胞的实例,C,只等。像在Excel电子表格

I wish to be able to instantiate my Cell class while naming the cell instance with such name as "A", "B", "C", etc. just like in an Excel spreadsheet.

我有我的细胞类,像这样:

public class Cell {
    public Cell(Range nativeCell) {
        NativeCell = nativeCell;
    }

    public Range NativeCell { get; private set; }
}

和我的类:

public class Sheet {
    private IDictionary<string, Cell> _cells;

    public Sheet(Worksheet nativeSheet) {
        NativeSheet = nativeSheet;

        _cells = new Dictionary<string, Cell>();

        for (int rowIndex = 1; rowIndex <= NativeSheet.Rows.Count; ++rowIndex)
            for (int colIndex = 1; colIndex <= NativeSheet.Columns.Count; ++colIndex) {
                ICell newCell = new Cell(NativeSheet.Cells(rowIndex, colIndex));
                newCell.Name = ?? // This name should look like "A1", "B1", "AA3", "CB20", etc.
                Cells.Add(newCell.Name, newCell);
            }
    }

    public IDictionary<string, Cell> Cells {
        get {
            return _cells;
        }
    }

    public Worksheet NativeSheet { get; private set; }
}



我需要生成基于所述字母和双重的名称和三倍他们一次我遇到的最后一个字母开头'Z'。该算法将不得不生成,我会用这会导致这种命名策略,如Excel中的的rowIndex 值连接的字母。

的字母是:

A, B, C, D...Z, AA, AB, AC...AZ, BA, BB, BC...BZ, CA...XAA, XAB, XAC...

虽然我们清楚地知道, colIndex 值1肯定会指定列A,值2 =b,价值3 =C,等等。

While we clearly know that colIndex value 1 will definitely designate column "A", value 2 = "B", value 3 = "C", etc.

我的问题是,特别是当我们双倍的字母。

My problem is particularly when we double the letters.

你对我怎么什么想法能以最简单的形式实现这一目标?

Do you have any idea on how I could achieve this in the simplest possible form?

谢谢! =)

推荐答案

下面是这样的。
翻译索引到一个Excel列名

不应该是硬让它递归,给你正是你需要的。我希望这有助于。

Shouldn't be to hard to make it recursive and give you exactly what you need. I hope this helps.

这篇关于如何实现从索引列的自我命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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