C# Microsoft.Office.Interop.Excel:如何将边框应用到一个单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17751192/
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
Microsoft.Office.Interop.Excel: How to Apply a border to ONE CELL
提问by Van-Brad
I am looking to apply a border to one cell using the Microsoft.Office.Interop.Excel library.
我希望使用 Microsoft.Office.Interop.Excel 库将边框应用于一个单元格。
I am running a while-loop that is search for a empty cell within a certain column, once the cell is found I want to apply a border to it.
我正在运行一个while循环,它在某个列中搜索一个空单元格,一旦找到该单元格,我想对其应用边框。
I know there many forums on this using Ranges, but I can't use the range functionality since I do not know what cell it is being applied to exactly.
我知道有很多论坛都在使用范围,但我无法使用范围功能,因为我不知道它究竟应用于哪个单元格。
My idea was:
我的想法是:
(Excel.Range)xlWS.Cells[row,1].Borders.LineStyle = (Whatever Value);
Any advice? (besides links to other forums, I already looked through tons of forms)?
有什么建议吗?(除了其他论坛的链接,我已经浏览了大量表格)?
回答by jiverson
Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
Microsoft.Office.Interop.Excel.Range cell = range.Cells[1][1];
Microsoft.Office.Interop.Excel.Borders border = cell.Borders;
border[XlBordersIndex.xlEdgeLeft].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeTop].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeBottom].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeRight].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
See this answerfor more details.
有关更多详细信息,请参阅此答案。
回答by Van-Brad
Excel.Range range = xlWS.UsedRange;
Excel.Range cell = range.Cells[row, column];
Excel.Borders border = cell.Borders;
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border.Weight = 2d;
Hope this helps someone! Puts a thin line border around cell[row,column]!
希望这可以帮助某人!在单元格 [row,column] 周围放置一条细线边框!
回答by B. Clay Shannon
This is based on jiverson's answer, but is much more concise for basic needs; simply create a range, get its Borders, and add a border to the bottom of the range:
这是基于 jiveson 的回答,但对于基本需求来说更加简洁;只需创建一个范围,获取其边框,然后在该范围的底部添加一个边框:
var platypusRange = _xlSheet.Range[_xlSheet.Cells[1, 1], _xlSheet.Cells[1, 3]];
. . .
Borders border = platypusRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
Of course, you could add a border to the top and sides, too, using xlEdgeTop, xlEdgeLeft, and xlEdgeRight.
当然,您也可以使用 xlEdgeTop、xlEdgeLeft 和 xlEdgeRight 在顶部和侧面添加边框。
回答by Muhammad Waqas Aziz
To each cell in range
到范围内的每个单元格
Microsoft.Office.Interop.Excel.Range chartRange;
chartRange = workSheet.get_Range("a1", "g7");
foreach (Microsoft.Office.Interop.Excel.Range cell in chartRange.Cells)
{
cell.BorderAround2();
}
To whole range
到全范围
chartRange.BorderAround2();