如何在 C# 中为 Excel 范围添加底部边框?
How can I add a bottom border to an Excel range in C#?
我需要在电子表格的某些行中添加底部边框(一条简单的实线)。我已经定义了我需要使用的范围,但是添加边框代码的尝试失败了,正如注释掉的尝试所示:
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 );
辅助函数:
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/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;
using XlConstants = Microsoft.Office.Interop.Excel.Constants;
这对我有用:
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;
}
我需要在电子表格的某些行中添加底部边框(一条简单的实线)。我已经定义了我需要使用的范围,但是添加边框代码的尝试失败了,正如注释掉的尝试所示:
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 );
辅助函数:
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/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;
using XlConstants = Microsoft.Office.Interop.Excel.Constants;
这对我有用:
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;
}