private string GetCellText(int row, int col, ExcelWorksheet oSheet)
{
string result = "";
bool isFound = false;
int rowEnd = 1;
int colEnd = 1;
ExcelRange oRng = (ExcelRange)oSheet.Cells[row, col];
if (oRng.Value != null)
{
result = oRng.Value.ToString();
isFound = true;
}
else
{
if (!(bool)oRng.Merge) // 如果该单元格无值且不是合并的,则返回 null
{
result = null;
isFound = true;
}
}
if (!isFound)
{
// 倒序遍历该列所有行(从倒2行开始),判断是否有合并单元格且有值,如果遇到则已求出,
// 如果遇到非合并单元格,则行+1(倒回1行),列同样倒序进行
for (int r = row - 1; r >= 1; r--)
{
oRng = (ExcelRange)oSheet.Cells[r, col];
if ((bool)oRng.Merge)
{
if (oRng.Value != null)
{
result = oRng.Value.ToString();
isFound = true;
break;
}
}
else
{
rowEnd = r + 1;
break;
}
}
if (!isFound)
{
// 倒序遍历该行所有列,判断是否有合并单元格且有值,如果遇到则已求出,如果遇到非合并单元格,则说明数据非法。。。
for (int c = col - 1; c >= 1; c--)
{
oRng = (ExcelRange)oSheet.Cells[rowEnd, c];
if ((bool)oRng.Merge)
{
if (oRng.Value != null)
{
result = oRng.Value.ToString();
isFound = true;
break;
}
}
else
{
colEnd = c + 1;
break;
}
}
}
if (!isFound)
{
result = null;
}
}
return result;
}