C#检查是否存在单元格边框

时间:2014-04-15 17:29:02

标签: c# excel range border

我目前正在使用Microsoft.Office.Interop.Excel在C#上做一个小项目,而且我有点卡住了。

我尝试做的是创建一个方法,该方法接受一个Range,然后根据该范围的下边缘是否有边框返回true或falsed。

private bool BottomEdgeHasBorder(string range)
{
if (has border at bottom edge of range)
return true;
else
return false;
}

我已经搜索了很多这个问题,但我能找到的只是添加边框的问题。我只想检查是否有边框。

我试过了

Excel.Range range = ExcelWorksheet.get_Range(range, Type.Missing);
if (range.Borders[Excel.XlBordersIndex.xlEdgeBottom] == Excel.XlLineStyle.xlContinuous)
return true;

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

我相信您需要获取LineStyle属性以进行比较。我对C#语法有点生疏,但我认为这是你需要使用的。

range.Borders [Excel.XlBordersIndex.xlEdgeBottom] .LineStyle

答案 1 :(得分:1)

陷入同样的​​问题。要将边框的linestyle与linestyle枚举进行比较,请将GetHashCode()附加到linestyle枚举以获取等效的整数:

Microsoft.Office.Interop.Excel.Range range = sheet.get_Range("AK59");
Microsoft.Office.Interop.Excel.Borders borders = range.Borders;
Microsoft.Office.Interop.Excel.Border borderTop = borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop];
Microsoft.Office.Interop.Excel.Border borderLeft = borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft];
Microsoft.Office.Interop.Excel.Border borderBottom = borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom];
Microsoft.Office.Interop.Excel.Border borderRight = borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight];

if (borderBottom.LineStyle == Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous.GetHashCode())
{
    Console.WriteLine("We have a bottom border line");
}
if (borderRight.LineStyle == Microsoft.Office.Interop.Excel.XlLineStyle.xlDouble.GetHashCode())
{
    Console.WriteLine("We have a double line on the right border");
}
if (borderRight.LineStyle != Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone.GetHashCode())
{
    Console.WriteLine("yeah we have some type of border on the right");
}

答案 2 :(得分:1)

bool BottomEdgeHasBorder(Range cell)
{
      return cell.Borders[XlBordersIndex.xlEdgeBottom].LineStyle != (int)XlLineStyle.xlLineStyleNone;
}