C# 如何将 excel 单元格地址转换为以下表示法:"LetterNumber" 到 "Number, Number",反之亦然?
C# How can I convert an excel cells address in the following notation: "LetterNumber" to "Number, Number" and vice-versa?
如何将 excel 单元格地址转换为以下表示法:"LetterNumber" 到 "Number, Number",反之亦然?
I'm trying to set the value of Worksheet.Cells[] but it does not except the "B3" kinds of values
Worksheet.Range("A4").Value = "Foo";
如果可以的话,我还建议您不要一次设置一个单元格。在缓慢的互操作调用中每次调用 Cells
或 Range
,因此如果将值放在数组中并一次设置整个范围的值,您将获得更好的性能:
int[,] values;
// fill values with integers
Worksheet.Range("A1","D4") = values;
好吧,即使问题不清楚,这里也有一个答案:
How can I convert an excel cells address in the following notation: "LetterNumber" to "Number, Number" and vice-versa?
private const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static string ToExcelCoordinates(string coordinates)
{
string first = coordinates.Substring(0, coordinates.IndexOf(','));
int i = int.Parse(first);
string second = coordinates.Substring(first.Length + 1);
string str = string.Empty;
while (i > 0)
{
str = ALPHABET[(i - 1) % 26] + str;
i /= 26;
}
return str + second;
}
public static string ToNumericCoordinates(string coordinates)
{
string first = string.Empty;
string second = string.Empty;
CharEnumerator ce = coordinates.GetEnumerator();
while (ce.MoveNext())
if (char.IsLetter(ce.Current))
first += ce.Current;
else
second += ce.Current;
int i = 0;
ce = first.GetEnumerator();
while (ce.MoveNext())
i = (26 * i) + ALPHABET.IndexOf(ce.Current) + 1;
string str = i.ToString();
return str + "," + second;
}
结果为:
"1,1" -> "A1"
"A1" -> "1,1"
"42,231" -> "AP231"
"AP231" -> "42,231"
如何将 excel 单元格地址转换为以下表示法:"LetterNumber" 到 "Number, Number",反之亦然?
I'm trying to set the value of Worksheet.Cells[] but it does not except the "B3" kinds of values
Worksheet.Range("A4").Value = "Foo";
如果可以的话,我还建议您不要一次设置一个单元格。在缓慢的互操作调用中每次调用 Cells
或 Range
,因此如果将值放在数组中并一次设置整个范围的值,您将获得更好的性能:
int[,] values;
// fill values with integers
Worksheet.Range("A1","D4") = values;
好吧,即使问题不清楚,这里也有一个答案:
How can I convert an excel cells address in the following notation: "LetterNumber" to "Number, Number" and vice-versa?
private const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static string ToExcelCoordinates(string coordinates)
{
string first = coordinates.Substring(0, coordinates.IndexOf(','));
int i = int.Parse(first);
string second = coordinates.Substring(first.Length + 1);
string str = string.Empty;
while (i > 0)
{
str = ALPHABET[(i - 1) % 26] + str;
i /= 26;
}
return str + second;
}
public static string ToNumericCoordinates(string coordinates)
{
string first = string.Empty;
string second = string.Empty;
CharEnumerator ce = coordinates.GetEnumerator();
while (ce.MoveNext())
if (char.IsLetter(ce.Current))
first += ce.Current;
else
second += ce.Current;
int i = 0;
ce = first.GetEnumerator();
while (ce.MoveNext())
i = (26 * i) + ALPHABET.IndexOf(ce.Current) + 1;
string str = i.ToString();
return str + "," + second;
}
结果为:
"1,1" -> "A1"
"A1" -> "1,1"
"42,231" -> "AP231"
"AP231" -> "42,231"