【问题标题】:Excel Interop - Draw All Borders in a RangeExcel 互操作 - 绘制范围内的所有边框
【发布时间】:2013-01-17 19:20:30
【问题描述】:

我从 Microsoft 的文档中看到,我可以使用“xlBordersIndex”属性访问单元格的特定边框边缘,例如设置单元格左边缘的边框样式:

range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle =     Excel.XlLineStyle.xlContinuous;

但如果我只想绘制所有边框怎么办?我试过了

range.BorderAround2();

但这只是在范围本身周围画了一个框,我理解。所以我尝试了

range.Cells.BorderAround2();

认为它会遍历范围内的每个单元格并在每个单元格周围放置所有边框。这不是发生的事情。因此,为了获得范围内所有单元格周围的所有边框,我必须手动访问四个边框索引中的每一个吗?

【问题讨论】:

    标签: c# excel interop


    【解决方案1】:
    Microsoft.Office.Interop.Excel.Range tRange = xlWorkSheet.UsedRange;
            tRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
            tRange.Borders.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    【解决方案2】:

    为什么不干脆做:

    Excel.Range tRange = xlWorkSheet.UsedRange;
    tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
    tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
    

    注意:在填充数据的行和单元格(范围)之后应用边框以获取范围,只需使用函数 .UsedRange()

    【讨论】:

      【解决方案3】:

      终于,我明白了。我这样做也没有影响性能。我这里拿一个简单的excel来解释一下:

      之前

      我设法将范围作为A1:C4 动态存储在 exRange 中的变量中,并使用下面的代码给出边框

      ((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
      


      之后

      【讨论】:

      • +1!直接在Borders 中设置lineStyle 属性不仅更简单,而且比在每个边框上都快得多:在我的情况下(具有~50k 单元格的工作表),操作从~145ms 到~35ms 每张,这是一个不错的 x4 改进!。
      【解决方案4】:
      For Each range In ranges
          For Each row As Range In .Range(range).Rows
              row.Cells.BorderAround(XlLineStyle.xlContinuous)
              row.Cells.Borders.Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
              row.Cells.Borders.Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
          Next
      Next
      

      【讨论】:

        【解决方案5】:
        oRange = SHEET2.get_Range("a1", "a10");
        oRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous;
        oRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous;
        oRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlContinuous;
        oRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlContinuous;
        

        【讨论】:

        • 简化:oRange.Borders.LineStyle = XlLineStyle.xlContinuous;
        • @LucaZiegler 为 .get_Item(.. 这是有效的方法。
        【解决方案6】:
        private void AllBorders(Excel.Borders _borders)
            {
                _borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
                _borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
                _borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
                _borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
                _borders.Color = Color.Black;
            }
        

        【讨论】:

          【解决方案7】:

          我还不熟悉 C#,但在 VBA 中有 Range.Borders(xlInsideVertical)Range.Borders(xlInsideHorizontal) 属性。尝试使用宏记录器并为任何工作簿区域应用所有边框。也许这会有所帮助。

          【讨论】:

          • 这个主意不错,用宏看看原生程序是怎么处理的。我最终编写了一个函数来绘制所有边框:
          • 很高兴能帮上忙的朋友)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-06
          • 2013-11-29
          • 2022-01-24
          • 1970-01-01
          相关资源
          最近更新 更多