我怎样才能在C#中的Excel范围添加底部边框?
我需要在电子表格的某些行上添加一个底部边框(简单的实线)。 我有我需要定义的范围,但尝试添加边界代码失败了,从注释掉的尝试可以看出:
private ApplicationClass _xlApp; private Workbook _xlBook; private Sheets _xlSheets; private Worksheet _xlSheet; . . . private void AddBottomBorder(int rowToBottomBorderize) { var rangeToBottomBorderize = (Range)_xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]; //rangeToBottomBorderize.Borders[_xlApp.XlBordersIndex.xlEdgeBottom] = //rangeToBottomBorderize.Borders[XlBordersIndex.xlEdgeBottom] = 1d; //rangeToBottomBorderize.Borders[_xlSheet. //_xlSheet.Cells[rowToBottomBorderize, TOTALS_COL].Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = 1d; // someRange.Borders.Item[XlBordersIndex.xlEdgeBottom)] = ...what now? }
我需要哪些对象的属性或方法进行分配或调用,以及如何执行?
尝试这个:
setBorder( rangeToBottomBorderize.Borders[_xlApp.XlBordersIndex.xlEdgeBottom], XlBorderWeight.xlThick );
帮手function:
private static void setBorder( Border border, XlBorderWeight borderWeight ) { border.LineStyle = XlLineStyle.xlContinuous; border.ColorIndex = XlConstants.xlAutomatic; border.TintAndShade = 0; border.Weight = borderWeight; }
要清除边框,您可以使用:
border.LineStyle = XlConstants.xlNone
对于边界重量,您可能需要.xlThin
边框重量:
请参阅: https : //msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(Microsoft.Office.Interop.Excel.XlBorderWeight);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4。 6); K(DevLang-CSHARP)RD =真
对于其他的线型选项,你可以尝试(我没有试过这些):
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.border.linestyle.aspx
这里是我的使用语句(你不需要所有这些):
using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Excel; using Application = Microsoft.Office.Interop.Excel.Application; using Border = Microsoft.Office.Interop.Excel.Border; using Range = Microsoft.Office.Interop.Excel.Range; using XlBorderWeight = Microsoft.Office.Interop.Excel.XlBorderWeight; using XlLineStyle = Microsoft.Office.Interop.Excel.XlLineStyle;
这适用于我:
private void AddBottomBorder(int rowToBottomBorderize) { var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]]; Borders border = rowToBottomBorderizeRange.Borders; border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous; }