mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-30 10:57:46 +08:00
Add GetCellColumnIndex
This commit is contained in:
parent
25a4ac6026
commit
d1fd5cfbc8
@ -212,7 +212,7 @@
|
||||
|
||||
var r = cell.Attribute("r")?.Value?.ToString();
|
||||
{
|
||||
var cellIndex = XlsxUtils.GetColumnIndex(r) - 1;
|
||||
var cellIndex = XlsxUtils.GetCellColumnIndex(r) - 1;
|
||||
columnIndexMaximum = Math.Max(columnIndexMaximum, cellIndex);
|
||||
|
||||
datarow.Add(cellIndex, v);
|
||||
|
@ -4,77 +4,105 @@
|
||||
|
||||
internal static class XlsxUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Encode to XML (special characteres: ' " > < &)
|
||||
/// </summary>
|
||||
internal static string EncodeXML(object value) => value == null
|
||||
? ""
|
||||
: value.ToString().Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'");
|
||||
/// <summary>
|
||||
/// Encode to XML (special characteres: ' " > < &)
|
||||
/// </summary>
|
||||
internal static string EncodeXML(object value) => value == null
|
||||
? ""
|
||||
: value.ToString().Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'");
|
||||
|
||||
/// <summary>X=CellLetter,Y=CellNumber,ex:A1=(1,1),B2=(2,2)</summary>
|
||||
internal static string ConvertXyToCell(Tuple<int, int> xy)
|
||||
{
|
||||
return ConvertXyToCell(xy.Item1, xy.Item2);
|
||||
}
|
||||
/// <summary>X=CellLetter,Y=CellNumber,ex:A1=(1,1),B2=(2,2)</summary>
|
||||
internal static string ConvertXyToCell(Tuple<int, int> xy)
|
||||
{
|
||||
return ConvertXyToCell(xy.Item1, xy.Item2);
|
||||
}
|
||||
|
||||
/// <summary>X=CellLetter,Y=CellNumber,ex:A1=(1,1),B2=(2,2)</summary>
|
||||
internal static string ConvertXyToCell(int x, int y)
|
||||
{
|
||||
int dividend = x;
|
||||
string columnName = String.Empty;
|
||||
int modulo;
|
||||
/// <summary>X=CellLetter,Y=CellNumber,ex:A1=(1,1),B2=(2,2)</summary>
|
||||
internal static string ConvertXyToCell(int x, int y)
|
||||
{
|
||||
int dividend = x;
|
||||
string columnName = String.Empty;
|
||||
int modulo;
|
||||
|
||||
while (dividend > 0)
|
||||
{
|
||||
modulo = (dividend - 1) % 26;
|
||||
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
|
||||
dividend = (int)((dividend - modulo) / 26);
|
||||
}
|
||||
return $"{columnName}{y}";
|
||||
}
|
||||
while (dividend > 0)
|
||||
{
|
||||
modulo = (dividend - 1) % 26;
|
||||
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
|
||||
dividend = (int)((dividend - modulo) / 26);
|
||||
}
|
||||
return $"{columnName}{y}";
|
||||
}
|
||||
|
||||
/// <summary>X=CellLetter,Y=CellNumber,ex:A1=(1,1),B2=(2,2)</summary>
|
||||
internal static Tuple<int, int> ConvertCellToXY(string cell)
|
||||
{
|
||||
return Tuple.Create(GetColumnIndex(cell), GetCellNumber(cell));
|
||||
}
|
||||
/// <summary>X=CellLetter,Y=CellNumber,ex:A1=(1,1),B2=(2,2)</summary>
|
||||
internal static Tuple<int, int> ConvertCellToXY(string cell)
|
||||
{
|
||||
return Tuple.Create(GetCellColumnIndex(cell), GetCellRowNumber(cell));
|
||||
}
|
||||
|
||||
internal static int GetColumnIndex(string cell)
|
||||
{
|
||||
const string keys = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const int mode = 26;
|
||||
internal static int GetColumnNumber(string name)
|
||||
{
|
||||
int number = -1;
|
||||
int pow = 1;
|
||||
for (int i = name.Length - 1; i >= 0; i--)
|
||||
{
|
||||
number += (name[i] - 'A' + 1) * pow;
|
||||
pow *= 26;
|
||||
}
|
||||
|
||||
var x = 0;
|
||||
var cellLetter = GetCellLetter(cell);
|
||||
//AA=27,ZZ=702
|
||||
for (int i = 0; i < cellLetter.Length; i++)
|
||||
x = x * mode + keys.IndexOf(cellLetter[i]);
|
||||
return number;
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
internal static int GetCellColumnIndex(string cell)
|
||||
{
|
||||
const string keys = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const int mode = 26;
|
||||
|
||||
internal static int GetCellNumber(string cell)
|
||||
{
|
||||
if (string.IsNullOrEmpty(cell))
|
||||
throw new Exception("cell is null or empty");
|
||||
string cellNumber = string.Empty;
|
||||
for (int i = 0; i < cell.Length; i++)
|
||||
{
|
||||
if (Char.IsDigit(cell[i]))
|
||||
var x = 0;
|
||||
var cellLetter = GetCellColumnLetter(cell);
|
||||
//AA=27,ZZ=702
|
||||
for (int i = 0; i < cellLetter.Length; i++)
|
||||
x = x * mode + keys.IndexOf(cellLetter[i]);
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
internal static int GetCellRowNumber(string cell)
|
||||
{
|
||||
if (string.IsNullOrEmpty(cell))
|
||||
throw new Exception("cell is null or empty");
|
||||
string cellNumber = string.Empty;
|
||||
for (int i = 0; i < cell.Length; i++)
|
||||
{
|
||||
if (Char.IsDigit(cell[i]))
|
||||
cellNumber += cell[i];
|
||||
}
|
||||
return int.Parse(cellNumber);
|
||||
}
|
||||
}
|
||||
return int.Parse(cellNumber);
|
||||
}
|
||||
|
||||
internal static string GetCellLetter(string cell)
|
||||
{
|
||||
string GetCellLetter = string.Empty;
|
||||
for (int i = 0; i < cell.Length; i++)
|
||||
{
|
||||
if (Char.IsLetter(cell[i]))
|
||||
GetCellLetter += cell[i];
|
||||
}
|
||||
return GetCellLetter;
|
||||
}
|
||||
internal static string GetCellColumnLetter(string cell)
|
||||
{
|
||||
string GetCellLetter = string.Empty;
|
||||
for (int i = 0; i < cell.Length; i++)
|
||||
{
|
||||
if (Char.IsLetter(cell[i]))
|
||||
GetCellLetter += cell[i];
|
||||
}
|
||||
return GetCellLetter;
|
||||
}
|
||||
|
||||
internal static string ConvertColumnName(int x)
|
||||
{
|
||||
int dividend = x;
|
||||
string columnName = String.Empty;
|
||||
int modulo;
|
||||
|
||||
while (dividend > 0)
|
||||
{
|
||||
modulo = (dividend - 1) % 26;
|
||||
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
|
||||
dividend = (int)((dividend - modulo) / 26);
|
||||
}
|
||||
return columnName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user