简体   繁体   English

在 C# 中,我如何构建从 A 到 ZZ 的数组,类似于 excel 对列进行排序的方式

[英]in c#, how can i build up array from A to ZZ that is similar to the way that excel orders columns

i am looking for code that can generate an array where the first item is A , then B , then C .我正在寻找可以生成数组的代码,其中第一项是A ,然后是B ,然后是C . . .after Z it would then go to AA , then AB then AC . .在Z 之后,它会转到AA ,然后是AB ,然后是AC . . . . all the way up to ZZ .一直到ZZ

what is the best way of doing this in C#?在 C# 中这样做的最佳方法是什么?

One of the ways is:其中一种方法是:

IEnumerable<string> generate()
{
    for (char c = 'A'; c <= 'Z'; c++)
        yield return new string(c, 1);
    for (char c = 'A'; c <= 'Z'; c++)
        for (char d = 'A'; d <= 'Z'; d++)
            yield return new string(new[] { c, d });
}

Edit:编辑:
you can actually produce "infinite" sequence (bounded by maximal long value) with somewhat more complicated code:您实际上可以使用更复杂的代码生成“无限”序列(以最大long值为界):

string toBase26(long i)
{
    if (i == 0) return ""; i--;
    return toBase26(i / 26) + (char)('A' + i % 26);
}

IEnumerable<string> generate()
{
    long n = 0;
    while (true) yield return toBase26(++n);
}

This one goes like that: A, B, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ... etc:这个是这样的:A, B, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ... 等等:

foreach (var s in generate().Take(200)) Console.WriteLine(s);

Great answer of Vlad.弗拉德的好答案。

Here's another variation on that:这是另一个变化:

 static IEnumerable<string> generate() {
   for (char c = 'A'; c <= 'Z'; c++) {
     yield return c.ToString();
   }

   foreach (string s in generate()) {
     for (char c = 'A'; c <= 'Z'; c++) {
       yield return s + c;
     }
   }
 }

If you don't mind starting the sequence with an empty string you could write it as follows:如果您不介意以空字符串开始序列,您可以按如下方式编写:

 static IEnumerable<string> generate() {
   yield return "";

   foreach (string s in generate()) {
     for (char c = 'A'; c <= 'Z'; c++) {
       yield return s + c;
     }
   }
 }

You could generate numbers using Enumerable.Range and cast them to char to generate AZ.您可以使用Enumerable.Range生成数字并将它们转换为char以生成 AZ。 Next step is to combine them.下一步是将它们组合起来。

Found this in SO albeit in PHP.尽管在 PHP 中,但在 SO 中找到了这个。 Would this help?这会有帮助吗? Algorithm to get the excel-like column name of a number 获取数字的类似excel的列名的算法

var q = Enumerable.Range(Convert.ToInt32('A'),26).Select( x => Convert.ToChar(x) );
var result = (
  q.Select( x => x.ToString() )
   .Concat( 
      q.SelectMany(
        x => q.Select( y => x.ToString() + y.ToString() )
      )
   )
);
class Program
{
    public static string IntegerToExcelColumn(int col)
    {
        // I've put a 256 upper bound here because Excel 2003 
        // allows only 256 columns. Change it if you're using 
        // Excel 2007 or 2010.
        Debug.Assert(col >= 1 && col <= 256);

        if (col >= 1 && col <= 26)
        {
            return ((char)(((int)'A') + (col - 1))).ToString();
        }

        // I've put a 256 upper bound here because Excel 2003 
        // allows only 256 columns. Change it if you're using 
        // Excel 2007 or 2010.
        if (col > 26 && col <= 256)
        {
            int rem = col % 26;
            int pri = col / 26;
            if (rem == 0)
            {
                rem = 26;
                pri--;
            }
            char[] buffer = new char[2];
            buffer[0] = (char)(((int)'A') + (pri - 1));
            buffer[1] = (char)(((int)'A') + (rem - 1));
            return new string(buffer);
        }

        return "";
    }

    static void Main(string[] args)
    {
        string[] columns= new string[255];
        for (int i = 1; i <= 255; i++)
            columns[i-1] = IntegerToExcelColumn(i);
        foreach(var col in columns)
            Console.WriteLine(col);
    }
}

Here's one way.这是一种方法。 :) :)

string[] values =
  Enumerable.Range(0, 27 * 26)
  .Select(
    n => new String(
      new[] { (char)('@' + n / 26), (char)('A' + n % 26) },
      n < 26 ? 1 : 0, n < 26 ? 1 : 2
    )
  )
  .ToArray();

Have a look at an ASCII table and take note of the values of the characters.查看一个ASCII 表并记下字符的值。 You should be able to work out a loop to increment the character from A to Z for as many times as you need to replicate the characters.您应该能够计算出一个循环,将字符从 A 增加到 Z 的次数与复制字符的次数相同。 :) :)

You can also try this.你也可以试试这个。 A to ZZ A 到 ZZ

public class Program
{
    public static void Main()
    {
        int c=1;
        string cc=AtoZZ(c);
        for(int i=1;i<=702;i++){
        cc=AtoZZ(i);
        Console.WriteLine(cc);
        }
    }

    static string AtoZZ(int chr)
    {
        char c1;
        char c2;
        string alp = "";
        if(chr<=26)
        {
            c1 = (char)(chr + 64);
            alp = c1 + "";
        }        
        else
        {
            int cl1 = chr / 26;
            int cl2 = chr % 26;
            if(cl2==0){
                cl1--;
                cl2=26;
            }
            c1 = (char)(cl1 + 64);
            c2 = (char)(cl2 + 64);
            alp = c1 + "" + c2;
        }
        return alp;
     }
}

To complement Vlad's answer, here's the reverse operation of ToBase26(int):为了补充 Vlad 的回答,这里是 ToBase26(int) 的反向操作:

static long ToBase10(string str)
{
   if (string.IsNullOrWhiteSpace(str)) return 0;
   var value = str[0] - 'A' + 1;

   return (long) (value * Math.Pow(26, str.Length - 1) + 
          ToBase10(str.Substring(1, str.Length - 1)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在 Angular 和 C# 中生成从“A”到“ZZ”的数组。 怎么可能? - I want to Generate Array From 'A' to 'ZZ' In Angular & C#. How it can be possible? 如何从 C# 加速创建 excel Excel 电子表格? - How can I speed-up creating excel Excel spreadsheets from C#? 如何从c#中的excel表中选择特定列? - How can i select specific columns from excel sheet in c#? 如何在查询C#的Excel工作表中选择一组行和列? - How can I select a set of rows and columns from an excel sheet in query for C#? 如何从用 ASP 3.0 编程的网站向 C# windows 应用程序下订单 - How can I give orders to a C# windows application from a website programmed in ASP 3.0 如何加速C#中的数组克隆? - How can I speed up array cloning in C#? 如何从 C# 中的二维数组中删除行和列? - How can I delete rows and columns from 2D array in C#? 我可以在C#数组列表中存储类似的对象 - can I store similar objects in a C# array list 如何将Excel列A,B和C读入C#数据结构(向量,数组,列表?) - How do I read Excel columns A, B, and C into a C# data structure (vectors, array, list?) c# datetimepickers 如何在一天内从 8:00 到 22:00 的工作时间内搜索所有订单? - How can I search all orders in working hours in one day from 8:00 till 22:00 c# datetimepickers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM