Fastest function to generate Excel column letters in C#

前端 未结 21 1804
无人共我
无人共我 2020-11-29 02:30

What is the fastest c# function that takes and int and returns a string containing a letter or letters for use in an Excel function? For example, 1 returns \"A\", 26 return

21条回答
  •  广开言路
    2020-11-29 02:50

    Don't convert it at all. Excel can work in R1C1 notation just as well as in A1 notation.

    So (apologies for using VBA rather than C#):

    Application.Worksheets("Sheet1").Range("B1").Font.Bold = True
    

    can just as easily be written as:

    Application.Worksheets("Sheet1").Cells(1, 2).Font.Bold = True
    

    The Range property takes A1 notation whereas the Cells property takes (row number, column number).

    To select multiple cells: Range(Cells(1, 1), Cells(4, 6)) (NB would need some kind of object qualifier if not using the active worksheet) rather than Range("A1:F4")

    The Columns property can take either a letter (e.g. F) or a number (e.g. 6)

提交回复
热议问题