简体   繁体   English

Microsoft.Office.Interop.Excel:如何将边框应用到一个单元格

[英]Microsoft.Office.Interop.Excel: How to Apply a border to ONE CELL

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)? (除了其他论坛的链接,我已经浏览了大量表格)?

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] 周围放置一条细线边框!

    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 answer for more details.有关更多详细信息,请参阅此答案

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();

This is based on jiverson's answer, but is much more concise for basic needs;这是基于 jiveson 的回答,但对于基本需求来说更加简洁; simply create a range, get its Borders, and add a border to the bottom of the range:只需创建一个范围,获取其边框,然后在该范围的底部添加一个边框:

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 在顶部和侧面添加边框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM