範囲が常に 10x10 であることが確実にわかっていない限り、その場合はループ条件を変更するだけの問題であり、連続する値を検出する方法が必要になります。
このコードスニペットは、そうするのに役立ちます
/// <summary>
/// gets the number of consecutive (without any empty cell) values vertically
/// </summary>
/// <param name="aRow"></param>
/// <param name="aColumn"></param>
/// <returns></returns>
public int GetNumberConsecValues(int aRow, int aColumn)
{
int wCount = 0;
while (this[aRow + wCount, aColumn] != null)
{
wCount++;
}
return wCount;
}
必要に応じて値を配列/行列に読み込むために使用します。
/// <summary>
/// Reads the vertical array from the specified cell
/// </summary>
/// <param name="aRow"></param>
/// <param name="aColumn"></param>
/// <returns></returns>
public int[] ReadArrayInt(int aRow, int aColumn)
{
int wNbValues = this.GetNumberConsecValues(aRow, aColumn);
if (wNbValues == 0)
return null;
int[] wArr = new int[wNbValues];
for (int iRow = 0; iRow < wNbValues; iRow++)
{
wArr[iRow] = Convert.ToInt32(this[aRow + iRow, aColumn]);
}
return wArr;
}
範囲がわかっている場合は、この他のコード スニペットを使用できます
public object[,] ReadRange(int aColFrom, int aColTo, int aRowFrom, int aRowTo)
{
this.BeginUpdate();
try
{
SpreadsheetGear.IRange oRange = m_ViewLock.Workbook.ActiveWorksheet.Cells[aRowFrom, aColFrom, aRowTo, aColTo];
object[,] oValues = new object[aRowTo - aRowFrom + 1, aColTo - aColFrom + 1];
int iRCol = 0;
for (int iCol = aColFrom; iCol <= aColTo; iCol++)
{
int iRRow = 0;
for (int iRow = aRowFrom; iRow <= aRowTo; iRow++)
{
oValues[iRRow, iRCol] = oRange.Cells[iRRow, iRCol].Value;
iRRow++;
}
iRCol++;
}
return oValues;
}
finally
{
this.EndUpdate();
}
}
BeginUpdate と EndUpdate は次のように定義されます。
/// <summary>
/// Must be called before the grid is being modified
/// We also call it inside every method so that if the user forgets to call it is no big deal
/// For this reason the calls can be nested.
/// So if the user forgets to make the call it is safe
/// and if the user does not forget he/she gets the full speed benefits..
/// </summary>
public void BeginUpdate()
{
if (m_ViewLockCount == 0)
m_ViewLock = new CWorkbookViewLockHelper(this.Workbook);
m_ViewLockCount++;
}
/// <summary>
/// Must be called after the grid has being modified
/// </summary>
public void EndUpdate()
{
m_ViewLockCount--;
if (m_ViewLockCount <= 0)
{
m_ViewLock.Dispose();
m_ViewLock = null;
}
}