[英]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.Cells []的值,但除“ B3”种类的值外没有其他值
Worksheet.Range
does: Worksheet.Range
执行以下操作:
Worksheet.Range("A4").Value = "Foo";
I would also advise you not to set one cell at a time if you can help it. 如果可以的话,我也建议您不要一次设置一个单元。 Every call to
Cells
or Range
in a slow interop call, so you'll get much better performance if you put your values in an array and set the value of an entire range in one shot: 在慢速互操作中,每次调用
Cells
或Range
,如果将值放在数组中并一次性设置整个范围的值,您将获得更好的性能:
int[,] values;
// fill values with integers
Worksheet.Range("A1","D4") = values;
Well, even if the question wasn't clear, here is an answer for: 好吧,即使这个问题不清楚,这里也有一个答案:
How can I convert an excel cells address in the following notation: "LetterNumber" to "Number, Number" and vice-versa?
如何将以下表示形式的Excel单元格地址转换为“ LetterNumber”到“ Number,Number”,反之亦然?
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;
}
The results are: 结果是:
"1,1" -> "A1"
“ 1,1”->“ A1”
"A1" -> "1,1"“ A1”->“ 1,1”
"42,231" -> "AP231"“ 42,231”->“ AP231”
"AP231" -> "42,231"“ AP231”->“ 42,231”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.