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

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

我想要做的是创build一个方法,需要一个范围,然后返回true或失败的基础上,如果有一个边界在范围的底部边缘。

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

我已经search了很多这个问题,但我能find的是有关添加边框的问题。 我只是想检查是否有边界。

我努力了

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

伟大的任何帮助!

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

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

跑到同一个问题。 为了比较一个边框的linestyle和linestyle的enum,将GetHashCode()添加到linestyle的enum中以得到相应的整数:

 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"); } 
 bool BottomEdgeHasBorder(Range cell) { return cell.Borders[XlBordersIndex.xlEdgeBottom].LineStyle != (int)XlLineStyle.xlLineStyleNone; }