10

我希望使用 Microsoft.Office.Interop.Excel 库为一个单元格应用边框。

我正在运行一个 while 循环,它在某个列中搜索一个空单元格,一旦找到该单元格,我想对其应用边框。

我知道有很多使用 Ranges 的论坛,但我不能使用 range 功能,因为我不知道它确切应用于哪个单元格。

我的想法是:

(Excel.Range)xlWS.Cells[row,1].Borders.LineStyle = (Whatever Value);

有什么建议吗?(除了其他论坛的链接,我已经浏览了很多表格)?

4

4 回答 4

19
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]!

于 2013-07-23T12:37:49.023 回答
14
    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;

有关更多详细信息,请参阅此答案

于 2013-07-19T16:58:40.840 回答
4

到范围内的每个单元格

Microsoft.Office.Interop.Excel.Range chartRange;
chartRange = workSheet.get_Range("a1", "g7");
foreach (Microsoft.Office.Interop.Excel.Range cell in chartRange.Cells)
{
    cell.BorderAround2();
}

到整个范围

chartRange.BorderAround2();
于 2016-08-26T10:03:17.747 回答
2

这是基于 jiverson 的回答,但对于基本需求来说更加简洁;只需创建一个范围,获取它的边框,然后在范围的底部添加一个边框:

var platypusRange = _xlSheet.Range[_xlSheet.Cells[1, 1], _xlSheet.Cells[1, 3]];
. . .
Borders border = platypusRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;

当然,您也可以使用 xlEdgeTop、xlEdgeLeft 和 xlEdgeRight 在顶部和侧面添加边框。

于 2015-11-12T17:35:36.167 回答