C# Excel 互操作 - 在一个范围内绘制所有边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14663926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel Interop - Draw All Borders in a Range
提问by spickles
I see from Microsoft's documentation that I can access the particular border edges of a cell using the 'xlBordersIndex' property and for example set the border style for the left edge of a cell:
我从 Microsoft 的文档中看到,我可以使用 'xlBordersIndex' 属性访问单元格的特定边框边缘,例如设置单元格左边缘的边框样式:
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
But what if I just want to draw all borders? I have tried
但是如果我只想绘制所有边界呢?我试过了
range.BorderAround2();
but that just draws a box around the range itself, which I understand. So then I tried
但这只是在范围本身周围画一个框,我理解。所以我尝试了
range.Cells.BorderAround2();
thinking that it would go through each of the cells within the range and place all borders around each cell. This is not what occurred. So in order to get all borders around all cells in a range, must I manually access each of the four border indices?
认为它会穿过范围内的每个单元格并在每个单元格周围放置所有边框。这不是发生的事情。因此,为了获得范围内所有单元格的所有边框,我是否必须手动访问四个边框索引中的每一个?
采纳答案by spickles
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;
}
回答by Peter L.
I'm not yet familiar wit C#, but in VBA there are Range.Borders(xlInsideVertical)
and Range.Borders(xlInsideHorizontal)
properties. Try to use macro-recorder and apply all borders for any workbook region. Perhaps that will help.
我还不熟悉 C#,但在 VBA 中有Range.Borders(xlInsideVertical)
和Range.Borders(xlInsideHorizontal)
属性。尝试使用宏记录器并为任何工作簿区域应用所有边框。也许这会有所帮助。
回答by E Coder
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;
回答by Isla
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
回答by Sarath Avanavu
Finally, I got it. I did this without impacting the performance too. I am taking a simple excel to explain here :
Before
最后我明白了。我这样做也没有影响性能。我在这里用一个简单的excel来解释:
之前
I managed to store the range as A1:C4
in a variable dynamically in exRangeand used the below code to give border
我设法A1:C4
在exRange 中动态地将范围存储在一个变量中,并使用下面的代码来给出边框
((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
After
后
回答by X-Coder
Why not to do simply:
为什么不简单地做:
Excel.Range tRange = xlWorkSheet.UsedRange;
tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
Note: apply border after the row and cell (range) filled with data to get range simply using function .UsedRange()
注意:在填充数据的行和单元格(范围)之后应用边框,只需使用函数.UsedRange()即可获取范围
回答by Rakesh Kumar
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;