for Push.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KssSmaPlaLib.IO.Excel
|
||||
{
|
||||
public static class ExcelCellValidatorUxer
|
||||
{
|
||||
/// <summary>
|
||||
/// ExcelのA1形式(例: "A1", "Z99", "AA1000")かどうかを判定します。
|
||||
/// </summary>
|
||||
/// <param name="cellRef">セル参照文字列</param>
|
||||
/// <returns>有効なA1形式ならtrue、無効ならfalse</returns>
|
||||
public static bool IsValidA1Format(string cellRef)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(cellRef))
|
||||
return false;
|
||||
|
||||
// 正規表現でA1形式をチェック(列: A〜XFD、行: 1〜1048576)
|
||||
var match = System.Text.RegularExpressions.Regex.Match(
|
||||
cellRef.ToUpperInvariant(),
|
||||
@"^([A-Z]{1,3})([1-9][0-9]{0,6})$"
|
||||
);
|
||||
|
||||
if (!match.Success)
|
||||
return false;
|
||||
|
||||
string colPart = match.Groups[1].Value;
|
||||
string rowPart = match.Groups[2].Value;
|
||||
|
||||
// 列がXFD(Excel最大列)以内かチェック
|
||||
if (ColumnNameToNumber(colPart) > 16384)
|
||||
return false;
|
||||
|
||||
// 行がExcel最大行以内かチェック
|
||||
if (int.TryParse(rowPart, out int row))
|
||||
{
|
||||
return row >= 1 && row <= 1048576;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列名(例: "A", "Z", "AA")を列番号に変換(例: 1, 26, 27)
|
||||
/// </summary>
|
||||
public static int ColumnNameToNumber(string columnName)
|
||||
{
|
||||
int sum = 0;
|
||||
foreach (char c in columnName)
|
||||
{
|
||||
sum *= 26;
|
||||
sum += (c - 'A' + 1);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KssSmaPlaLib.IO.Excel
|
||||
{
|
||||
public static class ExcelColumnHelperUxer
|
||||
{
|
||||
public static string GetExcelColumnLetter(int columnNumber)
|
||||
{
|
||||
if (columnNumber < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(columnNumber), "Column number must be >= 1");
|
||||
|
||||
string columnLetter = string.Empty;
|
||||
while (columnNumber > 0)
|
||||
{
|
||||
columnNumber--; // Adjust for 0-indexed calculation
|
||||
columnLetter = (char)('A' + (columnNumber % 26)) + columnLetter;
|
||||
columnNumber /= 26;
|
||||
}
|
||||
return columnLetter;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using ClosedXML.Excel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KssSmaPlaLib.IO.Excel
|
||||
{
|
||||
public class XlUxer
|
||||
{
|
||||
public static IXLCell GCell(IXLWorksheet sheet, string a1Address)
|
||||
{
|
||||
var cell = sheet.Cell(a1Address);
|
||||
return GCell(cell);
|
||||
}
|
||||
|
||||
public static IXLCell GCell(IXLWorksheet sheet, int row, int col)
|
||||
{
|
||||
var cell = sheet.Cell(row, col);
|
||||
return GCell(cell);
|
||||
}
|
||||
|
||||
public static IXLCell GCell(IXLCell cell)
|
||||
{
|
||||
return cell.IsMerged() ? cell.MergedRange().FirstCell() : cell;
|
||||
}
|
||||
|
||||
public static bool TryValidateA1CellAddress(string a1Address, out string normalizedAddress)
|
||||
{
|
||||
normalizedAddress = null;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(a1Address))
|
||||
return false;
|
||||
|
||||
// 正規表現でA1形式(例:A1, B2, AA10など)をチェック
|
||||
var regex = new Regex(@"^[A-Z]{1,3}[1-9][0-9]*$", RegexOptions.IgnoreCase);
|
||||
if (!regex.IsMatch(a1Address))
|
||||
return false;
|
||||
|
||||
// 正規化(大文字化)
|
||||
normalizedAddress = a1Address.ToUpperInvariant();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shorter
|
||||
/// </summary>
|
||||
/// <param name="colLetter"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetColNo(string colLetter)
|
||||
{
|
||||
if (!TryConvertColLetterToNumber(colLetter, out var no))
|
||||
{
|
||||
return int.MinValue;
|
||||
}
|
||||
return no;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列アルファベットを列番号(1オリジン)に変換
|
||||
/// </summary>
|
||||
/// <param name="colLetter">列アルファベット</param>
|
||||
/// <param name="colNo">[出力]列番号(1オリジン)</param>
|
||||
/// <returns>成否</returns>
|
||||
public static bool TryConvertColLetterToNumber(string colLetter, out int colNo)
|
||||
{
|
||||
colNo = int.MinValue;
|
||||
|
||||
// バリデ(A~XFD)
|
||||
if(!Regex.IsMatch(colLetter, @"^(?i)([A-Z]|[A-Z]{2}|[A-E][A-Z]{2})$"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 大文字化
|
||||
colLetter = colLetter.ToUpper();
|
||||
|
||||
// アルファベット1文字ずつ見て列番号を算出
|
||||
int no = 0;
|
||||
foreach (char c in colLetter)
|
||||
{
|
||||
no = no * 26 + (c - 'A' + 1);
|
||||
}
|
||||
|
||||
// 列番号範囲外なら失敗
|
||||
if (no > 16384 || no < 1) return false;
|
||||
|
||||
// 成功
|
||||
colNo = no;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user