主要是通过判断是否为合并单元格,和判断是否有值 , 以及 Excel 合并单元格只去最左上的一个单元格作为合并单元格的数据 来实现该获取值的方法。
代码如下:
private string GetCellText(int row,int col,Excel._Worksheet oSheet)
{
string result = "";
bool isFound = false;
int rowEnd = 1;
int colEnd = 1;
Excel.Range oRng = (Excel.Range)oSheet.Cells[row,col];
if(oRng.Value2 != null)
{
result = oRng.Value2.ToString();
isFound = true;
}
else
{
if(!(bool)oRng.MergeCells) // 如果该单元格无值且不是合并的,则返回 null
{
result = null;
isFound = true;
}
}
if(!isFound)
{
// 倒序遍历该列所有行(从倒2行开始),判断是否有合并单元格且有值,如果遇到则已求出,
// 如果遇到非合并单元格,则行+1(倒回1行),列同样倒序进行
for( int r = row-1; r >=1 ;r--)
{
oRng = (Excel.Range)oSheet.Cells[r,col];
if((bool)oRng.MergeCells)
{
try
{
if(oRng.Value2 != null)
{
result = oRng.Value2.ToString();
isFound = true;
break;
}
}
catch(Exception)
{}
}
else
{
rowEnd = r+1;
break;
}
}
if(!isFound)
{
// 倒序遍历该行所有列,判断是否有合并单元格且有值,如果遇到则已求出,如果遇到非合并单元格,则说明数据非法。。。
for( int c = col-1; c >=1 ;c--)
{
oRng = (Excel.Range)oSheet.Cells[rowEnd,c];
if((bool)oRng.MergeCells)
{
try
{
if(oRng.Value2 != null)
{
result = oRng.Value2.ToString();
isFound = true;
break;
}
}
catch(Exception)
{}
}
else
{
colEnd = c+1;
break;
}
}
}
if(!isFound)
{
result = null;
}
}
return result;
}
{
string result = "";
bool isFound = false;
int rowEnd = 1;
int colEnd = 1;
Excel.Range oRng = (Excel.Range)oSheet.Cells[row,col];
if(oRng.Value2 != null)
{
result = oRng.Value2.ToString();
isFound = true;
}
else
{
if(!(bool)oRng.MergeCells) // 如果该单元格无值且不是合并的,则返回 null
{
result = null;
isFound = true;
}
}
if(!isFound)
{
// 倒序遍历该列所有行(从倒2行开始),判断是否有合并单元格且有值,如果遇到则已求出,
// 如果遇到非合并单元格,则行+1(倒回1行),列同样倒序进行
for( int r = row-1; r >=1 ;r--)
{
oRng = (Excel.Range)oSheet.Cells[r,col];
if((bool)oRng.MergeCells)
{
try
{
if(oRng.Value2 != null)
{
result = oRng.Value2.ToString();
isFound = true;
break;
}
}
catch(Exception)
{}
}
else
{
rowEnd = r+1;
break;
}
}
if(!isFound)
{
// 倒序遍历该行所有列,判断是否有合并单元格且有值,如果遇到则已求出,如果遇到非合并单元格,则说明数据非法。。。
for( int c = col-1; c >=1 ;c--)
{
oRng = (Excel.Range)oSheet.Cells[rowEnd,c];
if((bool)oRng.MergeCells)
{
try
{
if(oRng.Value2 != null)
{
result = oRng.Value2.ToString();
isFound = true;
break;
}
}
catch(Exception)
{}
}
else
{
colEnd = c+1;
break;
}
}
}
if(!isFound)
{
result = null;
}
}
return result;
}
获取值测试:
private void GetAllText()
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
object missing = System.Type.Missing;
//创建Excel实例
oXL = new Excel.Application();
//打开已有的工作簿
oWB = oXL.Workbooks.Open(@"E:\BookTest.xls",missing,missing,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing,missing,missing);
oSheet = (Excel.Worksheet)oWB.Sheets.get_Item(1);
// 遍历获取 10行 10列 数据
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
string str = GetCellTest(i,j,oSheet);
richTextBox1.AppendText(" i : "+i.ToString() + " , j : "+ j.ToString() + " , value : "+ str +" \r\n ");
}
}
}
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
object missing = System.Type.Missing;
//创建Excel实例
oXL = new Excel.Application();
//打开已有的工作簿
oWB = oXL.Workbooks.Open(@"E:\BookTest.xls",missing,missing,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing,missing,missing);
oSheet = (Excel.Worksheet)oWB.Sheets.get_Item(1);
// 遍历获取 10行 10列 数据
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
string str = GetCellTest(i,j,oSheet);
richTextBox1.AppendText(" i : "+i.ToString() + " , j : "+ j.ToString() + " , value : "+ str +" \r\n ");
}
}
}